2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include <wtf/PassOwnPtr.h>
34 static PassOwnPtr<WorkItem> create(C*, void (C::*)());
36 template<typename C, typename T0>
37 static PassOwnPtr<WorkItem> create(C*, void (C::*)(T0), T0);
39 template<typename C, typename T0, typename T1>
40 static PassOwnPtr<WorkItem> create(C*, void (C::*)(T0, T1), T0, T1);
42 static PassOwnPtr<WorkItem> create(void (*)());
45 static PassOwnPtr<WorkItem> createDeref(C*);
47 virtual ~WorkItem() { }
48 virtual void execute() = 0;
54 WorkItem(const WorkItem&);
55 WorkItem& operator=(const WorkItem&);
59 class MemberFunctionWorkItem0 : private WorkItem {
60 // We only allow WorkItem to create this.
61 friend class WorkItem;
63 typedef void (C::*FunctionType)();
65 MemberFunctionWorkItem0(C* ptr, FunctionType function)
67 , m_function(function)
72 ~MemberFunctionWorkItem0()
77 virtual void execute()
79 (m_ptr->*m_function)();
83 FunctionType m_function;
86 template<typename C, typename T0>
87 class MemberFunctionWorkItem1 : private WorkItem {
88 // We only allow WorkItem to create this.
89 friend class WorkItem;
91 typedef void (C::*FunctionType)(T0);
93 MemberFunctionWorkItem1(C* ptr, FunctionType function, T0 t0)
95 , m_function(function)
101 ~MemberFunctionWorkItem1()
106 virtual void execute()
108 (m_ptr->*m_function)(m_t0);
112 FunctionType m_function;
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;
121 typedef void (C::*FunctionType)(T0, T1);
123 MemberFunctionWorkItem2(C* ptr, FunctionType function, T0 t0, T1 t1)
125 , m_function(function)
132 ~MemberFunctionWorkItem2()
137 virtual void execute()
139 (m_ptr->*m_function)(m_t0, m_t1);
143 FunctionType m_function;
149 PassOwnPtr<WorkItem> WorkItem::create(C* ptr, void (C::*function)())
151 return adoptPtr(static_cast<WorkItem*>(new MemberFunctionWorkItem0<C>(ptr, function)));
154 template<typename C, typename T0>
155 PassOwnPtr<WorkItem> WorkItem::create(C* ptr, void (C::*function)(T0), T0 t0)
157 return adoptPtr(static_cast<WorkItem*>(new MemberFunctionWorkItem1<C, T0>(ptr, function, t0)));
160 template<typename C, typename T0, typename T1>
161 PassOwnPtr<WorkItem> WorkItem::create(C* ptr, void (C::*function)(T0, T1), T0 t0, T1 t1)
163 return adoptPtr(static_cast<WorkItem*>(new MemberFunctionWorkItem2<C, T0, T1>(ptr, function, t0, t1)));
166 class FunctionWorkItem0 : private WorkItem {
167 // We only allow WorkItem to create this.
168 friend class WorkItem;
170 typedef void (*FunctionType)();
172 FunctionWorkItem0(FunctionType function)
173 : m_function(function)
177 virtual void execute()
182 FunctionType m_function;
185 inline PassOwnPtr<WorkItem> WorkItem::create(void (*function)())
187 return adoptPtr(static_cast<WorkItem*>(new FunctionWorkItem0(function)));
191 class DerefWorkItem : private WorkItem {
192 // We only allow WorkItem to create this.
193 friend class WorkItem;
195 explicit DerefWorkItem(C* ptr)
200 virtual void execute()
209 PassOwnPtr<WorkItem> WorkItem::createDeref(C* ptr)
211 return adoptPtr(static_cast<WorkItem*>(new DerefWorkItem<C>(ptr)));