Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members

mitkThread.h

00001 /*=========================================================================
00002 
00003   Program:   3DMed
00004   Date:      $Date: 2014-02-25 18:30:00 +0800 $
00005   Version:   $Version: 4.6.0 $
00006   Copyright: MIPG, Institute of Automation, Chinese Academy of Sciences
00007 
00008 =========================================================================*/
00009 
00010 #ifndef __mitkThread_class
00011 #define __mitkThread_class
00012 
00013 #include "mitkObject.h"
00014 
00015 class mitkThread;
00016 // an abstract class defining the interface of a thread implementor
00017 class mitkThreadImp
00018 {
00019 public:
00020     mitkThreadImp(mitkThread *thread,void (mitkThread::*run)());
00021     virtual ~mitkThreadImp();
00022     void Spawn()
00023     {
00024         _wait();
00025         _create();
00026     }
00027     void Wait()
00028     {
00029         _wait();    
00030     }
00031     struct ThreadEntrance
00032     {
00033         mitkThread *thread;
00034         void (mitkThread::*run)();
00035     };      
00036 protected:
00037     virtual void _create()=0;
00038     virtual void _wait()=0;
00039     ThreadEntrance *GetThreadEntrance()
00040     {
00041         return &m_ThreadEntrance;
00042     }
00043 private:
00044     ThreadEntrance m_ThreadEntrance;
00045 
00046 };
00047 
00051 class MITK_COMMON_API mitkThread: public mitkObject
00052 {
00053 public:
00054     MITK_TYPE(mitkThread,mitkObject)
00058     void Spawn()
00059     {
00060         m_Imp->Spawn();
00061     }
00063     // Wait for the thread to terminate
00065     void Wait()
00066     {
00067         m_Imp->Wait();
00068     }
00069 protected:
00070     mitkThread();
00071     virtual ~mitkThread();  
00072     virtual void Run()=0;
00073 private:
00074     mitkThread(const mitkThread&);
00075     void operator = (const mitkThread&);
00076     mitkThreadImp *m_Imp;
00077 };
00078 
00084 class MITK_COMMON_API mitkThreadDelegate: public mitkThread
00085 {
00086 public:
00087     MITK_TYPE(mitkThreadDelegate,mitkThread)
00088     void SetParameter(void *param)
00089     {
00090         m_Param=param;
00091     }
00092 protected:
00093     mitkThreadDelegate(){m_Param=NULL;}
00094     virtual ~mitkThreadDelegate(){}
00095     void *GetParameter()
00096     {
00097         return m_Param;
00098     }
00099 private:
00100     mitkThreadDelegate(const mitkThreadDelegate&);
00101     void operator = (const mitkThreadDelegate&);
00102     void *m_Param;
00103 };
00104 
00112 class mitkThreadStatic: public mitkThreadDelegate
00113 {
00114 public:
00115     mitkThreadStatic(void (*function)(void*))
00116     {
00117         m_Function=function;
00118     }
00119 protected:
00120     virtual ~mitkThreadStatic(){}
00121     virtual void Run()
00122     {
00123         m_Function(GetParameter());
00124     }
00125 private:
00126     mitkThreadStatic(const mitkThreadStatic&);
00127     void operator=(const mitkThreadStatic&);
00128     void (*m_Function)(void *);
00129 };
00130 
00138 template<class T>
00139 class mitkThreadMember : public mitkThreadDelegate
00140 {
00141 public:
00142     mitkThreadMember(T* object, void (T::*MemberFunction)(void *))
00143     {
00144         m_Object=object;
00145         m_MemberFunction=MemberFunction;
00146     }
00147 protected:
00148     virtual ~mitkThreadMember(){}
00149     virtual void Run()
00150     {
00151         (m_Object->*m_MemberFunction)(GetParameter());
00152     }
00153 private:
00154     mitkThreadMember(const mitkThreadMember&);
00155     void operator = (const mitkThreadMember&);
00156     T *m_Object;
00157     void (T::*m_MemberFunction)(void *);
00158 };
00159 
00160 #define MITK_THREAD_MEMBER(Class,ObjectPointer,MemberFunction)\
00161 mitkThreadMember<Class>(ObjectPointer,&Class::MemberFunction)
00162 
00163 #endif
00164 

Generated on Tue Feb 25 15:00:37 2014 for MITK (Medical Imaging ToolKit) by  doxygen 1.4.3