initial import
[vuplus_webkit] / Source / WebKit2 / Platform / WorkItem.h
1 /*
2  * Copyright (C) 2010 Apple 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''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef WorkItem_h
27 #define WorkItem_h
28
29 #include <wtf/PassOwnPtr.h>
30
31 class WorkItem {
32 public:
33     template<typename C> 
34     static PassOwnPtr<WorkItem> create(C*, void (C::*)());
35
36     template<typename C, typename T0>
37     static PassOwnPtr<WorkItem> create(C*, void (C::*)(T0), T0);
38
39     template<typename C, typename T0, typename T1>
40     static PassOwnPtr<WorkItem> create(C*, void (C::*)(T0, T1), T0, T1);
41
42     static PassOwnPtr<WorkItem> create(void (*)());
43
44     template<typename C>
45     static PassOwnPtr<WorkItem> createDeref(C*);
46
47     virtual ~WorkItem() { }
48     virtual void execute() = 0;
49
50 protected:
51     WorkItem() { }
52
53 private:
54     WorkItem(const WorkItem&);
55     WorkItem& operator=(const WorkItem&);
56 };
57
58 template <typename C>
59 class MemberFunctionWorkItem0 : private WorkItem {
60     // We only allow WorkItem to create this.
61     friend class WorkItem;
62
63     typedef void (C::*FunctionType)();
64     
65     MemberFunctionWorkItem0(C* ptr, FunctionType function)
66         : m_ptr(ptr)
67         , m_function(function)
68     {
69         m_ptr->ref();
70     }
71
72     ~MemberFunctionWorkItem0()
73     {
74         m_ptr->deref();
75     }
76
77     virtual void execute()
78     {
79         (m_ptr->*m_function)();
80     }
81
82     C* m_ptr;
83     FunctionType m_function;
84 };
85
86 template<typename C, typename T0>
87 class MemberFunctionWorkItem1 : private WorkItem {
88     // We only allow WorkItem to create this.
89     friend class WorkItem;
90     
91     typedef void (C::*FunctionType)(T0);
92     
93     MemberFunctionWorkItem1(C* ptr, FunctionType function, T0 t0)
94         : m_ptr(ptr)
95         , m_function(function)
96         , m_t0(t0)
97     {
98         m_ptr->ref();
99     }
100     
101     ~MemberFunctionWorkItem1()
102     {
103         m_ptr->deref();
104     }
105
106     virtual void execute()
107     {
108         (m_ptr->*m_function)(m_t0);
109     }
110     
111     C* m_ptr;
112     FunctionType m_function;
113     T0 m_t0;
114 };
115
116 template<typename C, typename T0, typename T1>
117 class MemberFunctionWorkItem2 : private WorkItem {
118     // We only allow WorkItem to create this.
119     friend class WorkItem;
120     
121     typedef void (C::*FunctionType)(T0, T1);
122     
123     MemberFunctionWorkItem2(C* ptr, FunctionType function, T0 t0, T1 t1)
124         : m_ptr(ptr)
125         , m_function(function)
126         , m_t0(t0)
127         , m_t1(t1)
128     {
129         m_ptr->ref();
130     }
131     
132     ~MemberFunctionWorkItem2()
133     {
134         m_ptr->deref();
135     }
136
137     virtual void execute()
138     {
139         (m_ptr->*m_function)(m_t0, m_t1);
140     }
141     
142     C* m_ptr;
143     FunctionType m_function;
144     T0 m_t0;
145     T1 m_t1;
146 };
147
148 template<typename C>
149 PassOwnPtr<WorkItem> WorkItem::create(C* ptr, void (C::*function)())
150 {
151     return adoptPtr(static_cast<WorkItem*>(new MemberFunctionWorkItem0<C>(ptr, function)));
152 }
153
154 template<typename C, typename T0>
155 PassOwnPtr<WorkItem> WorkItem::create(C* ptr, void (C::*function)(T0), T0 t0)
156 {
157     return adoptPtr(static_cast<WorkItem*>(new MemberFunctionWorkItem1<C, T0>(ptr, function, t0)));
158 }
159
160 template<typename C, typename T0, typename T1>
161 PassOwnPtr<WorkItem> WorkItem::create(C* ptr, void (C::*function)(T0, T1), T0 t0, T1 t1)
162 {
163     return adoptPtr(static_cast<WorkItem*>(new MemberFunctionWorkItem2<C, T0, T1>(ptr, function, t0, t1)));
164 }
165
166 class FunctionWorkItem0 : private WorkItem {
167     // We only allow WorkItem to create this.
168     friend class WorkItem;
169
170     typedef void (*FunctionType)();
171     
172     FunctionWorkItem0(FunctionType function)
173         : m_function(function)
174     {
175     }
176
177     virtual void execute()
178     {
179         (*m_function)();
180     }
181
182     FunctionType m_function;
183 };
184
185 inline PassOwnPtr<WorkItem> WorkItem::create(void (*function)())
186 {
187     return adoptPtr(static_cast<WorkItem*>(new FunctionWorkItem0(function)));
188 }
189
190 template<typename C>
191 class DerefWorkItem : private WorkItem {
192     // We only allow WorkItem to create this.
193     friend class WorkItem;
194
195     explicit DerefWorkItem(C* ptr)
196         : m_ptr(ptr)
197     {
198     }
199
200     virtual void execute()
201     {
202         m_ptr->deref();
203     }
204
205     C* m_ptr;
206 };
207
208 template<typename C>
209 PassOwnPtr<WorkItem> WorkItem::createDeref(C* ptr)
210 {
211     return adoptPtr(static_cast<WorkItem*>(new DerefWorkItem<C>(ptr)));
212 }
213
214 #endif // WorkItem_h