00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _SB_THREAD_STORAGE_H_
00027 #define _SB_THREAD_STORAGE_H_
00028
00029 #include <Inventor/SoInventorBase.h>
00030
00031 #include <Inventor/threads/SbThreadStorageBase.h>
00032 #include <Inventor/STL/functional>
00033
00040 template <class T>
00041 class SbThreadStorage : public SbThreadStorageBase
00042 {
00043 public:
00047 SbThreadStorage<T>() { }
00048
00049
00050
00051
00052 SbThreadStorage<T>( const T t )
00053 {
00054 Register( t );
00055 }
00056
00063 virtual ~SbThreadStorage()
00064 { erase(); }
00065
00071 SbThreadStorage<T>& operator=(T t)
00072 {
00073 Register( t );
00074 return *this;
00075 }
00076
00082 T operator->() const
00083 {
00084 #if defined(_DEBUG)
00085 T t = static_cast<T>(getValue());
00086 if ( t == NULL && SoInventorBase::checkMultithread() )
00087 fprintf( stderr, "MT ERROR: in ::operator-> , object not initialized for current thread." );
00088 return t;
00089 #else
00090 return static_cast<T>(getValue());
00091 #endif
00092 }
00093
00098 T operator*() const
00099 {
00100 return static_cast<T>(getValue());
00101 }
00102
00110 operator bool() const
00111 {
00112 return ((static_cast<T>(getValue())) != NULL);
00113 }
00114
00116 template< typename Func>
00117 void call( Func f )
00118 {
00119 s_threadStorageGlobalMutex.lock();
00120 SbThreadStorageGlobalList::iterator it_i = s_threadStorageGlobalList.begin();
00121 while(it_i != s_threadStorageGlobalList.end())
00122 {
00123
00124 void* value=get(it_i);
00125 if (value!=NULL)
00126 std::mem_fn(f)(static_cast<T>(value));
00127 ++it_i;
00128 }
00129 s_threadStorageGlobalMutex.unlock();
00130 }
00131
00133 template< typename Func, typename Param>
00134 void call( Func f, Param p )
00135 {
00136 s_threadStorageGlobalMutex.lock();
00137 SbThreadStorageGlobalList::iterator it_i = s_threadStorageGlobalList.begin();
00138 while(it_i != s_threadStorageGlobalList.end())
00139 {
00140
00141 void* value=get(it_i);
00142 if (value!=NULL)
00143 std::bind(std::mem_fn((Func)(f)), std::placeholders::_1, p)(static_cast<T>(value));
00144 ++it_i;
00145 }
00146 s_threadStorageGlobalMutex.unlock();
00147 }
00148
00149
00150 private:
00155 virtual void deleteStorage(void* p)
00156 {
00157 T val = (static_cast<T>(p));
00158 if (val)
00159 val->unref();
00160 }
00161
00162 private:
00163
00164 void* operator new[] (size_t) throw() { return 0; }
00165 void operator delete[] (void*) { }
00166 void* operator new( size_t, char ) throw() { return 0; }
00167 };
00168
00174 template <>
00175 class SbThreadStorage<bool> : public SbThreadStorageBase
00176 {
00177 public:
00179 virtual ~SbThreadStorage<bool>()
00180 { erase(); }
00181
00186 SbThreadStorage<bool>& operator <<=(bool t)
00187 {
00188 void* pt = reinterpret_cast<void*>(static_cast<uintptr_t>(t));
00189 Register( pt );
00190 return (*this);
00191 }
00192
00197 SbThreadStorage<bool>& operator >>=(bool t)
00198 {
00199 void *pt = reinterpret_cast<void*>(static_cast<uintptr_t>(t));
00200 Register( pt );
00201 setAll(pt);
00202 return *this;
00203 }
00204
00208 operator bool() const
00209 { return ((static_cast<bool>(getValue())) == true); }
00210
00211 private:
00213 virtual void deleteStorage(void*)
00214 {}
00215
00216 private:
00217
00218 void* operator new[] (size_t) throw() { return 0; }
00219 void operator delete[] (void*) { }
00220 void* operator new( size_t , char ) throw() { return 0; }
00221 SbThreadStorage<bool>& operator=(bool) { return *this; }
00222 };
00223
00224 #endif //_SB_THREAD_STORAGE_H_
00225
00226
00227
00228
00229