00001 #ifndef _CSELFDESTROYPOINTER 00002 #define _CSELFDESTROYPOINTER 00003 #include "../include/uses-declarations.h"/*A pointer which stores along with itself, if it 00004 destroys the element it points to 00005 */ 00006 00007 template<class T> 00008 class CSelfDestroyPointer{ 00010 mutable bool mIsSelfDestroyer; 00012 protected: 00013 T* mPointer; 00015 public: 00017 void resetWithoutDeleting(); 00019 inline bool isSelfDestroyer()const; 00021 inline void setIsSelfDestroyer(bool inisSelfDestroyer=true)const; 00023 void unsetIsSelfDestroyer()const; 00025 T* operator= (T* inPointer); 00027 T& operator*(); 00029 T const& operator*()const; 00031 T* operator -> (); 00033 T const* operator -> ()const; 00035 ~CSelfDestroyPointer(); 00037 CSelfDestroyPointer(T*, 00038 bool = true); 00040 CSelfDestroyPointer(const CSelfDestroyPointer<T>& 00041 inSelfDestroyPointer); 00043 CSelfDestroyPointer(); 00045 operator bool()const; 00047 operator T*()const; 00048 }; 00049 00050 00052 template<class T> 00053 void CSelfDestroyPointer<T>::resetWithoutDeleting(){ 00054 mPointer=0; 00055 } 00056 00057 template<class T> 00058 T* CSelfDestroyPointer<T>::operator=(T* inPointer){ 00059 00060 if(mIsSelfDestroyer){ 00061 delete mPointer; 00062 } 00063 return mPointer=inPointer; 00064 } 00065 00066 template<class T> 00067 T const& CSelfDestroyPointer<T>::operator *()const{ 00068 return *mPointer; 00069 } 00070 00071 template<class T> 00072 T const* CSelfDestroyPointer<T>::operator ->()const{ 00073 return mPointer; 00074 } 00075 00076 template<class T> 00077 T& CSelfDestroyPointer<T>::operator *(){ 00078 return *mPointer; 00079 } 00080 00081 template<class T> 00082 T* CSelfDestroyPointer<T>::operator ->(){ 00083 return mPointer; 00084 } 00085 00086 template<class T> 00087 CSelfDestroyPointer<T>::CSelfDestroyPointer(T* inPointer, 00088 bool inIsSelfDestroyer): 00089 mPointer(inPointer), 00090 mIsSelfDestroyer(inIsSelfDestroyer) 00091 { 00092 } 00094 template<class T> 00095 CSelfDestroyPointer<T>::CSelfDestroyPointer(const CSelfDestroyPointer<T>& in): 00096 mPointer(in.mPointer), 00097 mIsSelfDestroyer(in.mIsSelfDestroyer) 00098 { 00099 }; 00100 00101 template<class T> 00102 CSelfDestroyPointer<T>::CSelfDestroyPointer(): 00103 mPointer(0), 00104 mIsSelfDestroyer(true) 00105 { 00106 } 00107 00108 template<class T> 00109 CSelfDestroyPointer<T>::~CSelfDestroyPointer() 00110 { 00111 if(mIsSelfDestroyer){ 00112 00113 delete mPointer; 00114 } 00115 } 00116 00117 00118 template<class T> 00119 void CSelfDestroyPointer<T>::setIsSelfDestroyer(bool inIsSelfDestroyer)const{ 00120 mIsSelfDestroyer= inIsSelfDestroyer; 00121 }; 00122 00123 template<class T> 00124 bool CSelfDestroyPointer<T>::isSelfDestroyer()const{ 00125 return mIsSelfDestroyer; 00126 }; 00127 00128 template<class T> 00129 void CSelfDestroyPointer<T>::unsetIsSelfDestroyer()const{ 00130 mIsSelfDestroyer=0; 00131 }; 00132 00133 template<class T> 00134 CSelfDestroyPointer<T>::operator bool()const{ 00135 return mPointer; 00136 }; 00137 00138 template<class T> 00139 CSelfDestroyPointer<T>::operator T*()const{ 00140 return mPointer; 00141 }; 00142 00143 template<class T> 00144 class CSelfClonePointer: public CSelfDestroyPointer<T>{ 00146 mutable bool mIsSelfCloner; 00148 public: 00150 CSelfClonePointer(T*, 00151 bool = true); 00153 CSelfClonePointer<T>& operator= (T* in); 00155 CSelfClonePointer<T>& operator= (const CSelfClonePointer<T>& in); 00157 CSelfClonePointer(const CSelfClonePointer<T>&); 00159 CSelfClonePointer(); 00161 operator bool()const; 00163 operator T*()const; 00164 }; 00165 00166 00167 template<class T> 00168 CSelfClonePointer<T>& CSelfClonePointer<T>::operator=(T* in){ 00169 00170 00171 CSelfDestroyPointer<T>::operator=(in); 00172 return *this; 00173 }; 00174 00175 template<class T> 00176 CSelfClonePointer<T>& CSelfClonePointer<T>::operator= (const CSelfClonePointer<T>& in){ 00177 00178 mPointer=in.mPointer; 00179 setIsSelfDestroyer(in.isSelfDestroyer()); 00180 return *this; 00181 }; 00182 00183 template<class T> 00184 CSelfClonePointer<T>::CSelfClonePointer(T* inPointer,bool inIsSelfCloner): 00185 CSelfDestroyPointer<T>(inPointer, 00186 inIsSelfCloner) 00187 { 00188 } 00189 template<class T> 00190 CSelfClonePointer<T>::CSelfClonePointer(): 00191 CSelfDestroyPointer<T>(0, 00192 true) 00193 { 00194 } 00195 template<class T> 00196 CSelfClonePointer<T>::CSelfClonePointer(const CSelfClonePointer<T>& in): 00197 CSelfDestroyPointer<T>(in) 00198 { 00199 if(in.mPointer && in.isSelfDestroyer()){ 00200 mPointer=in.mPointer->clone(); 00201 }else{ 00202 mPointer=in.mPointer; 00203 } 00204 } 00205 00206 #endif
1.5.6