Minor updates for versions 0.2.1 and 0.2.2. See changelog file.
[movie-schedule] / src / utils / asynccall.h
1 // Copyright 2010 Jochen Becher
2 //
3 // This file is part of MovieSchedule.
4 //
5 // MovieSchedule is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // MovieSchedule is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with MovieSchedule.  If not, see <http://www.gnu.org/licenses/>.
17
18 #ifndef ASYNCCALL_H
19 #define ASYNCCALL_H
20
21 #include "asynccallclient.h"
22 #include "asynccallproxy.h"
23
24 namespace AsyncCallPrivate {
25
26     template<class OBJECT, typename METHOD>
27     class AsyncCall0 : public CallProxyBase
28     {
29     public:
30         AsyncCall0(OBJECT *object, const METHOD &method)
31             : _object(object),
32             _method(method)
33         {
34         }
35
36         void CallClient()
37         {
38             AsyncCallClient *client = new AsyncCallClient(_object->thread());
39             client->Call(this);
40         }
41
42         virtual void Call()
43         {
44             (_object->*_method)();
45         }
46
47     private:
48         OBJECT *_object;
49         METHOD _method;
50     };
51
52     template<class OBJECT, typename METHOD, typename ARG1>
53     class AsyncCall1 : public CallProxyBase
54     {
55     public:
56         AsyncCall1(OBJECT *object, const METHOD &method, const ARG1 &arg1)
57             : _object(object),
58             _method(method),
59             _arg1(arg1)
60         {   
61         }
62
63         void CallClient()
64         {
65             AsyncCallClient *client = new AsyncCallClient(_object->thread());
66             client->Call(this);
67         }
68
69         virtual void Call()
70         {
71             (_object->*_method)(_arg1);
72         }
73
74     private:
75         OBJECT *_object;
76         METHOD _method;
77         ARG1 _arg1;
78     };
79
80     template<class OBJECT, typename METHOD, typename ARG1, typename ARG2>
81     class AsyncCall2 : public CallProxyBase
82     {
83     public:
84         AsyncCall2(OBJECT *object, const METHOD &method, const ARG1 &arg1, const ARG2 &arg2)
85             : _object(object),
86             _method(method),
87             _arg1(arg1),
88             _arg2(arg2)
89         {
90         }
91
92         void CallClient()
93         {
94             AsyncCallClient *client = new AsyncCallClient(_object->thread());
95             client->Call(this);
96         }
97
98         virtual void Call()
99         {
100             (_object->*_method)(_arg1, _arg2);
101         }
102
103     private:
104         OBJECT *_object;
105         METHOD _method;
106         ARG1 _arg1;
107         ARG2 _arg2;
108     };
109
110     template<class OBJECT, typename METHOD, typename ARG1, typename ARG2, typename ARG3>
111     class AsyncCall3 : public CallProxyBase
112     {
113     public:
114         AsyncCall3(OBJECT *object, const METHOD &method, const ARG1 &arg1, const ARG2 &arg2, const ARG3 &arg3)
115             : _object(object),
116             _method(method),
117             _arg1(arg1),
118             _arg2(arg2),
119             _arg3(arg3)
120         {
121         }
122
123         void CallClient()
124         {
125             AsyncCallClient *client = new AsyncCallClient(_object->thread());
126             client->Call(this);
127         }
128
129         virtual void Call()
130         {
131             (_object->*_method)(_arg1, _arg2, _arg3);
132         }
133
134     private:
135         OBJECT *_object;
136         METHOD _method;
137         ARG1 _arg1;
138         ARG2 _arg2;
139         ARG3 _arg3;
140     };
141
142     template<class OBJECT, typename METHOD, typename ARG1, typename ARG2, typename ARG3, typename ARG4>
143     class AsyncCall4 : public CallProxyBase
144     {
145     public:
146         AsyncCall4(OBJECT *object, const METHOD &method, const ARG1 &arg1, const ARG2 &arg2, const ARG3 &arg3, const ARG4 &arg4)
147             : _object(object),
148             _method(method),
149             _arg1(arg1),
150             _arg2(arg2),
151             _arg3(arg3),
152             _arg4(arg4)
153         {
154         }
155
156         void CallClient()
157         {
158             AsyncCallClient *client = new AsyncCallClient(_object->thread());
159             client->Call(this);
160         }
161
162         virtual void Call()
163         {
164             (_object->*_method)(_arg1, _arg2, _arg3, _arg4);
165         }
166
167     private:
168         OBJECT *_object;
169         METHOD _method;
170         ARG1 _arg1;
171         ARG2 _arg2;
172         ARG3 _arg3;
173         ARG4 _arg4;
174     };
175 }
176
177 template<class OBJECT, typename METHOD>
178 void CallAsync(OBJECT *object, const METHOD &method)
179 {
180     AsyncCallPrivate::AsyncCall0<OBJECT, METHOD> *async_call = new AsyncCallPrivate::AsyncCall0<OBJECT, METHOD>(object, method);
181     async_call->CallClient();
182 }
183
184 template<class OBJECT, typename METHOD, typename ARG1>
185 void CallAsync(OBJECT *object, const METHOD &method, const ARG1 &arg1)
186 {
187     AsyncCallPrivate::AsyncCall1<OBJECT, METHOD, ARG1> *async_call = new AsyncCallPrivate::AsyncCall1<OBJECT, METHOD, ARG1>(object, method, arg1);
188     async_call->CallClient();
189 }
190
191 template<class OBJECT, typename METHOD, typename ARG1, typename ARG2>
192 void CallAsync(OBJECT *object, const METHOD &method, const ARG1 &arg1, const ARG2 &arg2)
193 {
194     AsyncCallPrivate::AsyncCall2<OBJECT, METHOD, ARG1, ARG2> *async_call = new AsyncCallPrivate::AsyncCall2<OBJECT, METHOD, ARG1, ARG2>(object, method, arg1, arg2);
195     async_call->CallClient();
196 }
197
198 template<class OBJECT, typename METHOD, typename ARG1, typename ARG2, typename ARG3>
199 void CallAsync(OBJECT *object, const METHOD &method, const ARG1 &arg1, const ARG2 &arg2, const ARG3 &arg3)
200 {
201     AsyncCallPrivate::AsyncCall3<OBJECT, METHOD, ARG1, ARG2, ARG3> *async_call = new AsyncCallPrivate::AsyncCall3<OBJECT, METHOD, ARG1, ARG2, ARG3>(object, method, arg1, arg2, arg3);
202     async_call->CallClient();
203 }
204
205 template<class OBJECT, typename METHOD, typename ARG1, typename ARG2, typename ARG3, typename ARG4>
206 void CallAsync(OBJECT *object, const METHOD &method, const ARG1 &arg1, const ARG2 &arg2, const ARG3 &arg3, const ARG4 &arg4)
207 {
208     AsyncCallPrivate::AsyncCall4<OBJECT, METHOD, ARG1, ARG2, ARG3, ARG4> *async_call = new AsyncCallPrivate::AsyncCall4<OBJECT, METHOD, ARG1, ARG2, ARG3, ARG4>(object, method, arg1, arg2, arg3, arg4);
209     async_call->CallClient();
210 }
211
212 #endif // ASYNCCALL_H