System documentation of the GNU Image-Finding Tool

Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members

CSelfDestroyPointer.h

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     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with this program; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 
00022 */
00023 #ifndef _CSELFDESTROYPOINTER
00024 #define _CSELFDESTROYPOINTER
00025 #include "libMRML/include/uses-declarations.h"
00026 /*
00027   destroys the element it points to or not.
00028 */
00029 
00030 template<class T>
00031 class CSelfDestroyPointer{
00033   mutable bool mIsSelfDestroyer;
00035  protected:
00036   T* mPointer;
00038 public:
00040   void resetWithoutDeleting();
00042   inline bool isSelfDestroyer()const;
00044   inline void setIsSelfDestroyer(bool inisSelfDestroyer=true)const;
00046   void unsetIsSelfDestroyer()const;
00048   T* operator= (T* inPointer);
00050   T& operator*();
00052   T const& operator*()const;
00054   T* operator -> ();
00056   T const* operator -> ()const;
00058   ~CSelfDestroyPointer();
00060   CSelfDestroyPointer(T*,
00061                       bool = true);
00063   CSelfDestroyPointer(const CSelfDestroyPointer<T>& 
00064                       inSelfDestroyPointer);
00066   CSelfDestroyPointer();
00068   operator bool()const;
00070   operator T*()const;
00071 };
00072 
00073 
00075 template<class T>
00076 void CSelfDestroyPointer<T>::resetWithoutDeleting(){
00077   mPointer=0;
00078 }
00079 
00080 template<class T>
00081 T* CSelfDestroyPointer<T>::operator=(T* inPointer){
00082 
00083   if(mIsSelfDestroyer){
00084 #ifdef _DEBUG_SELF_DESTROY_
00085     cout <<"£"<<flush;
00086 #endif
00087     delete mPointer;
00088   }
00089   mPointer=inPointer;
00090 }
00091 
00092 template<class T>
00093 T const& CSelfDestroyPointer<T>::operator *()const{
00094   return *mPointer;
00095 }
00096 
00097 template<class T>
00098 T const* CSelfDestroyPointer<T>::operator ->()const{
00099   return mPointer;
00100 }
00101 
00102 template<class T>
00103 T& CSelfDestroyPointer<T>::operator *(){
00104   return *mPointer;
00105 }
00106 
00107 template<class T>
00108 T* CSelfDestroyPointer<T>::operator ->(){
00109   return mPointer;
00110 }
00111 
00112 template<class T>
00113 CSelfDestroyPointer<T>::CSelfDestroyPointer(T* inPointer,
00114                                             bool inIsSelfDestroyer):
00115   mPointer(inPointer),
00116   mIsSelfDestroyer(inIsSelfDestroyer)
00117 {
00118 }
00120 template<class T>
00121 CSelfDestroyPointer<T>::CSelfDestroyPointer(const CSelfDestroyPointer<T>& in):
00122   mPointer(in.mPointer),
00123   mIsSelfDestroyer(in.mIsSelfDestroyer)
00124 {
00125 };
00126 
00127 template<class T>
00128 CSelfDestroyPointer<T>::CSelfDestroyPointer():
00129   mPointer(0),
00130   mIsSelfDestroyer(true)
00131 {
00132 }
00133 
00134 template<class T>
00135 CSelfDestroyPointer<T>::~CSelfDestroyPointer()
00136 {
00137   if(mIsSelfDestroyer){
00138 
00139     delete mPointer;
00140   }
00141 }
00142 
00143 
00144 template<class T>
00145 void CSelfDestroyPointer<T>::setIsSelfDestroyer(bool inIsSelfDestroyer)const{
00146   mIsSelfDestroyer= inIsSelfDestroyer;
00147 };
00148 
00149 template<class T>
00150 bool CSelfDestroyPointer<T>::isSelfDestroyer()const{
00151   return mIsSelfDestroyer;
00152 };
00153 
00154 template<class T>
00155 void CSelfDestroyPointer<T>::unsetIsSelfDestroyer()const{
00156   mIsSelfDestroyer=0;
00157 };
00158 
00159 template<class T>
00160 CSelfDestroyPointer<T>::operator bool()const{
00161   return mPointer;
00162 };
00163 
00164 template<class T>
00165 CSelfDestroyPointer<T>::operator T*()const{
00166   return mPointer;
00167 };
00168 
00169 template<class T>
00170 class CSelfClonePointer: public CSelfDestroyPointer<T>{
00172   mutable bool mIsSelfCloner;
00174 public:
00176   CSelfClonePointer(T*,
00177                     bool = true);
00179   CSelfClonePointer<T>& operator= (T* in);
00181   CSelfClonePointer<T>& operator= (const CSelfClonePointer<T>& in);
00183   CSelfClonePointer(const CSelfClonePointer<T>&);
00185   CSelfClonePointer();
00187   operator bool()const;
00189   operator T*()const;
00190 };
00191 
00192 
00193 template<class T>
00194 CSelfClonePointer<T>& CSelfClonePointer<T>::operator=(T* in){
00195 
00196 
00197   CSelfDestroyPointer<T>::operator=(in);
00198   return *this;
00199 };
00200 
00201 template<class T>
00202 CSelfClonePointer<T>& CSelfClonePointer<T>::operator= (const CSelfClonePointer<T>& in){
00203 
00204   mPointer=in.mPointer;
00205   setIsSelfDestroyer(in.isSelfDestroyer());
00206   return *this;
00207 };
00208 
00209 template<class T>
00210 CSelfClonePointer<T>::CSelfClonePointer(T* inPointer,bool inIsSelfCloner):
00211   CSelfDestroyPointer<T>(inPointer,
00212                       inIsSelfCloner)
00213 {
00214 }
00215 template<class T>
00216 CSelfClonePointer<T>::CSelfClonePointer():
00217   CSelfDestroyPointer<T>(0,
00218                       true)
00219 {
00220 }
00221 template<class T>
00222 CSelfClonePointer<T>::CSelfClonePointer(const CSelfClonePointer<T>& in):
00223   CSelfDestroyPointer<T>(in)
00224 {
00225   if(in.mPointer && in.isSelfDestroyer()){
00226     mPointer=in.mPointer->clone();
00227   }else{
00228     mPointer=in.mPointer;
00229   }  
00230 }
00231 
00232 #endif

Need for discussion? Want to contribute? Contact
help-gift@gnu.org Generated using Doxygen