src/core/ptr.h
changeset 567 6fb98941c36f
parent 544 cbc4158d47c9
child 569 31a7c6fc511e
equal deleted inserted replaced
566:a4ef066d1185 567:6fb98941c36f
    49   class Tester {
    49   class Tester {
    50   private:
    50   private:
    51     void operator delete (void *);
    51     void operator delete (void *);
    52   };
    52   };
    53   friend class Ptr<const T>;
    53   friend class Ptr<const T>;
       
    54   void Acquire (void) const;
    54 public:
    55 public:
    55   /**
    56   /**
    56    * Create an empty smart pointer
    57    * Create an empty smart pointer
    57    */
    58    */
    58   Ptr ();
    59   Ptr ();
   107   inline friend bool operator != (Ptr<T1> const &lhs, T2 const *rhs);
   108   inline friend bool operator != (Ptr<T1> const &lhs, T2 const *rhs);
   108   // allow if (0 != sp)
   109   // allow if (0 != sp)
   109   template <typename T1, typename T2>
   110   template <typename T1, typename T2>
   110   inline friend bool operator != (T1 const *lhs, Ptr<T2> &rhs);
   111   inline friend bool operator != (T1 const *lhs, Ptr<T2> &rhs);
   111 
   112 
       
   113   // allow if (sp0 == sp1)
       
   114   template <typename T1, typename T2>
       
   115   inline friend bool operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
       
   116   // allow if (sp0 != sp1)
       
   117   template <typename T1, typename T2>
       
   118   inline friend bool operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs);
       
   119 
   112   template <typename T1, typename T2>
   120   template <typename T1, typename T2>
   113   inline friend Ptr<T1> const_pointer_cast (Ptr<T2> const&p);
   121   inline friend Ptr<T1> const_pointer_cast (Ptr<T2> const&p);
   114 
   122 
   115 
   123 
   116 };
   124 };
       
   125 
       
   126 template <typename T>
       
   127 void 
       
   128 Ptr<T>::Acquire (void) const
       
   129 {
       
   130   if (m_ptr != 0)
       
   131     {
       
   132       m_ptr->Ref ();
       
   133     }  
       
   134 }
   117 
   135 
   118 template <typename T>
   136 template <typename T>
   119 Ptr<T>::Ptr ()
   137 Ptr<T>::Ptr ()
   120   : m_ptr (0)
   138   : m_ptr (0)
   121 {}
   139 {}
   122 
   140 
   123 template <typename T>
   141 template <typename T>
   124 Ptr<T>::Ptr (T *ptr) 
   142 Ptr<T>::Ptr (T *ptr) 
   125   : m_ptr (ptr)
   143   : m_ptr (ptr)
   126 {}
   144 {
       
   145   Acquire ();
       
   146 }
   127 
   147 
   128 template <typename T>
   148 template <typename T>
   129 Ptr<T>::Ptr (Ptr const&o) 
   149 Ptr<T>::Ptr (Ptr const&o) 
   130   : m_ptr (o.m_ptr)
   150   : m_ptr (o.Peek ())
   131 {
   151 {
   132   if (m_ptr != 0) 
   152   Acquire ();
   133     {
       
   134       m_ptr->Ref();
       
   135     }
       
   136 }
   153 }
   137 template <typename T>
   154 template <typename T>
   138 template <typename U>
   155 template <typename U>
   139 Ptr<T>::Ptr (Ptr<U> const &o)
   156 Ptr<T>::Ptr (Ptr<U> const &o)
   140   : m_ptr (o.m_ptr)
   157   : m_ptr (o.Peek ())
   141 {
   158 {
   142   if (m_ptr != 0) 
   159   Acquire ();
   143     {
       
   144       NS_ASSERT (o.m_ptr != 0);
       
   145       m_ptr->Ref();
       
   146     }
       
   147 }
   160 }
   148 
   161 
   149 template <typename T>
   162 template <typename T>
   150 Ptr<T>::~Ptr () 
   163 Ptr<T>::~Ptr () 
   151 {
   164 {
   158 template <typename T>
   171 template <typename T>
   159 Ptr<T> &
   172 Ptr<T> &
   160 Ptr<T>::operator = (Ptr const& o) 
   173 Ptr<T>::operator = (Ptr const& o) 
   161 {
   174 {
   162   if (&o == this)
   175   if (&o == this)
   163     return *this;
   176     {
       
   177       return *this;
       
   178     }
   164   if (m_ptr != 0) 
   179   if (m_ptr != 0) 
   165     {
   180     {
   166       m_ptr->Unref();
   181       m_ptr->Unref();
   167     }
   182     }
   168   m_ptr = o.m_ptr;
   183   m_ptr = o.m_ptr;
   169   if (m_ptr != 0) 
   184   Acquire ();
   170     {
       
   171       m_ptr->Ref();
       
   172     }
       
   173   return *this;
   185   return *this;
   174 }
   186 }
   175 
   187 
   176 template <typename T>
   188 template <typename T>
   177 T *
   189 T *
   182 
   194 
   183 template <typename T>
   195 template <typename T>
   184 T * 
   196 T * 
   185 Ptr<T>::Get () const
   197 Ptr<T>::Get () const
   186 {
   198 {
   187   m_ptr->Ref();
   199   Acquire ();
   188   return m_ptr;
   200   return m_ptr;
   189 }
   201 }
   190 
   202 
   191 template <typename T>
   203 template <typename T>
   192 T *
   204 T *
   245 {
   257 {
   246   return lhs != rhs.m_ptr;
   258   return lhs != rhs.m_ptr;
   247 }
   259 }
   248 
   260 
   249 template <typename T1, typename T2>
   261 template <typename T1, typename T2>
       
   262 bool 
       
   263 operator == (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
       
   264 {
       
   265   return lhs.Get () == rhs.Get ();
       
   266 }
       
   267 template <typename T1, typename T2>
       
   268 bool 
       
   269 operator != (Ptr<T1> const &lhs, Ptr<T2> const &rhs)
       
   270 {
       
   271   return lhs.Get () != rhs.Get ();
       
   272 }
       
   273 
       
   274 
       
   275 template <typename T1, typename T2>
   250 Ptr<T1>
   276 Ptr<T1>
   251 const_pointer_cast (Ptr<T2> const&p)
   277 const_pointer_cast (Ptr<T2> const&p)
   252 {
   278 {
   253   return Ptr<T1> (const_cast<T1 *> (p.m_ptr));
   279   return Ptr<T1> (const_cast<T1 *> (p.m_ptr));
   254 }
   280 }