Open Inventor Release 2024.1.3
 
Loading...
Searching...
No Matches
SbThreadStorage.h
1/*=======================================================================
2 *** THE CONTENT OF THIS WORK IS PROPRIETARY TO FEI S.A.S, (FEI S.A.S.), ***
3 *** AND IS DISTRIBUTED UNDER A LICENSE AGREEMENT. ***
4 *** ***
5 *** REPRODUCTION, DISCLOSURE, OR USE, IN WHOLE OR IN PART, OTHER THAN AS ***
6 *** SPECIFIED IN THE LICENSE ARE NOT TO BE UNDERTAKEN EXCEPT WITH PRIOR ***
7 *** WRITTEN AUTHORIZATION OF FEI S.A.S. ***
8 *** ***
9 *** RESTRICTED RIGHTS LEGEND ***
10 *** USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT OF THE CONTENT OF THIS ***
11 *** WORK OR RELATED DOCUMENTATION IS SUBJECT TO RESTRICTIONS AS SET FORTH IN ***
12 *** SUBPARAGRAPH (C)(1) OF THE COMMERCIAL COMPUTER SOFTWARE RESTRICTED RIGHT ***
13 *** CLAUSE AT FAR 52.227-19 OR SUBPARAGRAPH (C)(1)(II) OF THE RIGHTS IN ***
14 *** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 52.227-7013. ***
15 *** ***
16 *** COPYRIGHT (C) 1996-2022 BY FEI S.A.S, ***
17 *** BORDEAUX, FRANCE ***
18 *** ALL RIGHTS RESERVED ***
19**=======================================================================*/
20/*=======================================================================
21** Author : Nicolas DAGUISE (Nov 2008)
22**=======================================================================*/
23
24
25
26#ifndef _SB_THREAD_STORAGE_H_
27#define _SB_THREAD_STORAGE_H_
28
29#include <Inventor/SoInventorBase.h>
30
31#include <Inventor/threads/SbThreadStorageBase.h>
32#include <Inventor/STL/functional>
33
41template <class T>
42class SbThreadStorage : public SbThreadStorageBase
43{
44public:
49
50 /*
51 * Copy constructor.
52 */
53 SbThreadStorage<T>( const T t )
54 {
55 Register( t );
56 }
57
65 { erase(); }
66
73 {
74 Register( t );
75 return *this;
76 }
77
83 T operator->() const
84 {
85#if defined(_DEBUG)
86 T t = static_cast<T>(getValue());
87 if ( t == NULL && SoInventorBase::checkMultithread() )
88 fprintf( stderr, "MT ERROR: in ::operator-> , object not initialized for current thread." );
89 return t;
90#else
91 return static_cast<T>(getValue());
92#endif
93 }
94
99 T operator*() const
100 {
101 return static_cast<T>(getValue());
102 }
103
111 operator bool() const
112 {
113 return ((static_cast<T>(getValue())) != NULL);
114 }
115
117 template< typename Func>
118 void call( Func f )
119 {
120 s_threadStorageGlobalMutex.lock();
121 SbThreadStorageGlobalList::iterator it_i = s_threadStorageGlobalList.begin();
122 while(it_i != s_threadStorageGlobalList.end())
123 {
124 // For each registered map, search for this threadStorage and apply function call.
125 void* value=get(it_i);
126 if (value!=NULL)
127 std::mem_fn(f)(static_cast<T>(value));
128 ++it_i;
129 }
130 s_threadStorageGlobalMutex.unlock();
131 }
132
134 template< typename Func, typename Param>
135 void call( Func f, Param p )
136 {
137 s_threadStorageGlobalMutex.lock();
138 SbThreadStorageGlobalList::iterator it_i = s_threadStorageGlobalList.begin();
139 while(it_i != s_threadStorageGlobalList.end())
140 {
141 // For each registered map, search for this threadStorage and apply function call.
142 void* value=get(it_i);
143 if (value!=NULL)
144 std::bind(std::mem_fn((Func)(f)), std::placeholders::_1, p)(static_cast<T>(value));
145 ++it_i;
146 }
147 s_threadStorageGlobalMutex.unlock();
148 }
149
150
151private:
156 virtual void deleteStorage(void* p)
157 {
158 T val = (static_cast<T>(p));
159 if (val)
160 val->unref();
161 }
162
163private:
164 // Disable several operators
165 void* operator new[] (size_t) throw() { return 0; }
166 void operator delete[] (void*) { }
167 void* operator new( size_t, char ) throw() { return 0; }
168};
169
176template <>
177class SbThreadStorage<bool> : public SbThreadStorageBase
178{
179public:
181 virtual ~SbThreadStorage<bool>()
182 { erase(); }
183
188 SbThreadStorage<bool>& operator <<=(bool t)
189 {
190 void* pt = reinterpret_cast<void*>(static_cast<uintptr_t>(t));
191 Register( pt );
192 return (*this);
193 }
194
199 SbThreadStorage<bool>& operator >>=(bool t)
200 {
201 void *pt = reinterpret_cast<void*>(static_cast<uintptr_t>(t));
202 Register( pt );
203 setAll(pt);
204 return *this;
205 }
206
210 operator bool() const
211 { return ((static_cast<bool>(getValue())) == true); }
212
213private:
215 virtual void deleteStorage(void*)
216 {}
217
218private:
219 // Disable several operators
220 void* operator new[] (size_t) throw() { return 0; }
221 void operator delete[] (void*) { }
222 void* operator new( size_t , char ) throw() { return 0; }
223 SbThreadStorage<bool>& operator=(bool) { return *this; }
224};
225
226#endif //_SB_THREAD_STORAGE_H_
227
228
229
230
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Template class f...
T operator*() const
Override operator T*() Destroying objects from delete operator is not allowed.
T operator->() const
Override operator-> Gets value of the storage for current thread.
virtual ~SbThreadStorage()
Storage destructor: Find current storage in the global storage map, Delete thread dependent entry,...
void call(Func f, Param p)
Calls the passed function to all registered threads object.
SbThreadStorage< T > & operator=(T t)
Override operator= Set value of the storage for current thread.
void call(Func f)
Calls the passed function to all registered threads object.