smartpointer.hh

Go to the documentation of this file.
00001 // $Id: smartpointer.hh 5736 2009-12-04 13:13:10Z mblatt $
00002 
00003 #ifndef DUNE_SMARTPOINTER_HH
00004 #define DUNE_SMARTPOINTER_HH
00005 
00006 #include<cassert>
00007 
00008 #warning This file is deprecated.  Please use shared_ptr.hh instead!
00009 
00016 namespace Dune
00017 {
00029     template<class T>
00030     class SmartPointer
00031     {
00032     public:
00038         typedef T MemberType;
00039 
00043         inline SmartPointer();
00044         
00051         inline SmartPointer(T * pointer);
00052         
00057         inline SmartPointer(const SmartPointer<T>& pointer);
00058         
00062         inline ~SmartPointer();
00063 
00065         inline SmartPointer& operator=(const SmartPointer<T>& pointer);
00066         
00068         inline MemberType& operator*();
00069         
00071         inline MemberType* operator->();
00072       
00074         inline const MemberType& operator*() const;
00075         
00077         inline const MemberType* operator->() const;
00078 
00083         inline void deallocate();
00084         int count() const;
00085     private:
00087         class PointerRep
00088         {
00089             friend class SmartPointer<MemberType>;
00091             int count_;
00093             MemberType * rep_;
00095             PointerRep() : count_(0), rep_(0) {}
00097             PointerRep(MemberType * p) : count_(1), rep_(p) {}
00099             ~PointerRep() { delete rep_; }
00100         } *rep_;
00101     };
00102 
00103     template<class T>
00104     inline SmartPointer<T>::SmartPointer(T * p)
00105     {
00106         rep_ = new PointerRep(p);
00107     }
00108 
00109     template<class T>
00110     inline SmartPointer<T>::SmartPointer()
00111     {
00112         rep_ = new PointerRep;
00113     }
00114 
00115     template<class T>
00116     inline SmartPointer<T>::SmartPointer(const SmartPointer<T>& other) : rep_(other.rep_)
00117     {
00118         ++(rep_->count_);
00119     }
00120 
00121     template<class T>
00122     inline SmartPointer<T>& SmartPointer<T>::operator=(const SmartPointer<T>& other)
00123     {
00124         (other.rep_->count_)++;
00125         if(rep_!=0 && --(rep_->count_)<=0) delete rep_;
00126         rep_ = other.rep_;
00127         return *this;
00128     }
00129 
00130     template<class T>
00131     inline SmartPointer<T>::~SmartPointer()
00132     {
00133         if(rep_!=0 && --(rep_->count_)==0){
00134             delete rep_;
00135             rep_=0;
00136         }
00137     }
00138 
00139     template<class T>
00140     inline T& SmartPointer<T>::operator*()
00141     {
00142         return *(rep_->rep_);
00143     }
00144 
00145     template<class T>
00146     inline T *SmartPointer<T>::operator->()
00147     {
00148         return rep_->rep_;
00149     }
00150 
00151     template<class T>
00152     inline const T& SmartPointer<T>::operator*() const
00153     {
00154         return *(rep_->rep_);
00155     }
00156 
00157     template<class T>
00158     inline const T *SmartPointer<T>::operator->() const
00159     {
00160         return rep_->rep_;
00161     }
00162     
00163     template<class T>
00164     inline int SmartPointer<T>::count() const
00165     {
00166         return rep_->count_;
00167     }
00168 
00169     template<class T>
00170     inline void SmartPointer<T>::deallocate()
00171     {
00172         assert(rep_!=0 && rep_->count_==1);
00173         delete rep_;
00174         rep_=0;
00175     }
00177 }
00178 #endif
Generated on Mon Apr 26 10:45:21 2010 for dune-common by  doxygen 1.6.3