_ref_count.h

Go to the documentation of this file.
00001 
00025 /* === S T A R T =========================================================== */
00026 
00027 #ifndef __ETL_REF_COUNT_H
00028 #define __ETL_REF_COUNT_H
00029 
00030 /* === H E A D E R S ======================================================= */
00031 
00032 #include "_curve_func.h"
00033 #include <cassert>
00034 
00035 /* === M A C R O S ========================================================= */
00036 
00037 /* === T Y P E D E F S ===================================================== */
00038 
00039 /* === C L A S S E S & S T R U C T S ======================================= */
00040 
00041 _ETL_BEGIN_NAMESPACE
00042 
00043 class weak_reference_counter;
00044     
00045 // ========================================================================
00051 class reference_counter
00052 {
00053     friend class weak_reference_counter;
00054 private:
00055     int* counter_;
00056 public:
00057 
00058     reference_counter(const bool &x=true):counter_(x?new int(1):0) { }
00059     
00060     reference_counter(const reference_counter &x):counter_(x.counter_)
00061         { if(counter_) (*counter_)++; }
00062 
00063     reference_counter(const weak_reference_counter &x);
00064         
00065     ~reference_counter() { detach(); }
00066     
00067     reference_counter& operator=(const reference_counter &rhs)
00068     {
00069         detach();
00070         counter_=rhs.counter_;
00071         if(counter_)
00072         {
00073             assert(*counter_>0);
00074             (*counter_)++;  
00075         }
00076         return *this;           
00077     }
00078 
00079     void detach()
00080     {
00081         if(counter_)
00082         {
00083             assert(*counter_>0);
00084             if(!--(*counter_))
00085                 delete counter_;
00086             counter_=0;
00087         }       
00088     }   
00089     
00090     void reset()
00091     {
00092         detach();
00093         counter_=new int(1);        
00094     }
00095     
00096     int count()const { return counter_?*counter_:0; }
00097 
00098     bool unique()const { return counter_?*counter_==1:0; }
00099     
00100     operator int()const { return count(); }
00101 }; // END of class reference_counter
00102 
00103 // ========================================================================
00109 class weak_reference_counter
00110 {
00111     friend class reference_counter;
00112 private:
00113     int* counter_;
00114 public:
00115     weak_reference_counter():counter_(0) { }
00116     
00117     weak_reference_counter(const weak_reference_counter &x):counter_(x.counter_) { }
00118 
00119     weak_reference_counter(const reference_counter &x):counter_(x.counter_) { }
00120     
00121     ~weak_reference_counter() { }
00122     
00123     weak_reference_counter& operator=(const reference_counter &rhs)
00124     {
00125         counter_=rhs.counter_;
00126         assert(*counter_>0);
00127         return *this;
00128     }
00129 
00130     weak_reference_counter& operator=(const weak_reference_counter &rhs)
00131     {
00132         counter_=rhs.counter_;
00133         assert(*counter_>0);
00134         return *this;
00135     }
00136 
00137     void detach() { counter_=0; }   
00138     
00139     int count()const { return counter_?*counter_:0; }
00140 
00141     bool unique()const { return counter_?*counter_==1:0; }
00142     
00143     operator int()const { return count(); }
00144 }; // END of class weak_reference_counter
00145 
00146 inline reference_counter::reference_counter(const weak_reference_counter &x):
00147     counter_(x.counter_)
00148 {
00149     if(counter_) (*counter_)++;
00150 }
00151 
00152 _ETL_END_NAMESPACE
00153 
00154 /* === E X T E R N S ======================================================= */
00155 
00156 /* === E N D =============================================================== */
00157 
00158 #endif

Generated on Sat Nov 4 11:29:21 2006 for ETL by  doxygen 1.4.7