referenceCount.h

Go to the documentation of this file.
00001 
00015 #ifndef _DLR_COMMON_REFERENCECOUNT_H_
00016 #define _DLR_COMMON_REFERENCECOUNT_H_
00017 
00018 #include <cstddef>
00019 
00020 namespace dlr {
00021 
00022   namespace common {
00023     
00040     class ReferenceCount
00041     {
00042     public:
00054       ReferenceCount(size_t count=1)
00055         : m_countPtr(0) {
00056         if(count != 0) {
00057           m_countPtr = new size_t;
00058           *m_countPtr = count;
00059         }
00060       }
00061 
00062     
00069       ReferenceCount(const ReferenceCount& other)
00070         : m_countPtr(other.m_countPtr) {
00071         if(m_countPtr != 0) {++(*m_countPtr);}
00072       }
00073 
00074     
00078       ~ReferenceCount() {this->deleteIfNecessary();}
00079 
00080     
00086       ReferenceCount&
00087       operator++() {
00088         if(m_countPtr != 0) {++(*m_countPtr);}
00089         return *this;
00090       }
00091 
00092     
00100       ReferenceCount
00101       operator++(int) {return ++(*this);}
00102 
00103     
00109       ReferenceCount&
00110       operator--() {
00111         if(m_countPtr != 0) {
00112           if((*m_countPtr) != 0) {--(*m_countPtr);}
00113         }
00114         return *this;
00115       }
00116 
00117     
00125       ReferenceCount
00126       operator--(int) {return --(*this);}
00127 
00128 
00136       ReferenceCount&
00137       operator+=(size_t offset) {
00138         if(m_countPtr != 0) {(*m_countPtr) += offset;}
00139         return *this;
00140       }
00141 
00142     
00150       ReferenceCount&
00151       operator-=(size_t offset) {
00152         if(m_countPtr != 0) {
00153           if((*m_countPtr) > offset) {(*m_countPtr) -= offset;}
00154           else {(*m_countPtr) = 0;}
00155         }
00156         return *this;
00157       }
00158 
00159     
00168       ReferenceCount&
00169       operator=(const ReferenceCount& source) {
00170         // Check for self-assignment.
00171         if (this != &source) {
00172           this->deleteIfNecessary();
00173           m_countPtr = source.m_countPtr;
00174           if(m_countPtr != 0) {++(*m_countPtr);}
00175         }
00176         return *this;
00177       }
00178 
00179     
00185       size_t
00186       count() const {return *m_countPtr;}
00187 
00188     
00196       bool
00197       isShared() const {
00198         if(m_countPtr == 0) {return false;}
00199         return ((*m_countPtr) > 1);
00200       }
00201 
00202     
00213       void
00214       reset(size_t count=1) {
00215         this->deleteIfNecessary();
00216         if(count != 0) {
00217           m_countPtr = new size_t;
00218           *m_countPtr = count;
00219         }
00220       }
00221 
00222       
00230       bool
00231       shared() const {
00232         return this->isShared();
00233       }
00234 
00235     
00236     private:
00237 
00242       void deleteIfNecessary() {
00243         if(m_countPtr != 0) {
00244           switch(*m_countPtr) {
00245           case 0:
00246           case 1:
00247             delete m_countPtr;
00248             m_countPtr = 0;
00249             break;
00250           default:
00251             --(*m_countPtr);
00252           }
00253         }
00254       }
00255     
00256       size_t* m_countPtr;
00257     };
00258 
00259   } // namespace common
00260   
00261 } // namespace dlr
00262 
00263 
00264 /* ======= Declarations to maintain compatibility with legacy code. ======= */
00265 
00266 namespace dlr {
00267 
00268   using common::ReferenceCount;
00269   
00270 } // namespace dlr
00271 
00272 #endif // #ifndef _DLR_COMMON_REFERENCECOUNT_H_

Generated on Tue Nov 24 23:57:55 2009 for dlrCommon Utility Library by  doxygen 1.5.8