Go to the documentation of this file.00001 #ifndef wali_REF_PTR_GUARD
00002 #define wali_REF_PTR_GUARD 1
00003
00004
00005
00006
00007
00008
00009 #include <cstdio>
00010 #include <cassert>
00011 #include <climits>
00012 #include <iostream>
00013
00014 namespace wali
00015 {
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 template< typename T > class ref_ptr
00045 {
00046
00047 public:
00048 typedef unsigned int count_t;
00049
00050 ref_ptr( T *t = 0 ) {
00051 acquire(t);
00052 }
00053
00054 ref_ptr( const ref_ptr& rp ) {
00055 acquire( rp.ptr );
00056 }
00057
00058
00059
00060
00061 template< typename S > ref_ptr<T>( const ref_ptr<S>& rp ) {
00062 acquire( rp.get_ptr() );
00063 }
00064
00065 ~ref_ptr() {
00066 release();
00067 }
00068
00069 ref_ptr& operator=( T * t ) {
00070 if( ptr != t ) {
00071 T * old_ptr = ptr;
00072 acquire(t);
00073 release(old_ptr);
00074 }
00075 return *this;
00076 }
00077
00078 ref_ptr& operator=( const ref_ptr& rp ) {
00079 return *this = rp.get_ptr();
00080 }
00081
00082 bool operator==(const ref_ptr& that) const {
00083 return ptr == that.get_ptr();
00084 }
00085
00086 bool operator!=(const ref_ptr& that) const {
00087 return ptr != that.get_ptr();
00088 }
00089
00090 T * get_ptr() const {
00091 return ptr;
00092 }
00093
00094 T * operator->() const {
00095 return ptr;
00096 }
00097
00098 T& operator*() const {
00099 assert(0 != ptr);
00100 return *ptr;
00101 }
00102
00103 bool is_empty() const { return (0 == ptr); }
00104 bool is_valid() const { return !(is_empty());}
00105
00106 private:
00107 T * ptr;
00108
00109 void acquire( T * t )
00110 {
00111 ptr = t;
00112 if( t ) {
00113 ++t->count;
00114 #ifdef DBGREFPTR
00115 std::cout << "Acquired " << t << " with count = "
00116 << t->count << std::endl;
00117 #endif
00118 }
00119 }
00120
00121 static void release( T * old_ptr )
00122 {
00123 if( old_ptr ) {
00124 --old_ptr->count;
00125 #ifdef DBGREFPTR
00126 std::cout << "Released " << *old_ptr << " with count = "
00127 << old_ptr->count << std::endl;
00128 #endif
00129 if( old_ptr->count == 0 ) {
00130 #ifdef DBGREFPTR
00131 std::cout << "Deleting ptr: " << *old_ptr << std::endl;
00132 #endif
00133 delete old_ptr;
00134 }
00135 }
00136 }
00137
00138 void release() {
00139 release(ptr);
00140 ptr = 0;
00141 }
00142
00143 };
00144
00145 }
00146
00147 #endif // wali_REF_PTR_GUARD
00148