smartpointer.hh

Go to the documentation of this file.
00001 // $Id: smartpointer.hh 4726 2006-10-13 11:42:38Z oliver $
00002 
00003 #ifndef DUNE_SMARTPOINTER_HH
00004 #define DUNE_SMARTPOINTER_HH
00005 #include<iostream>
00006 
00013 namespace Dune
00014 {
00026     template<class T>
00027     class SmartPointer
00028     {
00029     public:
00035         typedef T MemberType;
00036 
00040         inline SmartPointer();
00041         
00046         inline SmartPointer(const SmartPointer<T>& pointer);
00047         
00051         inline ~SmartPointer();
00052 
00054         inline SmartPointer& operator=(const SmartPointer<T>& pointer);
00055         
00057         inline MemberType& operator*();
00058         
00060         inline MemberType* operator->();
00061       
00063         inline const MemberType& operator*() const;
00064         
00066         inline const MemberType* operator->() const;
00067 
00072       inline void deallocate();
00073         int count() const;
00074     private:
00076         class PointerRep
00077         {
00078             friend class SmartPointer<MemberType>;
00080             int count_;
00082             MemberType rep_;
00084             PointerRep(const MemberType& rep): count_(1), rep_(rep){}
00085         } *rep_;
00086     };
00087 
00088     template<class T>
00089     inline SmartPointer<T>::SmartPointer()
00090     {
00091         rep_ = new PointerRep(MemberType());
00092     }
00093 
00094     template<class T>
00095     inline SmartPointer<T>::SmartPointer(const SmartPointer<T>& other) : rep_(other.rep_)
00096     {
00097         ++(rep_->count_);
00098     }
00099 
00100     template<class T>
00101     inline SmartPointer<T>& SmartPointer<T>::operator=(const SmartPointer<T>& other)
00102     {
00103         (other.rep_->count_)++;
00104         if(rep_!=0 && --(rep_->count_)<=0) delete rep_;
00105         rep_ = other.rep_;
00106         return *this;
00107     }
00108 
00109     template<class T>
00110     inline SmartPointer<T>::~SmartPointer()
00111     {
00112         if(rep_!=0 && --(rep_->count_)==0){
00113             delete rep_;
00114             rep_=0;
00115         }
00116     }
00117 
00118     template<class T>
00119     inline T& SmartPointer<T>::operator*()
00120     {
00121         return rep_->rep_;
00122     }
00123 
00124     template<class T>
00125     inline T *SmartPointer<T>::operator->()
00126     {
00127         return &(rep_->rep_);
00128     }
00129 
00130     template<class T>
00131     inline const T& SmartPointer<T>::operator*()const
00132     {
00133         return rep_->rep_;
00134     }
00135 
00136     template<class T>
00137     inline const T *SmartPointer<T>::operator->() const
00138     {
00139         return &(rep_->rep_);
00140     }
00141     
00142     template<class T>
00143     inline int SmartPointer<T>::count() const
00144     {
00145         return rep_->count_;
00146     }
00147 
00148     template<class T>
00149     inline void SmartPointer<T>::deallocate()
00150     {
00151       assert(rep_!=0 && rep_->count_==1);
00152       delete rep_;
00153       rep_=0;
00154     }
00156 }
00157 #endif

Generated on Tue Jul 28 22:27:50 2009 for dune-common by  doxygen 1.5.6