2 * Copyright (C) 2013 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 <WebCore/CrossThreadCopier.h>
35 WTF_MAKE_NONCOPYABLE(AsyncTask);
37 virtual ~AsyncTask() { }
39 virtual void performTask() = 0;
42 explicit AsyncTask() { }
45 template <typename T, typename... Arguments>
46 class AsyncTaskImpl final : public AsyncTask {
48 AsyncTaskImpl(T* callee, void (T::*method)(Arguments...), Arguments&&... arguments)
50 m_taskFunction = [callee, method, arguments...]() {
51 (callee->*method)(arguments...);
55 virtual ~AsyncTaskImpl()
60 virtual void performTask() override
65 std::function<void()> m_taskFunction;
69 std::unique_ptr<AsyncTask> createAsyncTask(
73 return std::make_unique<AsyncTaskImpl<T>>(&callee, method);
76 template<typename T, typename P1, typename MP1>
77 std::unique_ptr<AsyncTask> createAsyncTask(
79 void (T::*method)(MP1),
82 return std::make_unique<AsyncTaskImpl<T, MP1>>(
85 WebCore::CrossThreadCopier<P1>::copy(parameter1));
88 template<typename T, typename P1, typename MP1, typename P2, typename MP2>
89 std::unique_ptr<AsyncTask> createAsyncTask(
91 void (T::*method)(MP1, MP2),
95 return std::make_unique<AsyncTaskImpl<T, MP1, MP2>>(
98 WebCore::CrossThreadCopier<P1>::copy(parameter1),
99 WebCore::CrossThreadCopier<P2>::copy(parameter2));
103 template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3>
104 std::unique_ptr<AsyncTask> createAsyncTask(
106 void (T::*method)(MP1, MP2, MP3),
107 const P1& parameter1,
108 const P2& parameter2,
109 const P3& parameter3)
111 return std::make_unique<AsyncTaskImpl<T, MP1, MP2, MP3>>(
114 WebCore::CrossThreadCopier<P1>::copy(parameter1),
115 WebCore::CrossThreadCopier<P2>::copy(parameter2),
116 WebCore::CrossThreadCopier<P3>::copy(parameter3));
119 template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3, typename P4, typename MP4>
120 std::unique_ptr<AsyncTask> createAsyncTask(
122 void (T::*method)(MP1, MP2, MP3, MP4),
123 const P1& parameter1,
124 const P2& parameter2,
125 const P3& parameter3,
126 const P4& parameter4)
128 return std::make_unique<AsyncTaskImpl<T, MP1, MP2, MP3, MP4>>(
131 WebCore::CrossThreadCopier<P1>::copy(parameter1),
132 WebCore::CrossThreadCopier<P2>::copy(parameter2),
133 WebCore::CrossThreadCopier<P3>::copy(parameter3),
134 WebCore::CrossThreadCopier<P4>::copy(parameter4));
137 template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3, typename P4, typename MP4, typename P5, typename MP5, typename P6, typename MP6>
138 std::unique_ptr<AsyncTask> createAsyncTask(
140 void (T::*method)(MP1, MP2, MP3, MP4, MP5, MP6),
141 const P1& parameter1,
142 const P2& parameter2,
143 const P3& parameter3,
144 const P4& parameter4,
145 const P5& parameter5,
146 const P6& parameter6)
148 return std::make_unique<AsyncTaskImpl<T, MP1, MP2, MP3, MP4, MP5, MP6>>(
151 WebCore::CrossThreadCopier<P1>::copy(parameter1),
152 WebCore::CrossThreadCopier<P2>::copy(parameter2),
153 WebCore::CrossThreadCopier<P3>::copy(parameter3),
154 WebCore::CrossThreadCopier<P4>::copy(parameter4),
155 WebCore::CrossThreadCopier<P5>::copy(parameter5),
156 WebCore::CrossThreadCopier<P6>::copy(parameter6));
159 template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3, typename P4, typename MP4, typename P5, typename MP5, typename P6, typename MP6, typename P7, typename MP7, typename P8, typename MP8>
160 std::unique_ptr<AsyncTask> createAsyncTask(
162 void (T::*method)(MP1, MP2, MP3, MP4, MP5, MP6, MP7, MP8),
163 const P1& parameter1,
164 const P2& parameter2,
165 const P3& parameter3,
166 const P4& parameter4,
167 const P5& parameter5,
168 const P6& parameter6,
169 const P7& parameter7,
170 const P8& parameter8)
172 return std::make_unique<AsyncTaskImpl<T, MP1, MP2, MP3, MP4, MP5, MP6, MP7, MP8>>(
175 WebCore::CrossThreadCopier<P1>::copy(parameter1),
176 WebCore::CrossThreadCopier<P2>::copy(parameter2),
177 WebCore::CrossThreadCopier<P3>::copy(parameter3),
178 WebCore::CrossThreadCopier<P4>::copy(parameter4),
179 WebCore::CrossThreadCopier<P5>::copy(parameter5),
180 WebCore::CrossThreadCopier<P6>::copy(parameter6),
181 WebCore::CrossThreadCopier<P7>::copy(parameter7),
182 WebCore::CrossThreadCopier<P8>::copy(parameter8));
185 } // namespace WebKit
187 #endif // AsyncTask_h