1/*
2 * Copyright (C) 2010 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef FileThreadTask_h
32#define FileThreadTask_h
33
34#include "CrossThreadCopier.h"
35#include "CrossThreadTask.h"
36#include "FileThread.h"
37#include <wtf/PassOwnPtr.h>
38#include <wtf/PassRefPtr.h>
39
40namespace WebCore {
41
42template<typename T>
43class FileThreadTask0 : public FileThread::Task {
44public:
45    typedef void (T::*Method)();
46    typedef FileThreadTask0<T> FileThreadTaskImpl;
47
48    static PassOwnPtr<FileThreadTaskImpl> create(T* instance, Method method)
49    {
50        return adoptPtr(new FileThreadTaskImpl(instance, method));
51    }
52
53private:
54    FileThreadTask0(T* instance, Method method)
55        : FileThread::Task(instance)
56        , m_method(method)
57    {
58    }
59
60    virtual void performTask()
61    {
62        (*static_cast<T*>(instance()).*m_method)();
63    }
64
65private:
66    Method m_method;
67};
68
69template<typename T, typename P1, typename MP1>
70class FileThreadTask1 : public FileThread::Task {
71public:
72    typedef void (T::*Method)(MP1);
73    typedef FileThreadTask1<T, P1, MP1> FileThreadTaskImpl;
74    typedef typename CrossThreadTaskTraits<P1>::ParamType Param1;
75
76    static PassOwnPtr<FileThreadTaskImpl> create(T* instance, Method method, Param1 parameter1)
77    {
78        return adoptPtr(new FileThreadTaskImpl(instance, method, parameter1));
79    }
80
81private:
82    FileThreadTask1(T* instance, Method method, Param1 parameter1)
83        : FileThread::Task(instance)
84        , m_method(method)
85        , m_parameter1(parameter1)
86    {
87    }
88
89    virtual void performTask()
90    {
91        (*static_cast<T*>(instance()).*m_method)(m_parameter1);
92    }
93
94private:
95    Method m_method;
96    P1 m_parameter1;
97};
98
99template<typename T, typename P1, typename MP1, typename P2, typename MP2>
100class FileThreadTask2 : public FileThread::Task {
101public:
102    typedef void (T::*Method)(MP1, MP2);
103    typedef FileThreadTask2<T, P1, MP1, P2, MP2> FileThreadTaskImpl;
104    typedef typename CrossThreadTaskTraits<P1>::ParamType Param1;
105    typedef typename CrossThreadTaskTraits<P2>::ParamType Param2;
106
107    static PassOwnPtr<FileThreadTaskImpl> create(T* instance, Method method, Param1 parameter1, Param2 parameter2)
108    {
109        return adoptPtr(new FileThreadTaskImpl(instance, method, parameter1, parameter2));
110    }
111
112private:
113    FileThreadTask2(T* instance, Method method, Param1 parameter1, Param2 parameter2)
114        : FileThread::Task(instance)
115        , m_method(method)
116        , m_parameter1(parameter1)
117        , m_parameter2(parameter2)
118    {
119    }
120
121    virtual void performTask()
122    {
123        (*static_cast<T*>(instance()).*m_method)(m_parameter1, m_parameter2);
124    }
125
126private:
127    Method m_method;
128    P1 m_parameter1;
129    P2 m_parameter2;
130};
131
132template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3>
133class FileThreadTask3 : public FileThread::Task {
134public:
135    typedef void (T::*Method)(MP1, MP2, MP3);
136    typedef FileThreadTask3<T, P1, MP1, P2, MP2, P3, MP3> FileThreadTaskImpl;
137    typedef typename CrossThreadTaskTraits<P1>::ParamType Param1;
138    typedef typename CrossThreadTaskTraits<P2>::ParamType Param2;
139    typedef typename CrossThreadTaskTraits<P3>::ParamType Param3;
140
141    static PassOwnPtr<FileThreadTaskImpl> create(T* instance, Method method, Param1 parameter1, Param2 parameter2, Param3 parameter3)
142    {
143        return adoptPtr(new FileThreadTaskImpl(instance, method, parameter1, parameter2, parameter3));
144    }
145
146private:
147    FileThreadTask3(T* instance, Method method, Param1 parameter1, Param2 parameter2, Param3 parameter3)
148        : FileThread::Task(instance)
149        , m_method(method)
150        , m_parameter1(parameter1)
151        , m_parameter2(parameter2)
152        , m_parameter3(parameter3)
153    {
154    }
155
156    virtual void performTask()
157    {
158        (*static_cast<T*>(instance()).*m_method)(m_parameter1, m_parameter2, m_parameter3);
159    }
160
161private:
162    Method m_method;
163    P1 m_parameter1;
164    P2 m_parameter2;
165    P3 m_parameter3;
166};
167
168template<typename T>
169PassOwnPtr<FileThread::Task> createFileThreadTask(
170    T* const callee,
171    void (T::*method)());
172
173template<typename T>
174PassOwnPtr<FileThread::Task> createFileThreadTask(
175    T* const callee,
176    void (T::*method)())
177{
178    return FileThreadTask0<T>::create(
179        callee,
180        method);
181}
182
183template<typename T, typename P1, typename MP1>
184PassOwnPtr<FileThread::Task> createFileThreadTask(
185    T* const callee,
186    void (T::*method)(MP1),
187    const P1& parameter1)
188{
189    return FileThreadTask1<T, typename CrossThreadCopier<P1>::Type, MP1>::create(
190        callee,
191        method,
192        CrossThreadCopier<P1>::copy(parameter1));
193}
194
195template<typename T, typename P1, typename MP1, typename P2, typename MP2>
196PassOwnPtr<FileThread::Task> createFileThreadTask(
197    T* const callee,
198    void (T::*method)(MP1, MP2),
199    const P1& parameter1,
200    const P2& parameter2)
201{
202    return FileThreadTask2<T, typename CrossThreadCopier<P1>::Type, MP1, typename CrossThreadCopier<P2>::Type, MP2>::create(
203        callee,
204        method,
205        CrossThreadCopier<P1>::copy(parameter1),
206        CrossThreadCopier<P2>::copy(parameter2));
207}
208
209template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3>
210PassOwnPtr<FileThread::Task> createFileThreadTask(
211    T* const callee,
212    void (T::*method)(MP1, MP2, MP3),
213    const P1& parameter1,
214    const P2& parameter2,
215    const P3& parameter3)
216{
217    return FileThreadTask3<T, typename CrossThreadCopier<P1>::Type, MP1, typename CrossThreadCopier<P2>::Type, MP2, typename CrossThreadCopier<P3>::Type, MP3>::create(
218        callee,
219        method,
220        CrossThreadCopier<P1>::copy(parameter1),
221        CrossThreadCopier<P2>::copy(parameter2),
222        CrossThreadCopier<P3>::copy(parameter3));
223}
224
225} // namespace WebCore
226
227#endif // FileThreadTask_h
228