• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1.  Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 #ifndef CCThreadTask_h
25 #define CCThreadTask_h
26 
27 #include "CCThread.h"
28 #include "CrossThreadCopier.h"
29 #include "CrossThreadTask.h"
30 #include <wtf/PassOwnPtr.h>
31 #include <wtf/PassRefPtr.h>
32 
33 namespace WebCore {
34 
35 template<typename T>
36 class CCThreadTask0 : public CCThread::Task {
37 public:
38     typedef void (T::*Method)();
39     typedef CCThreadTask0<T> CCThreadTaskImpl;
40 
create(T * instance,Method method)41     static PassOwnPtr<CCThreadTaskImpl> create(T* instance, Method method)
42     {
43         return adoptPtr(new CCThreadTaskImpl(instance, method));
44     }
45 
46 private:
CCThreadTask0(T * instance,Method method)47     CCThreadTask0(T* instance, Method method)
48         : CCThread::Task(instance)
49         , m_method(method)
50     {
51     }
52 
performTask()53     virtual void performTask()
54     {
55         (*static_cast<T*>(instance()).*m_method)();
56     }
57 
58 private:
59     Method m_method;
60 };
61 
62 template<typename T, typename P1, typename MP1>
63 class CCThreadTask1 : public CCThread::Task {
64 public:
65     typedef void (T::*Method)(MP1);
66     typedef CCThreadTask1<T, P1, MP1> CCThreadTaskImpl;
67     typedef typename CrossThreadTaskTraits<P1>::ParamType Param1;
68 
create(T * instance,Method method,Param1 parameter1)69     static PassOwnPtr<CCThreadTaskImpl> create(T* instance, Method method, Param1 parameter1)
70     {
71         return adoptPtr(new CCThreadTaskImpl(instance, method, parameter1));
72     }
73 
74 private:
CCThreadTask1(T * instance,Method method,Param1 parameter1)75     CCThreadTask1(T* instance, Method method, Param1 parameter1)
76         : CCThread::Task(instance)
77         , m_method(method)
78         , m_parameter1(parameter1)
79     {
80     }
81 
performTask()82     virtual void performTask()
83     {
84         (*static_cast<T*>(instance()).*m_method)(m_parameter1);
85     }
86 
87 private:
88     Method m_method;
89     P1 m_parameter1;
90 };
91 
92 template<typename T, typename P1, typename MP1, typename P2, typename MP2>
93 class CCThreadTask2 : public CCThread::Task {
94 public:
95     typedef void (T::*Method)(MP1, MP2);
96     typedef CCThreadTask2<T, P1, MP1, P2, MP2> CCThreadTaskImpl;
97     typedef typename CrossThreadTaskTraits<P1>::ParamType Param1;
98     typedef typename CrossThreadTaskTraits<P2>::ParamType Param2;
99 
create(T * instance,Method method,Param1 parameter1,Param2 parameter2)100     static PassOwnPtr<CCThreadTaskImpl> create(T* instance, Method method, Param1 parameter1, Param2 parameter2)
101     {
102         return adoptPtr(new CCThreadTaskImpl(instance, method, parameter1, parameter2));
103     }
104 
105 private:
CCThreadTask2(T * instance,Method method,Param1 parameter1,Param2 parameter2)106     CCThreadTask2(T* instance, Method method, Param1 parameter1, Param2 parameter2)
107         : CCThread::Task(instance)
108         , m_method(method)
109         , m_parameter1(parameter1)
110         , m_parameter2(parameter2)
111     {
112     }
113 
performTask()114     virtual void performTask()
115     {
116         (*static_cast<T*>(instance()).*m_method)(m_parameter1, m_parameter2);
117     }
118 
119 private:
120     Method m_method;
121     P1 m_parameter1;
122     P2 m_parameter2;
123 };
124 
125 template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3>
126 class CCThreadTask3 : public CCThread::Task {
127 public:
128     typedef void (T::*Method)(MP1, MP2, MP3);
129     typedef CCThreadTask3<T, P1, MP1, P2, MP2, P3, MP3> CCThreadTaskImpl;
130     typedef typename CrossThreadTaskTraits<P1>::ParamType Param1;
131     typedef typename CrossThreadTaskTraits<P2>::ParamType Param2;
132     typedef typename CrossThreadTaskTraits<P3>::ParamType Param3;
133 
create(T * instance,Method method,Param1 parameter1,Param2 parameter2,Param3 parameter3)134     static PassOwnPtr<CCThreadTaskImpl> create(T* instance, Method method, Param1 parameter1, Param2 parameter2, Param3 parameter3)
135     {
136         return adoptPtr(new CCThreadTaskImpl(instance, method, parameter1, parameter2, parameter3));
137     }
138 
139 private:
CCThreadTask3(T * instance,Method method,Param1 parameter1,Param2 parameter2,Param3 parameter3)140     CCThreadTask3(T* instance, Method method, Param1 parameter1, Param2 parameter2, Param3 parameter3)
141         : CCThread::Task(instance)
142         , m_method(method)
143         , m_parameter1(parameter1)
144         , m_parameter2(parameter2)
145         , m_parameter3(parameter3)
146     {
147     }
148 
performTask()149     virtual void performTask()
150     {
151         (*static_cast<T*>(instance()).*m_method)(m_parameter1, m_parameter2, m_parameter3);
152     }
153 
154 private:
155     Method m_method;
156     P1 m_parameter1;
157     P2 m_parameter2;
158     P3 m_parameter3;
159 };
160 
161 
162 template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3, typename P4, typename MP4>
163 class CCThreadTask4 : public CCThread::Task {
164 public:
165     typedef void (T::*Method)(MP1, MP2, MP3, MP4);
166     typedef CCThreadTask4<T, P1, MP1, P2, MP2, P3, MP3, P4, MP4> CCThreadTaskImpl;
167     typedef typename CrossThreadTaskTraits<P1>::ParamType Param1;
168     typedef typename CrossThreadTaskTraits<P2>::ParamType Param2;
169     typedef typename CrossThreadTaskTraits<P3>::ParamType Param3;
170     typedef typename CrossThreadTaskTraits<P4>::ParamType Param4;
171 
create(T * instance,Method method,Param1 parameter1,Param2 parameter2,Param3 parameter3,Param4 parameter4)172     static PassOwnPtr<CCThreadTaskImpl> create(T* instance, Method method, Param1 parameter1, Param2 parameter2, Param3 parameter3, Param4 parameter4)
173     {
174         return adoptPtr(new CCThreadTaskImpl(instance, method, parameter1, parameter2, parameter3, parameter4));
175     }
176 
177 private:
CCThreadTask4(T * instance,Method method,Param1 parameter1,Param2 parameter2,Param3 parameter3,Param4 parameter4)178     CCThreadTask4(T* instance, Method method, Param1 parameter1, Param2 parameter2, Param3 parameter3, Param4 parameter4)
179         : CCThread::Task(instance)
180         , m_method(method)
181         , m_parameter1(parameter1)
182         , m_parameter2(parameter2)
183         , m_parameter3(parameter3)
184         , m_parameter4(parameter4)
185     {
186     }
187 
performTask()188     virtual void performTask()
189     {
190         (*static_cast<T*>(instance()).*m_method)(m_parameter1, m_parameter2, m_parameter3, m_parameter4);
191     }
192 
193 private:
194     Method m_method;
195     P1 m_parameter1;
196     P2 m_parameter2;
197     P3 m_parameter3;
198     P4 m_parameter4;
199 };
200 
201 template<typename T>
202 PassOwnPtr<CCThread::Task> createCCThreadTask(
203     T* const callee,
204     void (T::*method)());
205 
206 template<typename T>
createCCThreadTask(T * const callee,void (T::* method)())207 PassOwnPtr<CCThread::Task> createCCThreadTask(
208     T* const callee,
209     void (T::*method)())
210 {
211     return CCThreadTask0<T>::create(
212         callee,
213         method);
214 }
215 
216 template<typename T, typename P1, typename MP1>
createCCThreadTask(T * const callee,void (T::* method)(MP1),const P1 & parameter1)217 PassOwnPtr<CCThread::Task> createCCThreadTask(
218     T* const callee,
219     void (T::*method)(MP1),
220     const P1& parameter1)
221 {
222     return CCThreadTask1<T, typename CrossThreadCopier<P1>::Type, MP1>::create(
223         callee,
224         method,
225         CrossThreadCopier<P1>::copy(parameter1));
226 }
227 
228 template<typename T, typename P1, typename MP1, typename P2, typename MP2>
createCCThreadTask(T * const callee,void (T::* method)(MP1,MP2),const P1 & parameter1,const P2 & parameter2)229 PassOwnPtr<CCThread::Task> createCCThreadTask(
230     T* const callee,
231     void (T::*method)(MP1, MP2),
232     const P1& parameter1,
233     const P2& parameter2)
234 {
235     return CCThreadTask2<T, typename CrossThreadCopier<P1>::Type, MP1, typename CrossThreadCopier<P2>::Type, MP2>::create(
236         callee,
237         method,
238         CrossThreadCopier<P1>::copy(parameter1),
239         CrossThreadCopier<P2>::copy(parameter2));
240 }
241 
242 template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3>
createCCThreadTask(T * const callee,void (T::* method)(MP1,MP2,MP3),const P1 & parameter1,const P2 & parameter2,const P3 & parameter3)243 PassOwnPtr<CCThread::Task> createCCThreadTask(
244     T* const callee,
245     void (T::*method)(MP1, MP2, MP3),
246     const P1& parameter1,
247     const P2& parameter2,
248     const P3& parameter3)
249 {
250     return CCThreadTask3<T, typename CrossThreadCopier<P1>::Type, MP1, typename CrossThreadCopier<P2>::Type, MP2, typename CrossThreadCopier<P3>::Type, MP3>::create(
251         callee,
252         method,
253         CrossThreadCopier<P1>::copy(parameter1),
254         CrossThreadCopier<P2>::copy(parameter2),
255         CrossThreadCopier<P3>::copy(parameter3));
256 }
257 
258 template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3, typename P4, typename MP4>
createCCThreadTask(T * const callee,void (T::* method)(MP1,MP2,MP3,MP4),const P1 & parameter1,const P2 & parameter2,const P3 & parameter3,const P4 & parameter4)259 PassOwnPtr<CCThread::Task> createCCThreadTask(
260     T* const callee,
261     void (T::*method)(MP1, MP2, MP3, MP4),
262     const P1& parameter1,
263     const P2& parameter2,
264     const P3& parameter3,
265     const P4& parameter4)
266 {
267     return CCThreadTask4<T, typename CrossThreadCopier<P1>::Type, MP1, typename CrossThreadCopier<P2>::Type, MP2, typename CrossThreadCopier<P3>::Type, MP3, typename CrossThreadCopier<P4>::Type, MP4>::create(
268         callee,
269         method,
270         CrossThreadCopier<P1>::copy(parameter1),
271         CrossThreadCopier<P2>::copy(parameter2),
272         CrossThreadCopier<P3>::copy(parameter3),
273         CrossThreadCopier<P4>::copy(parameter4));
274 
275 }
276 
277 } // namespace WebCore
278 
279 #endif // CCThreadTask_h
280