00001 /* 00002 * $Id: SharedVar.h 562 2007-12-02 08:04:16Z mindstorm2600 $ 00003 * 00004 * X-VR2 00005 * 00006 * Copyright (C) Juan V. Guerrero 2007 00007 * 00008 * Juan V. Guerrero <mindstorm2600@users.sourceforge.net> 00009 * 00010 * This program is free software, distributed under the terms of 00011 * the GNU General Public License Version 2. See the LICENSE file 00012 * at the top of the source tree. 00013 */ 00014 #ifndef __XVR2_SHARED_VAR_H__ 00015 #define __XVR2_SHARED_VAR_H__ 00016 #include<xvr2/Mutex.h> 00017 #include<xvr2/CoreExceptions.h> 00018 00019 00020 namespace xvr2{ 00021 00025 template <class T> 00026 class SharedVar:protected Mutex{ 00027 private: 00029 T var; 00030 public: 00032 T getValue(T v = 0){ 00033 lock(); 00034 v = var; 00035 unlock(); 00036 return v; 00037 } 00039 void setValue(T v = 0){ 00040 lock(); 00041 var = v; 00042 unlock(); 00043 } 00045 SharedVar(){ 00046 } 00048 SharedVar(T v){ 00049 //setValue(v); 00050 var = v; 00051 } 00053 T operator++(){ 00054 T u= 0; 00055 lock(); 00056 u = var; 00057 u++; 00058 var = u; 00059 unlock(); 00060 return u; 00061 } 00063 T operator--(){ 00064 T u= 0; 00065 lock(); 00066 u = var; 00067 u--; 00068 var = u; 00069 unlock(); 00070 return u; 00071 } 00072 T operator=(T v){ 00073 lock(); 00074 var = v; 00075 unlock(); 00076 return v; 00077 } 00078 bool operator==(T v){ 00079 bool ret; 00080 lock(); 00081 ret = (var==v)?true:false; 00082 unlock(); 00083 return ret; 00084 } 00085 bool operator!=(T v){ 00086 bool ret; 00087 lock(); 00088 ret = (var==v)?false:true; 00089 unlock(); 00090 return ret; 00091 } 00092 }; 00093 } 00094 00095 #endif