Alex Bikfalvi
SimWorker Documentation
SimWorkItem.h
00001 #pragma once 00002 00003 #ifdef WIN32 00004 #include <windows.h> 00005 #elif POSIX 00006 #include <pthread.h> 00007 #endif 00008 00009 class CSimWorkItem 00010 { 00011 public: 00012 enum EWorkItemState 00013 { 00014 PENDING = 0, 00015 COMPLETED_SUCCESS = 1, 00016 COMPLETED_FAIL = 2 00017 }; 00018 00019 protected: 00020 EWorkItemState state; 00021 00022 #ifdef WIN32 00023 HANDLE evt; 00024 #elif POSIX 00025 pthread_mutex_t mutex; 00026 pthread_cond_t cond; 00027 #endif 00028 00029 public: 00030 CSimWorkItem(); 00031 virtual ~CSimWorkItem(); 00032 00033 void Signal(EWorkItemState state); 00034 void Wait(); 00035 inline EWorkItemState State() { return this->state; } 00036 00037 virtual void Execute() = 0; 00038 }; 00039 00040 template<typename T, typename I, typename O> class CSimWorkItemImpl : public CSimWorkItem 00041 { 00042 private: 00043 T* object; 00044 void (T::*func)(I, O); 00045 I in; 00046 O out; 00047 00048 public: 00049 CSimWorkItemImpl(T* object, void (T::*func)(I, O)) 00050 { 00051 this->object = object; 00052 this->func = func; 00053 } 00054 CSimWorkItemImpl(T* object, void (T::*func)(I, O), I in, O out) 00055 { 00056 this->object = object; 00057 this->func = func; 00058 this->in = in; 00059 this->out = out; 00060 } 00061 virtual ~CSimWorkItemImpl() { } 00062 00063 void Set(I in, O out) 00064 { 00065 // Reset initial work item state 00066 this->state = PENDING; 00067 00068 // Set the parameters 00069 this->in = in; 00070 this->out = out; 00071 } 00072 00073 virtual void Execute() 00074 { 00075 (this->object->*this->func)(this->in, this->out); 00076 } 00077 };
Last updated: February 8, 2011