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
00041 template <class T>
00042 class SbThreadStorage : public SbThreadStorageBase
00043 {
00044 public:
00048 SbThreadStorage<T>() { }
00049
00050
00051
00052
00053 SbThreadStorage<T>( const T t )
00054 {
00055 Register( t );
00056 }
00057
00064 virtual ~SbThreadStorage()
00065 { erase(); }
00066
00072 SbThreadStorage<T>& operator=(T t)
00073 {
00074 Register( t );
00075 return *this;
00076 }
00077
00083 T operator->() const
00084 {
00085 #if defined(_DEBUG)
00086 T t = static_cast<T>(getValue());
00087 if ( t == NULL && SoInventorBase::checkMultithread() )
00088 fprintf( stderr, "MT ERROR: in ::operator-> , object not initialized for current thread." );
00089 return t;
00090 #else
00091 return static_cast<T>(getValue());
00092 #endif
00093 }
00094
00099 T operator*() const
00100 {
00101 return static_cast<T>(getValue());
00102 }
00103
00111 operator bool() const
00112 {
00113 return ((static_cast<T>(getValue())) != NULL);
00114 }
00115
00117 template< typename Func>
00118 void call( Func f )
00119 {
00120 s_threadStorageGlobalMutex.lock();
00121 SbThreadStorageGlobalList::iterator it_i = s_threadStorageGlobalList.begin();
00122 while(it_i != s_threadStorageGlobalList.end())
00123 {
00124
00125 void* value=get(it_i);
00126 if (value!=NULL)
00127 std::mem_fn(f)(static_cast<T>(value));
00128 ++it_i;
00129 }
00130 s_threadStorageGlobalMutex.unlock();
00131 }
00132
00134 template< typename Func, typename Param>
00135 void call( Func f, Param p )
00136 {
00137 s_threadStorageGlobalMutex.lock();
00138 SbThreadStorageGlobalList::iterator it_i = s_threadStorageGlobalList.begin();
00139 while(it_i != s_threadStorageGlobalList.end())
00140 {
00141
00142 void* value=get(it_i);
00143 if (value!=NULL)
00144 std::bind(std::mem_fn((Func)(f)), std::placeholders::_1, p)(static_cast<T>(value));
00145 ++it_i;
00146 }
00147 s_threadStorageGlobalMutex.unlock();
00148 }
00149
00150
00151 private:
00156 virtual void deleteStorage(void* p)
00157 {
00158 T val = (static_cast<T>(p));
00159 if (val)
00160 val->unref();
00161 }
00162
00163 private:
00164
00165 void* operator new[] (size_t) throw() { return 0; }
00166 void operator delete[] (void*) { }
00167 void* operator new( size_t, char ) throw() { return 0; }
00168 };
00169
00176 template <>
00177 class SbThreadStorage<bool> : public SbThreadStorageBase
00178 {
00179 public:
00181 virtual ~SbThreadStorage<bool>()
00182 { erase(); }
00183
00188 SbThreadStorage<bool>& operator <<=(bool t)
00189 {
00190 void* pt = reinterpret_cast<void*>(static_cast<uintptr_t>(t));
00191 Register( pt );
00192 return (*this);
00193 }
00194
00199 SbThreadStorage<bool>& operator >>=(bool t)
00200 {
00201 void *pt = reinterpret_cast<void*>(static_cast<uintptr_t>(t));
00202 Register( pt );
00203 setAll(pt);
00204 return *this;
00205 }
00206
00210 operator bool() const
00211 { return ((static_cast<bool>(getValue())) == true); }
00212
00213 private:
00215 virtual void deleteStorage(void*)
00216 {}
00217
00218 private:
00219
00220 void* operator new[] (size_t) throw() { return 0; }
00221 void operator delete[] (void*) { }
00222 void* operator new( size_t , char ) throw() { return 0; }
00223 SbThreadStorage<bool>& operator=(bool) { return *this; }
00224 };
00225
00226 #endif //_SB_THREAD_STORAGE_H_
00227
00228
00229
00230
00231