00001 /* Copyright (C) 2000 KOM/Darmstadt University of Technology 00002 * 00003 * You are allowed to use all other parts of the code under the following terms: 00004 * 00005 * For non-commercial use, code may be used in unmodified form provided 00006 * that this copyright notice and this permission notice appear in 00007 * supporting documentation. 00008 * 00009 * This software is provided "as is" and without any express or implied 00010 * warranties, including, without limitation, the implied warranty of 00011 * fitness for a particular purpose. 00012 * 00013 * The code may be subjected to the GNU General Public License, Version 2, 00014 * and re-distributed under the terms of this license. 00015 * As a special exception, permission is granted to link this code 00016 * with the Qt library and distribute executables, as long as you 00017 * follow the requirements of the GNU GPL in regard to all of the 00018 * software in the executable aside from Qt. 00019 * 00020 * Commercial use other than under the terms of the GNU General Public 00021 * License is allowed only after express negotiation of conditions 00022 * with the authors. 00023 */ 00024 #ifndef MNPROTECTEDVAR_TYPE_H 00025 #define MNPROTECTEDVAR_TYPE_H 00026 00027 #include <config.h> 00028 00029 #include "mnstream.h" 00030 #include "MNMutex.h" 00031 00032 /*********************************************************************** 00033 * ProtectedVar<T> 00034 * - protects integer-like variables with a mutex 00035 ***********************************************************************/ 00036 00037 /* Template Protected Var - use it for creating mutexed protected numeric variables*/ 00038 template<class T> class ProtectedVar 00039 { 00040 T _x; 00041 MNMutex _m; 00042 public: 00043 ProtectedVar ( T i ) 00044 :_x(i) 00045 { 00046 } 00047 00048 ProtectedVar () 00049 { 00050 _x=(T) 0; 00051 } 00052 00053 inline ProtectedVar<T>& operator= ( T i ); 00054 inline ProtectedVar<T>& operator+=( T i ); 00055 inline ProtectedVar<T>& operator-=( T i ); 00056 00057 inline bool operator==( T i ); 00058 inline bool operator!=( T i ); 00059 00060 inline T operator++(); 00061 inline T operator++(int); 00062 inline T operator--(); 00063 inline T operator--(int); 00064 00065 inline T replace( T nval ); 00066 00067 inline operator T(); // implicit typecast operator 00068 00069 //friend ostream& operator<< ( ostream&, ProtectedVar<T>& ); 00070 00071 inline void print_T( ostream& ostr ); 00072 00073 }; 00074 00075 template<class T> 00076 inline ProtectedVar<T>& ProtectedVar<T>::operator=( T i ) 00077 { 00078 _m.lock(); 00079 _x = i; 00080 _m.unlock(); 00081 return *this; 00082 } 00083 00084 template<class T> 00085 inline ProtectedVar<T>& ProtectedVar<T>::operator+=(T i) 00086 { 00087 _m.lock(); 00088 _x+=i; 00089 _m.unlock(); 00090 return *this; 00091 } 00092 00093 template<class T> 00094 inline ProtectedVar<T>& ProtectedVar<T>::operator-=(T i) 00095 { 00096 _m.lock(); 00097 _x-=i; 00098 _m.unlock(); 00099 return *this; 00100 } 00101 00102 template<class T> 00103 inline bool ProtectedVar<T>::operator==( T i ) 00104 { 00105 _m.lock(); 00106 bool ret = ( _x == i ); 00107 _m.unlock(); 00108 return ret; 00109 } 00110 00111 template<class T> 00112 inline bool ProtectedVar<T>::operator!=( T i ) 00113 { 00114 _m.lock(); 00115 bool ret = ( _x != i ); 00116 _m.unlock(); 00117 return ret; 00118 } 00119 00120 template<class T> 00121 inline T ProtectedVar<T>::operator++() 00122 { 00123 _m.lock(); 00124 T ret = ++_x; 00125 _m.unlock(); 00126 return ret; 00127 } 00128 00129 template<class T> 00130 inline T ProtectedVar<T>::operator++(int) 00131 { 00132 _m.lock(); 00133 T ret = _x++; 00134 _m.unlock(); 00135 return ret; 00136 } 00137 00138 template<class T> 00139 inline T ProtectedVar<T>::operator--() 00140 { 00141 _m.lock(); 00142 T ret = --_x; 00143 _m.unlock(); 00144 return ret; 00145 } 00146 00147 template<class T> 00148 inline T ProtectedVar<T>::operator--(int) 00149 { 00150 _m.lock(); 00151 T ret = _x--; 00152 _m.unlock(); 00153 return ret; 00154 } 00155 00156 template<class T> 00157 inline T ProtectedVar<T>::replace( T nval ) 00158 { 00159 _m.lock(); 00160 T ret = _x; 00161 _x = nval; 00162 _m.unlock(); 00163 return ret; 00164 } 00165 00166 template<class T> 00167 inline ProtectedVar<T>::operator T() // implicit typecast operator 00168 { 00169 _m.lock(); 00170 T ret = _x; 00171 _m.unlock(); 00172 return ret; 00173 } 00174 00175 template<class T> 00176 inline void ProtectedVar<T>::print_T( ostream& ostr ) 00177 { 00178 _m.lock(); 00179 ostr << _x; 00180 _m.unlock(); 00181 } 00182 00183 extern ostream& operator<< ( ostream& ostr, ProtectedVar<int>& pi ); 00184 00185 /*********************************************************************** 00186 * ProtectedPtr<T> 00187 * - protects known pointer variables with a mutex 00188 ***********************************************************************/ 00189 00190 /* Template Protected Var - use it for creating mutexed protected numeric variables*/ 00191 template<class T> class ProtectedPtr 00192 { 00193 T _x; 00194 MNMutex _m; 00195 public: 00196 ProtectedPtr ( T i ) 00197 :_x(i) 00198 { 00199 } 00200 00201 ProtectedPtr () 00202 { 00203 _x=(T) 0; 00204 } 00205 00206 inline ProtectedPtr<T>& operator= ( T i ); 00207 inline ProtectedPtr<T>& operator+=( int i ); 00208 inline ProtectedPtr<T>& operator-=( int i ); 00209 00210 inline bool operator==( T i ); 00211 inline bool operator!=( T i ); 00212 00213 inline T operator++(); 00214 inline T operator++(int); 00215 inline T operator--(); 00216 inline T operator--(int); 00217 00218 inline T replace( T nval ); 00219 00220 inline operator T(); // implicit typecast operator 00221 00222 //friend ostream& operator<< ( ostream&, ProtectedPtr<T>& ); 00223 00224 inline void print_T( ostream& ostr ); 00225 00226 }; 00227 00228 template<class T> 00229 inline ProtectedPtr<T>& ProtectedPtr<T>::operator=( T i ) 00230 { 00231 _m.lock(); 00232 _x = i; 00233 _m.unlock(); 00234 return *this; 00235 } 00236 00237 template<class T> 00238 inline ProtectedPtr<T>& ProtectedPtr<T>::operator+=(int i) 00239 { 00240 _m.lock(); 00241 _x+=i; 00242 _m.unlock(); 00243 return *this; 00244 } 00245 00246 template<class T> 00247 inline ProtectedPtr<T>& ProtectedPtr<T>::operator-=(int i) 00248 { 00249 _m.lock(); 00250 _x-=i; 00251 _m.unlock(); 00252 return *this; 00253 } 00254 00255 template<class T> 00256 inline bool ProtectedPtr<T>::operator==( T i ) 00257 { 00258 _m.lock(); 00259 bool ret = ( _x == i ); 00260 _m.unlock(); 00261 return ret; 00262 } 00263 00264 template<class T> 00265 inline bool ProtectedPtr<T>::operator!=( T i ) 00266 { 00267 _m.lock(); 00268 bool ret = ( _x != i ); 00269 _m.unlock(); 00270 return ret; 00271 } 00272 00273 template<class T> 00274 inline T ProtectedPtr<T>::operator++() 00275 { 00276 _m.lock(); 00277 T ret = ++_x; 00278 _m.unlock(); 00279 return ret; 00280 } 00281 00282 template<class T> 00283 inline T ProtectedPtr<T>::operator++(int) 00284 { 00285 _m.lock(); 00286 T ret = _x++; 00287 _m.unlock(); 00288 return ret; 00289 } 00290 00291 template<class T> 00292 inline T ProtectedPtr<T>::operator--() 00293 { 00294 _m.lock(); 00295 T ret = --_x; 00296 _m.unlock(); 00297 return ret; 00298 } 00299 00300 template<class T> 00301 inline T ProtectedPtr<T>::operator--(int) 00302 { 00303 _m.lock(); 00304 T ret = _x--; 00305 _m.unlock(); 00306 return ret; 00307 } 00308 00309 template<class T> 00310 inline T ProtectedPtr<T>::replace( T nval ) 00311 { 00312 _m.lock(); 00313 T ret = _x; 00314 _x = nval; 00315 _m.unlock(); 00316 return ret; 00317 } 00318 00319 template<class T> 00320 inline ProtectedPtr<T>::operator T() // implicit typecast operator 00321 { 00322 _m.lock(); 00323 T ret = _x; 00324 _m.unlock(); 00325 return ret; 00326 } 00327 00328 template<class T> 00329 inline void ProtectedPtr<T>::print_T( ostream& ostr ) 00330 { 00331 _m.lock(); 00332 ostr << _x; 00333 _m.unlock(); 00334 } 00335 00336 extern ostream& operator<< ( ostream& ostr, ProtectedPtr<int>& pi ); 00337 00338 /*********************************************************************** 00339 * ProtectedVarEnum<T> 00340 * - protects enum variable with a mutex 00341 ***********************************************************************/ 00342 00343 template<class T> class ProtectedVarEnum 00344 { 00345 T _x; 00346 mutable MNMutex _m; 00347 public: 00348 ProtectedVarEnum( T i ) 00349 :_x(i) 00350 { 00351 } 00352 00353 ProtectedVarEnum() 00354 { 00355 _x=(T) 0; 00356 } 00357 00358 ProtectedVarEnum<T>& operator=( T i ) 00359 { 00360 _m.lock(); 00361 _x = i; 00362 _m.unlock(); 00363 return *this; 00364 } 00365 00366 bool operator==( T i ) const 00367 { 00368 _m.lock(); 00369 bool ret = ( _x == i ); 00370 _m.unlock(); 00371 return ret; 00372 } 00373 00374 operator T() const // implicit typecast operator 00375 { 00376 _m.lock(); 00377 T ret = _x; 00378 _m.unlock(); 00379 return ret; 00380 } 00381 }; 00382 00383 00384 class ProtectedBool 00385 { 00386 bool _x; 00387 MNMutex _m; 00388 public: 00389 ProtectedBool ( bool i ) 00390 :_x(i) 00391 { 00392 } 00393 00394 ProtectedBool () 00395 { 00396 _x=false; 00397 } 00398 00399 ProtectedBool& operator=( bool i ) 00400 { 00401 _m.lock(); 00402 _x = i; 00403 _m.unlock(); 00404 return *this; 00405 } 00406 00407 bool operator==( bool i ) 00408 { 00409 _m.lock(); 00410 bool ret = ( _x == i ); 00411 _m.unlock(); 00412 return ret; 00413 } 00414 00415 operator bool() // implicit typecast operator 00416 { 00417 _m.lock(); 00418 bool ret = _x; 00419 _m.unlock(); 00420 return ret; 00421 } 00422 00423 void print_B( ostream& ostr ) 00424 { 00425 _m.lock(); 00426 if (_x) 00427 ostr << "true"; 00428 else 00429 ostr << "false"; 00430 _m.unlock(); 00431 } 00432 00433 friend ostream& operator << ( ostream&, ProtectedBool& ); 00434 }; 00435 00436 #endif