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

ref.h

Go to the documentation of this file.
00001 #ifndef REF_H
00002 #define REF_H
00003 
00004 
00034 template<class T>
00035 class ref {
00037 friend T;
00038 public:
00040   ref(): t_(0), n_(new unsigned int(1)) {}
00042   ref(const ref<T>& r): t_(r.t_), n_(r.n_) { ++*n_; }
00044   ~ref() { if (--*n_) return; delete t_; delete n_; }
00045 
00047   operator  bool() const  { return (t_!=0); }
00049   T&  operator*() const { return *t_; }
00051   T*  operator->() const { return t_; }    
00053   ref<T>&  operator=(const ref<T>& r) { 
00054              if (this==&r) return *this;
00055              if (--*n_==0) { 
00056                delete t_; delete n_;
00057                }
00058              t_ = r.t_;  n_=r.n_; ++*n_; return *this;  
00059              }
00061   bool operator==(const ref<T>& r) const {
00062         if (t_ == r.t_) return true;
00063         if (t_)
00064           if (r.t_)
00065             return (*t_ == *r);
00066           else
00067             return false;
00068         // r.t_ != t_ == 0
00069         return false;
00070         }
00072   bool operator<(const ref<T>& r) const {
00073         // null is minimal element in this implementation
00074         if (t_ == r.t_) return false;
00075         if (t_)
00076           if (r.t_)
00077             return (*t_ < *r);
00078           else
00079             return false;
00080         // t_ == 0, r.t_ != 0
00081         return true;
00082         }
00083 protected:
00085   ref(T* t): t_(t), n_(new unsigned int(1)) {}
00086 private:
00087   T*            t_;
00088   unsigned int* n_;
00089 };
00090 
00091 #endif

FSM [ August, 2001]