Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

IncrementHolder.h

Go to the documentation of this file.
00001 #ifndef IncrementHolder_H 00002 #define IncrementHolder_H 00003 00004 #include <PtMutex.h> 00005 #include <LockHolder.h> 00006 #include <iostream> 00007 00008 /* 00009 When this object is instantiated, it increments the specified value using 00010 the ++ operator. When this object is uninstantiated, it decrements the 00011 specified value using the -- operator. It temporarily holds a lock on the 00012 supplied mutex while performing each of those operations. 00013 */ 00014 template<typename T> 00015 class IncrementHolder 00016 { 00017 public: 00018 // Throws an exception if the bounds are violated during this object's 00019 // instantiation / uninstantiation. 00020 IncrementHolder(T& value, 00021 PtMutex &lock, 00022 T minValue, 00023 T maxValue) 00024 throw (std::exception); 00025 00026 virtual ~IncrementHolder(); 00027 00028 private: 00029 void verifyBounds() 00030 throw (std::exception); 00031 00032 PtMutex & _lock; 00033 T& _value; 00034 T _minValue; 00035 T _maxValue; 00036 }; 00037 00038 00039 //=============================================================================== 00040 00041 template<typename T> 00042 IncrementHolder::IncrementHolder(T& value, 00043 PtMutex &lock, 00044 T minValue, 00045 T maxValue) 00046 throw (std::exception) : 00047 _value(value), 00048 _lock(lock), 00049 _minValue(minValue), 00050 _maxValue(maxValue) 00051 { 00052 LockHolder holder(_lock); 00053 _value++; 00054 verifyBounds(); 00055 } 00056 00057 //=============================================================================== 00058 00059 template<typename T> 00060 IncrementHolder::~IncrementHolder() 00061 { 00062 try 00063 { 00064 LockHolder holder(_lock); 00065 _value--; 00066 verifyBounds(); 00067 } 00068 catch (std::exception &e) 00069 { 00070 cerr << e.what() << endl; 00071 assert(false); 00072 } 00073 } 00074 00075 //=============================================================================== 00076 00077 template<typename T> 00078 void IncrementHolder::verifyBounds() 00079 throw (std::exception) 00080 { 00081 if (_value < _minValue) 00082 { 00083 Throw(AuroraException, "_value < _minValue"); 00084 } 00085 00086 if (_value > _maxValue) 00087 { 00088 Throw(AuroraException, "_value > _maxValue"); 00089 } 00090 } 00091 00092 00093 #endif

Generated on Fri Nov 12 15:15:21 2004 for Borealis by doxygen 1.3.8