ClanLib  2.3.7
runnable.h
Go to the documentation of this file.
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2011 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
31 
32 
33 #pragma once
34 
35 
36 #include "../api_core.h"
37 #include <typeinfo>
38 
43 {
46 
47 public:
48  CL_Runnable();
49 
50  virtual ~CL_Runnable();
51 
52 
56 
57 public:
58 
59 
63 
64 public:
65  virtual void run() = 0;
66 
68 
69  static void set_thread_name(const char *name);
70 
71 
75 
76 private:
78 };
79 
82 template<class C>
84 {
87 
88 public:
89  typedef void (C::*PtrMemberFunc)();
90 
92  : instance(instance), func(func)
93  {
94  }
95 
96 
100 
101 public:
102  virtual void run()
103  {
104  set_thread_name(typeid(func).name());
105  C *local_instance = instance;
106  PtrMemberFunc local_func = func;
107  delete this;
108  (local_instance->*local_func)();
109  }
110 
111 
115 
116 private:
117  C *instance;
118 
119  PtrMemberFunc func;
121 };
122 
125 template<class C, class P1>
127 {
130 
131 public:
132  typedef void (C::*PtrMemberFunc)(P1 p1);
133 
134  CL_RunnableMember_v1(C *instance, PtrMemberFunc func, P1 p1)
135  : instance(instance), func(func), p1(p1)
136  {
137  }
138 
139 
143 
144 public:
145  virtual void run()
146  {
147  set_thread_name(typeid(func).name());
148  C *local_instance = instance;
149  PtrMemberFunc local_func = func;
150  P1 local_p1 = p1;
151  delete this;
152  (local_instance->*local_func)(local_p1);
153  }
154 
155 
159 
160 private:
161  C *instance;
162 
163  PtrMemberFunc func;
164 
165  P1 p1;
167 };
168 
171 template<class C, class P1, class P2>
173 {
176 
177 public:
178  typedef void (C::*PtrMemberFunc)(P1 p1, P2 p2);
179 
180  CL_RunnableMember_v2(C *instance, PtrMemberFunc func, P1 p1, P2 p2)
181  : instance(instance), func(func), p1(p1), p2(p2)
182  {
183  }
184 
185 
189 
190 public:
191  virtual void run()
192  {
193  set_thread_name(typeid(func).name());
194  C *local_instance = instance;
195  PtrMemberFunc local_func = func;
196  P1 local_p1 = p1;
197  P2 local_p2 = p2;
198  delete this;
199  (local_instance->*local_func)(local_p1, local_p2);
200  }
201 
202 
206 
207 private:
208  C *instance;
209 
210  PtrMemberFunc func;
211 
212  P1 p1;
213 
214  P2 p2;
216 };
217 
220 template<class C, class P1, class P2, class P3>
222 {
225 
226 public:
227  typedef void (C::*PtrMemberFunc)(P1 p1, P2 p2, P3 p3);
228 
229  CL_RunnableMember_v3(C *instance, PtrMemberFunc func, P1 p1, P2 p2, P3 p3)
230  : instance(instance), func(func), p1(p1), p2(p2), p3(p3)
231  {
232  }
233 
234 
238 
239 public:
240  virtual void run()
241  {
242  set_thread_name(typeid(func).name());
243  C *local_instance = instance;
244  PtrMemberFunc local_func = func;
245  P1 local_p1 = p1;
246  P2 local_p2 = p2;
247  P3 local_p3 = p3;
248  delete this;
249  (local_instance->*local_func)(local_p1, local_p2, local_p3);
250  }
251 
252 
256 
257 private:
258  C *instance;
259 
260  PtrMemberFunc func;
261 
262  P1 p1;
263 
264  P2 p2;
265 
266  P3 p3;
268 };
269 
272 template<class C, class P1, class P2, class P3, class P4>
274 {
277 
278 public:
279  typedef void (C::*PtrMemberFunc)(P1 p1, P2 p2, P3 p3, P4 p4);
280 
281  CL_RunnableMember_v4(C *instance, PtrMemberFunc func, P1 p1, P2 p2, P3 p3, P4 p4)
282  : instance(instance), func(func), p1(p1), p2(p2), p3(p3), p4(p4)
283  {
284  }
285 
286 
290 
291 public:
292  virtual void run()
293  {
294  set_thread_name(typeid(func).name());
295  C *local_instance = instance;
296  PtrMemberFunc local_func = func;
297  P1 local_p1 = p1;
298  P2 local_p2 = p2;
299  P3 local_p3 = p3;
300  P4 local_p4 = p4;
301  delete this;
302  (local_instance->*local_func)(local_p1, local_p2, local_p3, local_p4);
303  }
304 
305 
309 
310 private:
311  C *instance;
312 
313  PtrMemberFunc func;
314 
315  P1 p1;
316 
317  P2 p2;
318 
319  P3 p3;
320 
321  P4 p4;
323 };
324 
327 template<class C, class P1, class P2, class P3, class P4, class P5>
329 {
332 
333 public:
334  typedef void (C::*PtrMemberFunc)(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5);
335 
336  CL_RunnableMember_v5(C *instance, PtrMemberFunc func, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
337  : instance(instance), func(func), p1(p1), p2(p2), p3(p3), p4(p4), p5(p5)
338  {
339  }
340 
341 
345 
346 public:
347  virtual void run()
348  {
349  set_thread_name(typeid(func).name());
350  C *local_instance = instance;
351  PtrMemberFunc local_func = func;
352  P1 local_p1 = p1;
353  P2 local_p2 = p2;
354  P3 local_p3 = p3;
355  P4 local_p4 = p4;
356  P5 local_p5 = p5;
357  delete this;
358  (local_instance->*local_func)(local_p1, local_p2, local_p3, local_p4, local_p5);
359  }
360 
361 
365 
366 private:
367  C *instance;
368 
369  PtrMemberFunc func;
370 
371  P1 p1;
372 
373  P2 p2;
374 
375  P3 p3;
376 
377  P4 p4;
378 
379  P5 p5;
381 };
382 
383