00001 00002 00003 00004 00005 00006
00007 #ifndef CountedPointer_h
00008 #define CountedPointer_h
00009
00010 #include "ReferenceCount.h"
00011 #include "LibGPIB.h"
00012
00013 extern "C++" {
00014 namespace LibGPIB {
00033 template<class PointedType>
00034 class CountedPointer {
00035 public:
00039
00041
00042 00043 00044 00045
00050 static const CountedPointer CreateWithObject() ;
00051
00055
00057
00061
00063
00067
00070 CountedPointer() ;
00071
00075 ~CountedPointer() ;
00077
00085 bool IsUnique() const ;
00086
00090 bool IsNull() const ;
00091
00092
00094
00101 CountedPointer& operator=(const CountedPointer& other) ;
00102
00106 PointedType& operator*() const ;
00107
00112 PointedType* operator->() const ;
00114
00115 00116 00117 00118
00119
00123 friend bool operator==(const CountedPointer<PointedType>& lhs,
00124 const CountedPointer<PointedType>& rhs) {
00125 return (lhs.mPointer == rhs.mPointer) ;
00126 }
00127
00131 friend bool operator!=(const CountedPointer<PointedType>& lhs,
00132 const CountedPointer<PointedType>& rhs) {
00133 return !(lhs == rhs) ;
00134 }
00135
00136 protected:
00137 00138 00139 00140
00144 PointedType *mPointer ;
00145
00146 00147 00148 00149
00150
00151 private:
00152 00153 00154 00155
00159 ReferenceCount mRefCount ;
00160
00161 00162 00163 00164
00168 CountedPointer(PointedType *new_object) ;
00169
00170 } ;
00171
00172 template<class PointedType>
00173 CountedPointer<PointedType>::CountedPointer() : mPointer(0) {
00174
00175 }
00176
00177 template<class PointedType>
00178 CountedPointer<PointedType>::~CountedPointer() {
00179 if( mRefCount.IsUnique() ) {
00180 delete mPointer ;
00181 }
00182 }
00183
00184 template<class PointedType>
00185 CountedPointer<PointedType>::CountedPointer(PointedType *new_object)
00186 : mPointer(new_object) {
00187
00188 }
00189
00190 template<class PointedType>
00191 bool
00192 CountedPointer<PointedType>::IsUnique() const {
00193 return mRefCount.IsUnique() ;
00194 }
00195
00196 template<class PointedType>
00197 bool
00198 CountedPointer<PointedType>::IsNull() const {
00199 return (0 == mPointer) ;
00200 }
00201
00202 template<class PointedType>
00203 const CountedPointer<PointedType>
00204 CountedPointer<PointedType>::CreateWithObject() {
00205 return CountedPointer(new PointedType) ;
00206 }
00207
00208 template<class PointedType>
00209 CountedPointer<PointedType>&
00210 CountedPointer<PointedType>::operator=(const CountedPointer& other) {
00211 if( mPointer != other.mPointer ) {
00212 if( IsUnique() ) {
00213 delete mPointer ;
00214 }
00215 mPointer = other.mPointer ;
00216 mRefCount = other.mRefCount ;
00217 }
00218 return *this ;
00219 }
00220
00221 template<class PointedType>
00222 PointedType *
00223 CountedPointer<PointedType>::operator->() const {
00224 return mPointer ;
00225 }
00226
00227 template<class PointedType>
00228 PointedType&
00229 CountedPointer<PointedType>::operator*() const {
00230 return *mPointer ;
00231 }
00232
00233 } ;
00234 }
00235 #endif // #ifndef CountedPointer_h