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 asynchronuously in the background.
30 * Return the result in \ref GetResult. The latter will wait for
31 * the call to finished, if necessary.
33 * Accepts a pointer to some free function or member function plus
34 * parameters and schedules it for execution it in the given job scheduler.
35 * If no scheduler is given, the default scheduler is used (see
36 * \ref CJobScheduler::GetDefault for details).
38 * Please note that the parameters must remain valid until the call
39 * actually got executed. Therefore, only pointer and value parameter
40 * are possible; reference parameters won't compile.
48 class CFuture0
: public CJobBase
57 virtual void InternalExecute() override
64 CFuture0 (R (*func
)(), R
& result
, CJobScheduler
* scheduler
)
68 Schedule (false, scheduler
);
73 class CFutureMember0
: public CJobBase
83 virtual void InternalExecute() override
85 result
= (instance
->*func
)();
90 CFutureMember0 (C
* instance
, R (C::*func
)(), R
& result
, CJobScheduler
* scheduler
)
95 Schedule (false, scheduler
);
100 class CFuture1
: public CJobBase
110 virtual void InternalExecute() override
112 result
= (*func
)(parameter1
);
117 CFuture1 (R (*func
)(T1
), R
& result
, T1 parameter1
, CJobScheduler
* scheduler
)
118 : parameter1 (parameter1
)
122 Schedule (false, scheduler
);
126 template<class C
, class T1
>
127 class CFutureMember1
: public CJobBase
138 virtual void InternalExecute() override
140 result
= (instance
->*func
)(parameter1
);
145 CFutureMember1 (C
* instance
, R (C::*func
)(T1
), R
& result
, T1 parameter1
, CJobScheduler
* scheduler
)
146 : instance (instance
)
147 , parameter1 (parameter1
)
151 Schedule (false, scheduler
);
155 template<class T1
, class T2
>
156 class CFuture2
: public CJobBase
167 virtual void InternalExecute() override
169 result
= (*func
)(parameter1
, parameter2
);
174 CFuture2 (R (*func
)(T1
, T2
), R
& result
, T1 parameter1
, T2 parameter2
, CJobScheduler
* scheduler
)
175 : parameter1 (parameter1
)
176 , parameter2 (parameter2
)
180 Schedule (false, scheduler
);
184 template<class C
, class T1
, class T2
>
185 class CFutureMember2
: public CJobBase
192 R (C::*func
)(T1
, T2
);
197 virtual void InternalExecute() override
199 result
= (instance
->*func
)(parameter1
, parameter2
);
204 CFutureMember2 (C
* instance
, R (C::*func
)(T1
, T2
), R
& result
, T1 parameter1
, T2 parameter2
, CJobScheduler
* scheduler
)
205 : instance (instance
)
206 , parameter1 (parameter1
)
207 , parameter2 (parameter2
)
211 Schedule (false, scheduler
);
215 template<class T1
, class T2
, class T3
>
216 class CFuture3
: public CJobBase
223 R (*func
)(T1
, T2
, T3
);
228 virtual void InternalExecute() override
230 result
= (*func
)(parameter1
, parameter2
, parameter3
);
235 CFuture3 (R (*func
)(T1
, T2
, T3
), R
& result
, T1 parameter1
, T2 parameter2
, T3 parameter3
, CJobScheduler
* scheduler
)
236 : parameter1 (parameter1
)
237 , parameter2 (parameter2
)
238 , parameter3 (parameter3
)
242 Schedule (false, scheduler
);
246 template<class C
, class T1
, class T2
, class T3
>
247 class CFutureMember3
: public CJobBase
255 R (C::*func
)(T1
, T2
, T3
);
260 virtual void InternalExecute() override
262 result
= (instance
->*func
)(parameter1
, parameter2
, parameter3
);
267 CFutureMember3 (C
* instance
, R (C::*func
)(T1
, T2
, T3
), R
& result
, T1 parameter1
, T2 parameter2
, T3 parameter3
, CJobScheduler
* scheduler
)
268 : instance (instance
)
269 , parameter1 (parameter1
)
270 , parameter2 (parameter2
)
271 , parameter3 (parameter3
)
275 Schedule (false, scheduler
);
279 // result and actual "future"
286 // construct futures for all sorts of callable items
288 CFuture (R (*func
)(), CJobScheduler
* scheduler
= NULL
)
291 job
= new CFuture0(func
, result
, scheduler
);
295 CFuture (C
* instance
, R (C::*func
)(), CJobScheduler
* scheduler
= NULL
)
298 job
= new CFutureMember0
<C
>(instance
, func
, result
, scheduler
);
302 CFuture (R (*func
)(T1
), T1 parameter1
, CJobScheduler
* scheduler
= NULL
)
305 job
= new CFuture1
<T1
>(func
, result
, parameter1
, scheduler
);
308 template<class C
, class T1
>
309 CFuture (C
* instance
, R (C::*func
)(T1
), T1 parameter1
, CJobScheduler
* scheduler
= NULL
)
312 job
= new CFutureMember1
<C
, T1
>(instance
, func
, result
, parameter1
, scheduler
);
315 template<class T1
, class T2
>
316 CFuture (R (*func
)(T1
, T2
), T1 parameter1
, T2 parameter2
, CJobScheduler
* scheduler
= NULL
)
319 job
= new CFuture2
<T1
, T2
>
320 (func
, result
, parameter1
, parameter2
, scheduler
);
323 template<class C
, class T1
, class T2
>
324 CFuture (C
* instance
, R (C::*func
)(T1
, T2
), T1 parameter1
, T2 parameter2
, CJobScheduler
* scheduler
= NULL
)
327 job
= new CFutureMember2
<C
, T1
, T2
>
328 (instance
, func
, result
, parameter1
, parameter2
, scheduler
);
331 template<class T1
, class T2
, class T3
>
332 CFuture (R (*func
)(T1
, T2
, T3
), T1 parameter1
, T2 parameter2
, T3 parameter3
, CJobScheduler
* scheduler
= NULL
)
335 job
= new CFuture3
<T1
, T2
, T3
>
336 (func
, result
, parameter1
, parameter2
, parameter3
, scheduler
);
339 template<class C
, class T1
, class T2
, class T3
>
340 CFuture (C
* instance
, R (C::*func
)(T1
, T2
, T3
), T1 parameter1
, T2 parameter2
, T3 parameter3
, CJobScheduler
* scheduler
= NULL
)
343 job
= new CFutureMember3
<C
, T1
, T2
, T3
>
344 (instance
, func
, result
, parameter1
, parameter2
, parameter3
, scheduler
);
354 // ensure the function has returned and pass the result back to the caller
356 const R
& GetResult() const
358 job
->WaitUntilDone();
364 return job
->GetStatus() == IJob::done
;