00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef TJHANDLER_H
00019 #define TJHANDLER_H
00020
00021 #include <tjutils/tjutils.h>
00022 #include <tjutils/tjthread.h>
00023
00030 class HandlerComponent {
00031 public:
00032 static const char* get_compName();
00033 };
00034
00035
00037
00038 template<class I> class Handled;
00039
00040
00041
00043
00044 template<class I>
00045 class Handler {
00046
00047
00048 public:
00049
00050 Handler();
00051
00052 Handler(const Handler& handler);
00053
00054 Handler& operator = (const Handler& handler);
00055
00056 ~Handler();
00057
00058 const Handler& clear_handledobj() const;
00059
00060 const Handler& set_handled(I handled) const;
00061
00062 I get_handled() const;
00063
00064
00065 private:
00066 friend class Handled<I>;
00067
00068 const Handler& handled_remove(Handled<I>* handled) const;
00069
00070 mutable I handledobj;
00071
00072 };
00073
00074
00076
00077
00078 template<class I>
00079 class Handled {
00080
00081 public:
00082 Handled();
00083 ~Handled();
00084
00085 bool is_handled() const {return bool(handlers.size());}
00086
00087 private:
00088 friend class Handler<I>;
00089
00090 const Handled& set_handler(const Handler<I>& handler) const;
00091 const Handled& erase_handler(const Handler<I>& handler) const;
00092
00093 mutable STD_list< const Handler<I>* > handlers;
00094 };
00095
00096
00100
00101 class SingletonBase;
00102
00103
00104 typedef STD_map<STD_string, SingletonBase*> SingletonMap;
00105
00106
00107 class SingletonBase {
00108
00109 public:
00110 virtual void* get_ptr() const = 0;
00111
00112 static SingletonMap* get_singleton_map();
00113 static void set_singleton_map_external(SingletonMap* extmap);
00114
00115 protected:
00116 SingletonBase();
00117 virtual ~SingletonBase() {}
00118
00119 static STD_string get_singleton_label(SingletonBase* sing_ptr);
00120 static void* get_external_map_ptr(const STD_string& sing_label);
00121
00122 static SingletonMap* singleton_map;
00123 static SingletonMap* singleton_map_external;
00124 };
00125
00126
00128
00129
00133 template<class T>
00134 class LockProxy {
00135
00136 public:
00137 LockProxy(volatile T* r, Mutex* m) : presource(const_cast<T*>(r)), pmutex(m) {if(pmutex) pmutex->lock();}
00138 ~LockProxy() {if(pmutex) pmutex->unlock();}
00139 T* operator -> () {return presource;}
00140
00141 private:
00142 T* presource;
00143 Mutex* pmutex;
00144 };
00145
00146
00148
00149
00150
00165 template<class T, bool thread_safe>
00166 class SingletonHandler : public SingletonBase {
00167
00168 public:
00169
00170 SingletonHandler() {
00171
00172 }
00173
00174
00180 void init (const char* unique_label);
00181
00185 void destroy ();
00186
00187
00191 void copy(T& destination) const;
00192
00196 T* unlocked_ptr() {return get_map_ptr();}
00197
00198
00199
00200
00204 LockProxy<T> operator -> () {return LockProxy<T>(get_map_ptr(),mutex);}
00205
00209 const T* operator -> () const {return get_map_ptr();}
00210
00214 operator bool () const {return (bool)get_map_ptr();}
00215
00216
00217
00218 private:
00219
00220
00221 void* get_ptr() const {return ptr;}
00222
00223 T* get_map_ptr() const;
00224
00225
00226 mutable T* ptr;
00227 STD_string* singleton_label;
00228 Mutex* mutex;
00229
00230 };
00231
00232
00233
00236 #endif