00001 /* -*- mode: c++ -*- 00002 */ 00003 /* 00004 00005 GIFT, a flexible content based image retrieval system. 00006 Copyright (C) 1998, 1999, 2000, 2001, 2002, CUI University of Geneva 00007 00008 Copyright (C) 2003, 2004 Bayreuth University 00009 2005 Bamberg University 00010 This program is free software; you can redistribute it and/or modify 00011 it under the terms of the GNU General Public License as published by 00012 the Free Software Foundation; either version 2 of the License, or 00013 (at your option) any later version. 00014 00015 This program is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU General Public License for more details. 00019 00020 You should have received a copy of the GNU General Public License 00021 along with this program; if not, write to the Free Software 00022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 00024 */ 00025 #ifndef _CSELFDESTROYPOINTER 00026 #define _CSELFDESTROYPOINTER 00027 #include "libMRML/include/uses-declarations.h" 00031 template<class T> 00032 class CSelfDestroyPointer{ 00034 mutable bool mIsSelfDestroyer; 00036 protected: 00037 T* mPointer; 00039 public: 00041 void resetWithoutDeleting(); 00043 inline bool isSelfDestroyer()const; 00045 inline void setIsSelfDestroyer(bool inisSelfDestroyer=true)const; 00047 void unsetIsSelfDestroyer()const; 00049 T* operator= (T* inPointer); 00051 T& operator*(); 00053 T const& operator*()const; 00055 T* operator -> (); 00057 T const* operator -> ()const; 00059 ~CSelfDestroyPointer(); 00061 CSelfDestroyPointer(T*, 00062 bool = true); 00064 CSelfDestroyPointer(const CSelfDestroyPointer<T>& 00065 inSelfDestroyPointer); 00067 CSelfDestroyPointer(); 00069 operator bool()const; 00071 operator T*()const; 00072 }; 00073 00074 00076 template<class T> 00077 void CSelfDestroyPointer<T>::resetWithoutDeleting(){ 00078 mPointer=0; 00079 } 00080 00081 template<class T> 00082 T* CSelfDestroyPointer<T>::operator=(T* inPointer){ 00083 00084 if(mIsSelfDestroyer){ 00085 #ifdef _DEBUG_SELF_DESTROY_ 00086 cout <<"£"<<flush; 00087 #endif 00088 delete mPointer; 00089 } 00090 mPointer=inPointer; 00091 } 00092 00093 template<class T> 00094 T const& CSelfDestroyPointer<T>::operator *()const{ 00095 return *mPointer; 00096 } 00097 00098 template<class T> 00099 T const* CSelfDestroyPointer<T>::operator ->()const{ 00100 return mPointer; 00101 } 00102 00103 template<class T> 00104 T& CSelfDestroyPointer<T>::operator *(){ 00105 return *mPointer; 00106 } 00107 00108 template<class T> 00109 T* CSelfDestroyPointer<T>::operator ->(){ 00110 return mPointer; 00111 } 00112 00113 template<class T> 00114 CSelfDestroyPointer<T>::CSelfDestroyPointer(T* inPointer, 00115 bool inIsSelfDestroyer): 00116 mPointer(inPointer), 00117 mIsSelfDestroyer(inIsSelfDestroyer) 00118 { 00119 } 00121 template<class T> 00122 CSelfDestroyPointer<T>::CSelfDestroyPointer(const CSelfDestroyPointer<T>& in): 00123 mPointer(in.mPointer), 00124 mIsSelfDestroyer(in.mIsSelfDestroyer) 00125 { 00126 }; 00127 00128 template<class T> 00129 CSelfDestroyPointer<T>::CSelfDestroyPointer(): 00130 mPointer(0), 00131 mIsSelfDestroyer(true) 00132 { 00133 } 00134 00135 template<class T> 00136 CSelfDestroyPointer<T>::~CSelfDestroyPointer() 00137 { 00138 if(mIsSelfDestroyer){ 00139 00140 delete mPointer; 00141 } 00142 } 00143 00144 00145 template<class T> 00146 void CSelfDestroyPointer<T>::setIsSelfDestroyer(bool inIsSelfDestroyer)const{ 00147 mIsSelfDestroyer= inIsSelfDestroyer; 00148 }; 00149 00150 template<class T> 00151 bool CSelfDestroyPointer<T>::isSelfDestroyer()const{ 00152 return mIsSelfDestroyer; 00153 }; 00154 00155 template<class T> 00156 void CSelfDestroyPointer<T>::unsetIsSelfDestroyer()const{ 00157 mIsSelfDestroyer=0; 00158 }; 00159 00160 template<class T> 00161 CSelfDestroyPointer<T>::operator bool()const{ 00162 return mPointer; 00163 }; 00164 00165 template<class T> 00166 CSelfDestroyPointer<T>::operator T*()const{ 00167 return mPointer; 00168 }; 00169 00170 template<class T> 00171 class CSelfClonePointer: public CSelfDestroyPointer<T>{ 00173 mutable bool mIsSelfCloner; 00175 public: 00177 CSelfClonePointer(T*, 00178 bool = true); 00180 CSelfClonePointer<T>& operator= (T* in); 00182 CSelfClonePointer<T>& operator= (const CSelfClonePointer<T>& in); 00184 CSelfClonePointer(const CSelfClonePointer<T>&); 00186 CSelfClonePointer(); 00188 operator bool()const; 00190 operator T*()const; 00191 }; 00192 00193 00194 template<class T> 00195 CSelfClonePointer<T>& CSelfClonePointer<T>::operator=(T* in){ 00196 00197 00198 CSelfDestroyPointer<T>::operator=(in); 00199 return *this; 00200 }; 00201 00202 template<class T> 00203 CSelfClonePointer<T>& CSelfClonePointer<T>::operator= (const CSelfClonePointer<T>& in){ 00204 00205 this->mPointer=in.mPointer; 00206 setIsSelfDestroyer(in.isSelfDestroyer()); 00207 return *this; 00208 }; 00209 00210 template<class T> 00211 CSelfClonePointer<T>::CSelfClonePointer(T* inPointer,bool inIsSelfCloner): 00212 CSelfDestroyPointer<T>(inPointer, 00213 inIsSelfCloner) 00214 { 00215 } 00216 template<class T> 00217 CSelfClonePointer<T>::CSelfClonePointer(): 00218 CSelfDestroyPointer<T>(0, 00219 true) 00220 { 00221 } 00222 template<class T> 00223 CSelfClonePointer<T>::CSelfClonePointer(const CSelfClonePointer<T>& in): 00224 CSelfDestroyPointer<T>(in) 00225 { 00226 if(in.mPointer && in.isSelfDestroyer()){ 00227 this->mPointer=in.mPointer->clone(); 00228 }else{ 00229 this->mPointer=in.mPointer; 00230 } 00231 } 00232 00233 #endif