runnable.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00031 
00032 
00033 #pragma once
00034 
00035 
00036 #include "../api_core.h"
00037 #include <typeinfo>
00038 
00042 class CL_API_CORE CL_Runnable
00043 {
00046 
00047 public:
00048         CL_Runnable();
00049 
00050         virtual ~CL_Runnable();
00051 
00052 
00056 
00057 public:
00058 
00059 
00063 
00064 public:
00065         virtual void run() = 0;
00066 
00068 
00069         static void set_thread_name(const char *name);
00070 
00071 
00075 
00076 private:
00078 };
00079 
00082 template<class C>
00083 class CL_RunnableMember_v0 : public CL_Runnable
00084 {
00087 
00088 public:
00089         typedef void (C::*PtrMemberFunc)();
00090 
00091         CL_RunnableMember_v0(C *instance, PtrMemberFunc func)
00092         : instance(instance), func(func)
00093         {
00094         }
00095 
00096 
00100 
00101 public:
00102         virtual void run()
00103         {
00104                 set_thread_name(typeid(func).name());
00105                 C *local_instance = instance;
00106                 PtrMemberFunc local_func = func;
00107                 delete this;
00108                 (local_instance->*local_func)();
00109         }
00110 
00111 
00115 
00116 private:
00117         C *instance;
00118 
00119         PtrMemberFunc func;
00121 };
00122 
00125 template<class C, class P1>
00126 class CL_RunnableMember_v1 : public CL_Runnable
00127 {
00130 
00131 public:
00132         typedef void (C::*PtrMemberFunc)(P1 p1);
00133 
00134         CL_RunnableMember_v1(C *instance, PtrMemberFunc func, P1 p1)
00135         : instance(instance), func(func), p1(p1)
00136         {
00137         }
00138 
00139 
00143 
00144 public:
00145         virtual void run()
00146         {
00147                 set_thread_name(typeid(func).name());
00148                 C *local_instance = instance;
00149                 PtrMemberFunc local_func = func;
00150                 P1 local_p1 = p1;
00151                 delete this;
00152                 (local_instance->*local_func)(local_p1);
00153         }
00154 
00155 
00159 
00160 private:
00161         C *instance;
00162 
00163         PtrMemberFunc func;
00164 
00165         P1 p1;
00167 };
00168 
00171 template<class C, class P1, class P2>
00172 class CL_RunnableMember_v2 : public CL_Runnable
00173 {
00176 
00177 public:
00178         typedef void (C::*PtrMemberFunc)(P1 p1, P2 p2);
00179 
00180         CL_RunnableMember_v2(C *instance, PtrMemberFunc func, P1 p1, P2 p2)
00181         : instance(instance), func(func), p1(p1), p2(p2)
00182         {
00183         }
00184 
00185 
00189 
00190 public:
00191         virtual void run()
00192         {
00193                 set_thread_name(typeid(func).name());
00194                 C *local_instance = instance;
00195                 PtrMemberFunc local_func = func;
00196                 P1 local_p1 = p1;
00197                 P2 local_p2 = p2;
00198                 delete this;
00199                 (local_instance->*local_func)(local_p1, local_p2);
00200         }
00201 
00202 
00206 
00207 private:
00208         C *instance;
00209 
00210         PtrMemberFunc func;
00211 
00212         P1 p1;
00213 
00214         P2 p2;
00216 };
00217 
00220 template<class C, class P1, class P2, class P3>
00221 class CL_RunnableMember_v3 : public CL_Runnable
00222 {
00225 
00226 public:
00227         typedef void (C::*PtrMemberFunc)(P1 p1, P2 p2, P3 p3);
00228 
00229         CL_RunnableMember_v3(C *instance, PtrMemberFunc func, P1 p1, P2 p2, P3 p3)
00230         : instance(instance), func(func), p1(p1), p2(p2), p3(p3)
00231         {
00232         }
00233 
00234 
00238 
00239 public:
00240         virtual void run()
00241         {
00242                 set_thread_name(typeid(func).name());
00243                 C *local_instance = instance;
00244                 PtrMemberFunc local_func = func;
00245                 P1 local_p1 = p1;
00246                 P2 local_p2 = p2;
00247                 P3 local_p3 = p3;
00248                 delete this;
00249                 (local_instance->*local_func)(local_p1, local_p2, local_p3);
00250         }
00251 
00252 
00256 
00257 private:
00258         C *instance;
00259 
00260         PtrMemberFunc func;
00261 
00262         P1 p1;
00263 
00264         P2 p2;
00265 
00266         P3 p3;
00268 };
00269 
00272 template<class C, class P1, class P2, class P3, class P4>
00273 class CL_RunnableMember_v4 : public CL_Runnable
00274 {
00277 
00278 public:
00279         typedef void (C::*PtrMemberFunc)(P1 p1, P2 p2, P3 p3, P4 p4);
00280 
00281         CL_RunnableMember_v4(C *instance, PtrMemberFunc func, P1 p1, P2 p2, P3 p3, P4 p4)
00282         : instance(instance), func(func), p1(p1), p2(p2), p3(p3), p4(p4)
00283         {
00284         }
00285 
00286 
00290 
00291 public:
00292         virtual void run()
00293         {
00294                 set_thread_name(typeid(func).name());
00295                 C *local_instance = instance;
00296                 PtrMemberFunc local_func = func;
00297                 P1 local_p1 = p1;
00298                 P2 local_p2 = p2;
00299                 P3 local_p3 = p3;
00300                 P4 local_p4 = p4;
00301                 delete this;
00302                 (local_instance->*local_func)(local_p1, local_p2, local_p3, local_p4);
00303         }
00304 
00305 
00309 
00310 private:
00311         C *instance;
00312 
00313         PtrMemberFunc func;
00314 
00315         P1 p1;
00316 
00317         P2 p2;
00318 
00319         P3 p3;
00320 
00321         P4 p4;
00323 };
00324 
00327 template<class C, class P1, class P2, class P3, class P4, class P5>
00328 class CL_RunnableMember_v5 : public CL_Runnable
00329 {
00332 
00333 public:
00334         typedef void (C::*PtrMemberFunc)(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5);
00335 
00336         CL_RunnableMember_v5(C *instance, PtrMemberFunc func, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
00337         : instance(instance), func(func), p1(p1), p2(p2), p3(p3), p4(p4), p5(p5)
00338         {
00339         }
00340 
00341 
00345 
00346 public:
00347         virtual void run()
00348         {
00349                 set_thread_name(typeid(func).name());
00350                 C *local_instance = instance;
00351                 PtrMemberFunc local_func = func;
00352                 P1 local_p1 = p1;
00353                 P2 local_p2 = p2;
00354                 P3 local_p3 = p3;
00355                 P4 local_p4 = p4;
00356                 P5 local_p5 = p5;
00357                 delete this;
00358                 (local_instance->*local_func)(local_p1, local_p2, local_p3, local_p4, local_p5);
00359         }
00360 
00361 
00365 
00366 private:
00367         C *instance;
00368 
00369         PtrMemberFunc func;
00370 
00371         P1 p1;
00372 
00373         P2 p2;
00374 
00375         P3 p3;
00376 
00377         P4 p4;
00378 
00379         P5 p5;
00381 };
00382 
00383