Fixed issue #1642: Incorrect behavior if repo is located on root of drive
[TortoiseGit.git] / src / AsyncFramework / AsyncCall.h
blob7826c41ba2d83b6035d4ed27dbf6c96695a8b31a
1 /***************************************************************************
2 * Copyright (C) 2009, 2012 by Stefan Fuhrmann *
3 * stefanfuhrmann@alice-dsl.de *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
21 #pragma once
23 #include "JobBase.h"
24 #include <functional>
25 namespace async
28 /**
29 * Execute a call to a funtion returning @a void asynchronuously
30 * in the background.
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
47 private:
49 class ICall
51 public:
53 virtual ~ICall() {};
54 virtual void Execute() = 0;
57 class CCallStd : public ICall
59 private:
61 std::function<void()> func;
63 public:
65 CCallStd (std::function<void()> f)
66 : func (f)
70 virtual void Execute() override
72 func();
76 class CCall0 : public ICall
78 private:
80 void (*func)();
82 public:
84 CCall0 (void (*func)())
85 : func (func)
89 virtual void Execute() override
91 (*func)();
95 template<class C>
96 class CCallMember0 : public ICall
98 private:
100 C* instance;
101 void (C::*func)();
103 public:
105 CCallMember0 (C* instance, void (C::*func)())
106 : instance (instance)
107 , func (func)
111 virtual void Execute() override
113 (instance->*func)();
117 template<class T1>
118 class CCall1 : public ICall
120 private:
122 T1 parameter1;
123 void (*func)(T1);
125 public:
127 CCall1 (void (*func)(T1), T1 parameter1)
128 : parameter1 (parameter1)
129 , func (func)
133 virtual void Execute() override
135 (*func)(parameter1);
139 template<class C, class T1>
140 class CCallMember1 : public ICall
142 private:
144 C* instance;
145 T1 parameter1;
146 void (C::*func)(T1);
148 public:
150 CCallMember1 (C* instance, void (C::*func)(T1), T1 parameter1)
151 : instance (instance)
152 , parameter1 (parameter1)
153 , func (func)
157 virtual void Execute() override
159 (instance->*func)(parameter1);
163 template<class T1, class T2>
164 class CCall2 : public ICall
166 private:
168 T1 parameter1;
169 T2 parameter2;
170 void (*func)(T1, T2);
172 public:
174 CCall2 (void (*func)(T1, T2), T1 parameter1, T2 parameter2)
175 : parameter1 (parameter1)
176 , parameter2 (parameter2)
177 , func (func)
181 virtual void Execute() override
183 (*func)(parameter1, parameter2);
187 template<class C, class T1, class T2>
188 class CCallMember2 : public ICall
190 private:
192 C* instance;
193 T1 parameter1;
194 T2 parameter2;
195 void (C::*func)(T1, T2);
197 public:
199 CCallMember2 (C* instance, void (C::*func)(T1, T2), T1 parameter1, T2 parameter2)
200 : instance (instance)
201 , parameter1 (parameter1)
202 , parameter2 (parameter2)
203 , func (func)
207 virtual void Execute() override
209 (instance->*func)(parameter1, parameter2);
213 // result and actual "future"
215 ICall* call;
217 // actually execute the call
219 virtual void InternalExecute()
221 call->Execute();
224 public:
226 // construct futures for all sorts of callable items
228 CAsyncCall (void (*func)(), CJobScheduler* scheduler = NULL)
229 : call (NULL)
231 call = new CCall0 (func);
232 Schedule (true, scheduler);
235 CAsyncCall (std::function<void()> func, CJobScheduler* scheduler = NULL)
236 : call (NULL)
238 call = new CCallStd (func);
239 Schedule (true, scheduler);
242 template<class C>
243 CAsyncCall (C* instance, void (C::*func)(), CJobScheduler* scheduler = NULL)
244 : call (NULL)
246 call = new CCallMember0<C>(instance, func);
247 Schedule (true, scheduler);
250 template<class T1>
251 CAsyncCall (void (*func)(T1), T1 parameter1, CJobScheduler* scheduler = NULL)
252 : call (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)
260 : call (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)
268 : call (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)
276 : call (NULL)
278 call = new CCallMember2<C, T1, T2>(instance, func, parameter1, parameter2);
279 Schedule (true, scheduler);
282 // cleanup
284 ~CAsyncCall()
286 delete call;