• 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 CCMainThreadTask_h
25 #define CCMainThreadTask_h
26 
27 #include "CCMainThread.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 MainThreadTask0 : public CCMainThread::Task {
37 public:
38     typedef void (T::*Method)();
39     typedef MainThreadTask0<T> MainThreadTaskImpl;
40 
create(T * instance,Method method)41     static PassOwnPtr<MainThreadTaskImpl> create(T* instance, Method method)
42     {
43         return adoptPtr(new MainThreadTaskImpl(instance, method));
44     }
45 
46 private:
MainThreadTask0(T * instance,Method method)47     MainThreadTask0(T* instance, Method method)
48         : CCMainThread::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 MainThreadTask1 : public CCMainThread::Task {
64 public:
65     typedef void (T::*Method)(MP1);
66     typedef MainThreadTask1<T, P1, MP1> MainThreadTaskImpl;
67     typedef typename CrossThreadTaskTraits<P1>::ParamType Param1;
68 
create(T * instance,Method method,Param1 parameter1)69     static PassOwnPtr<MainThreadTaskImpl> create(T* instance, Method method, Param1 parameter1)
70     {
71         return adoptPtr(new MainThreadTaskImpl(instance, method, parameter1));
72     }
73 
74 private:
MainThreadTask1(T * instance,Method method,Param1 parameter1)75     MainThreadTask1(T* instance, Method method, Param1 parameter1)
76         : CCMainThread::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 MainThreadTask2 : public CCMainThread::Task {
94 public:
95     typedef void (T::*Method)(MP1, MP2);
96     typedef MainThreadTask2<T, P1, MP1, P2, MP2> MainThreadTaskImpl;
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<MainThreadTaskImpl> create(T* instance, Method method, Param1 parameter1, Param2 parameter2)
101     {
102         return adoptPtr(new MainThreadTaskImpl(instance, method, parameter1, parameter2));
103     }
104 
105 private:
MainThreadTask2(T * instance,Method method,Param1 parameter1,Param2 parameter2)106     MainThreadTask2(T* instance, Method method, Param1 parameter1, Param2 parameter2)
107         : CCMainThread::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 MainThreadTask3 : public CCMainThread::Task {
127 public:
128     typedef void (T::*Method)(MP1, MP2, MP3);
129     typedef MainThreadTask3<T, P1, MP1, P2, MP2, P3, MP3> MainThreadTaskImpl;
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<MainThreadTaskImpl> create(T* instance, Method method, Param1 parameter1, Param2 parameter2, Param3 parameter3)
135     {
136         return adoptPtr(new MainThreadTaskImpl(instance, method, parameter1, parameter2, parameter3));
137     }
138 
139 private:
MainThreadTask3(T * instance,Method method,Param1 parameter1,Param2 parameter2,Param3 parameter3)140     MainThreadTask3(T* instance, Method method, Param1 parameter1, Param2 parameter2, Param3 parameter3)
141         : CCMainThread::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 template<typename T>
162 PassOwnPtr<CCMainThread::Task> createMainThreadTask(
163     T* const callee,
164     void (T::*method)());
165 
166 template<typename T>
createMainThreadTask(T * const callee,void (T::* method)())167 PassOwnPtr<CCMainThread::Task> createMainThreadTask(
168     T* const callee,
169     void (T::*method)())
170 {
171     return MainThreadTask0<T>::create(
172         callee,
173         method);
174 }
175 
176 template<typename T, typename P1, typename MP1>
createMainThreadTask(T * const callee,void (T::* method)(MP1),const P1 & parameter1)177 PassOwnPtr<CCMainThread::Task> createMainThreadTask(
178     T* const callee,
179     void (T::*method)(MP1),
180     const P1& parameter1)
181 {
182     return MainThreadTask1<T, typename CrossThreadCopier<P1>::Type, MP1>::create(
183         callee,
184         method,
185         CrossThreadCopier<P1>::copy(parameter1));
186 }
187 
188 template<typename T, typename P1, typename MP1, typename P2, typename MP2>
createMainThreadTask(T * const callee,void (T::* method)(MP1,MP2),const P1 & parameter1,const P2 & parameter2)189 PassOwnPtr<CCMainThread::Task> createMainThreadTask(
190     T* const callee,
191     void (T::*method)(MP1, MP2),
192     const P1& parameter1,
193     const P2& parameter2)
194 {
195     return MainThreadTask2<T, typename CrossThreadCopier<P1>::Type, MP1, typename CrossThreadCopier<P2>::Type, MP2>::create(
196         callee,
197         method,
198         CrossThreadCopier<P1>::copy(parameter1),
199         CrossThreadCopier<P2>::copy(parameter2));
200 }
201 
202 template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3>
createMainThreadTask(T * const callee,void (T::* method)(MP1,MP2,MP3),const P1 & parameter1,const P2 & parameter2,const P3 & parameter3)203 PassOwnPtr<CCMainThread::Task> createMainThreadTask(
204     T* const callee,
205     void (T::*method)(MP1, MP2, MP3),
206     const P1& parameter1,
207     const P2& parameter2,
208     const P3& parameter3)
209 {
210     return MainThreadTask3<T, typename CrossThreadCopier<P1>::Type, MP1, typename CrossThreadCopier<P2>::Type, MP2, typename CrossThreadCopier<P3>::Type, MP3>::create(
211         callee,
212         method,
213         CrossThreadCopier<P1>::copy(parameter1),
214         CrossThreadCopier<P2>::copy(parameter2),
215         CrossThreadCopier<P3>::copy(parameter3));
216 }
217 
218 } // namespace WebCore
219 
220 #endif // CCMainThreadTask_h
221