Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. template<class T>
  2. class Ref
  3. {
  4. public:
  5.     Ref()
  6.     {
  7.         obj_ = NULL;
  8.     }
  9.  
  10.     Ref(T* Obj)
  11.     {
  12.         obj_ = Obj;
  13.     }
  14.     Ref(const Ref<T> & o)
  15.     {
  16.         obj_ = o.obj_;
  17.     }
  18.  
  19.     ~Ref()
  20.     {
  21.         delete_();
  22.     }
  23.  
  24.     inline T* Get()
  25.     {
  26.         return obj_;
  27.     }
  28.  
  29.     inline void operator =(T* Obj)
  30.     {
  31.         delete_();
  32.         obj_ = Obj;
  33.     }
  34.  
  35.     inline void operator =(const Ref<T> & o)
  36.     {
  37.         delete_();
  38.         obj_ = o.obj_;
  39.     }
  40.  
  41.     inline T* operator ->()
  42.     {
  43.         return obj_;
  44.     }
  45.  
  46.     inline const T* operator ->() const
  47.     {
  48.         return obj_;
  49.     }
  50.  
  51.     inline operator bool() const
  52.     {
  53.         return obj_ != NULL;
  54.     }
  55.  
  56.     inline T& operator *()
  57.     {
  58.         return *obj_;
  59.     }
  60.  
  61.     inline const T& operator *() const
  62.     {
  63.         return *obj_;
  64.     }
  65.  
  66.     inline bool operator == (const Ref & o) const
  67.     {
  68.         return obj_ == o.obj_;
  69.     }
  70.  
  71.     inline bool operator != (const Ref & o) const
  72.     {
  73.         return obj_ != o.obj_;
  74.     }
  75.  
  76.     inline bool operator != (void * o) const
  77.     {
  78.         return obj_ != o;
  79.     }
  80.  
  81.     inline bool operator != (int o) const
  82.     {
  83.         return obj_ != reinterpret_cast<void *>(o);
  84.     }
  85.  
  86. private:
  87.     inline void delete_()
  88.     {
  89.         if(obj_)
  90.         {
  91.             delete obj_;
  92.         }
  93.     }
  94.  
  95.     T* obj_;
  96. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement