1 /***************************************************************************
2 * Copyright (C) 2009, 2012 by Stefan Fuhrmann *
3 * stefanfuhrmann@alice-dsl.de *
5 * This program 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 2 of the License, or *
8 * (at your option) any later version. *
10 * This program 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. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
29 * Execute a call to a funtion returning @a void asynchronuously
32 * Accepts a pointer to some free function or member function plus
33 * parameters and schedules it for execution it in the given job scheduler.
34 * If no scheduler is given, the default scheduler is used (see
35 * \ref CJobScheduler::GetDefault for details).
37 * Instances of this class must be allocated dynamically and will be
38 * deleted automatically immediately after function execution finished.
40 * Please note that the parameters must remain valid until the call
41 * actually got executed. Therefore, only pointer and value parameter
42 * are possible; reference parameters won't compile.
45 class CAsyncCall
: public CJobBase
54 virtual void Execute() = 0;
57 class CCallStd
: public ICall
61 std::function
<void()> func
;
65 CCallStd (std::function
<void()> f
)
70 virtual void Execute() override
76 class CCall0
: public ICall
84 CCall0 (void (*func
)())
89 virtual void Execute() override
96 class CCallMember0
: public ICall
105 CCallMember0 (C
* instance
, void (C::*func
)())
106 : instance (instance
)
111 virtual void Execute() override
118 class CCall1
: public ICall
127 CCall1 (void (*func
)(T1
), T1 parameter1
)
128 : parameter1 (parameter1
)
133 virtual void Execute() override
139 template<class C
, class T1
>
140 class CCallMember1
: public ICall
150 CCallMember1 (C
* instance
, void (C::*func
)(T1
), T1 parameter1
)
151 : instance (instance
)
152 , parameter1 (parameter1
)
157 virtual void Execute() override
159 (instance
->*func
)(parameter1
);
163 template<class T1
, class T2
>
164 class CCall2
: public ICall
170 void (*func
)(T1
, T2
);
174 CCall2 (void (*func
)(T1
, T2
), T1 parameter1
, T2 parameter2
)
175 : parameter1 (parameter1
)
176 , parameter2 (parameter2
)
181 virtual void Execute() override
183 (*func
)(parameter1
, parameter2
);
187 template<class C
, class T1
, class T2
>
188 class CCallMember2
: public ICall
195 void (C::*func
)(T1
, T2
);
199 CCallMember2 (C
* instance
, void (C::*func
)(T1
, T2
), T1 parameter1
, T2 parameter2
)
200 : instance (instance
)
201 , parameter1 (parameter1
)
202 , parameter2 (parameter2
)
207 virtual void Execute() override
209 (instance
->*func
)(parameter1
, parameter2
);
213 // result and actual "future"
217 // actually execute the call
219 virtual void InternalExecute()
226 // construct futures for all sorts of callable items
228 CAsyncCall (void (*func
)(), CJobScheduler
* scheduler
= NULL
)
231 call
= new CCall0 (func
);
232 Schedule (true, scheduler
);
235 CAsyncCall (std::function
<void()> func
, CJobScheduler
* scheduler
= NULL
)
238 call
= new CCallStd (func
);
239 Schedule (true, scheduler
);
243 CAsyncCall (C
* instance
, void (C::*func
)(), CJobScheduler
* scheduler
= NULL
)
246 call
= new CCallMember0
<C
>(instance
, func
);
247 Schedule (true, scheduler
);
251 CAsyncCall (void (*func
)(T1
), T1 parameter1
, CJobScheduler
* scheduler
= NULL
)
254 call
= new CCall1
<T1
>(func
, parameter1
);
255 Schedule (true, scheduler
);
258 template<class C
, class T1
>
259 CAsyncCall (C
* instance
, void (C::*func
)(T1
), T1 parameter1
, CJobScheduler
* scheduler
= NULL
)
262 call
= new CCallMember1
<C
, T1
>(instance
, func
, parameter1
);
263 Schedule (true, scheduler
);
266 template<class T1
, class T2
>
267 CAsyncCall (void (*func
)(T1
,T2
), T1 parameter1
, T2 parameter2
, CJobScheduler
* scheduler
= NULL
)
270 call
= new CCall2
<T1
, T2
>(func
, parameter1
, parameter2
);
271 Schedule (true, scheduler
);
274 template<class C
, class T1
, class T2
>
275 CAsyncCall (C
* instance
, void (C::*func
)(T1
,T2
), T1 parameter1
, T2 parameter2
, CJobScheduler
* scheduler
= NULL
)
278 call
= new CCallMember2
<C
, T1
, T2
>(instance
, func
, parameter1
, parameter2
);
279 Schedule (true, scheduler
);