# countedptr.hpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #ifndef COUNTEDPTR_HPP
    00013 #define COUNTEDPTR_HPP
    00014
    00026 template <class Tcls_>
    00027 class CCountedPtr {
    00029 public:
    00030   typedef Tcls_ Tcls;
    00031
    00032 protected:
    00034   Tcls *m_pT;
    00035
    00036 public:
    00038   inline CCountedPtr(Tcls *pObj = NULL) : m_pT(pObj) {AddRef();}
    00039
    00041   inline CCountedPtr(const CCountedPtr& src) : m_pT(src.m_pT) {AddRef();}
    00042
    00044   inline ~CCountedPtr() {Release();}
    00045
    00046 protected:
    00048   inline void AddRef() {if (m_pT != NULL) m_pT->AddRef();}
    00049
    00050 public:
    00052   inline void Release() {if (m_pT != NULL) {Tcls *pT = m_pT; m_pT = NULL; pT->Release();}}
    00053
    00055   inline const Tcls *operator -> () const {assert(m_pT != NULL); return m_pT;}
    00056
    00058   inline Tcls *operator -> () {assert(m_pT != NULL); return m_pT;}
    00059
    00061   inline operator const Tcls*() const {assert(m_pT == NULL); return m_pT;}
    00062
    00064   inline operator Tcls*() {return m_pT;}
    00065
    00067   inline Tcls** operator &() {assert(m_pT == NULL); return &m_pT;}
    00068
    00070   inline CCountedPtr& operator = (Tcls *pT) {Assign(pT); return *this;}
    00071
    00073   inline CCountedPtr& operator = (const CCountedPtr& src) {Assign(src.m_pT); return *this;}
    00074
    00076   inline void Assign(Tcls *pT);
    00077
    00079   inline bool IsNull() const {return m_pT == NULL;}
    00080
    00082   //inline bool operator == (const CCountedPtr& sp) const {return m_pT == sp.m_pT;}
    00083
    00085   //inline bool operator != (const CCountedPtr& sp) const {return m_pT != sp.m_pT;}
    00086
    00088   inline void Attach(Tcls *pT) {Release(); m_pT = pT;}
    00089
    00091   inline Tcls *Detach() {Tcls *pT = m_pT; m_pT = NULL; return pT;}
    00092 };
    00093
    00094 template <class Tcls_>
    00095 inline void CCountedPtr<Tcls_>::Assign(Tcls *pT)
    00096 {
    00097   /* if they are the same, we do nothing */
    00098   if (pT != m_pT) {
    00099     if (pT != NULL) pT->AddRef();        // AddRef new pointer if any
    00100     Tcls *pTold = m_pT;                  // save original ptr
    00101     m_pT = pT;                           // update m_pT to new value
    00102     if (pTold != NULL) pTold->Release(); // release old ptr if any
    00103   }
    00104 }
    00105
    00111 template <class T> struct AdaptT {
    00112   T m_t;
    00113
    00115   AdaptT(const T &t)
    00116     : m_t(t)
    00117   {}
    00118
    00120   T& operator = (const T &t)
    00121   {
    00122     m_t = t;
    00123     return t;
    00124   }
    00125
    00127   operator T& ()
    00128   {
    00129     return m_t;
    00130   }
    00131
    00133   operator const T& () const
    00134   {
    00135     return m_t;
    00136   }
    00137 };
    00138
    00139
    00149 struct SimpleCountedObject {
    00150   int32 m_ref_cnt;
    00151
    00152   SimpleCountedObject()
    00153     : m_ref_cnt(0)
    00154   {}
    00155
    00156   virtual ~SimpleCountedObject()
    00157   {}
    00158
    00159   virtual int32 AddRef();
    00160   virtual int32 Release();
    00161   virtual void FinalRelease() {};
    00162 };
    00163
    00164
    00165
    00166
    00167 #endif /* COUNTEDPTR_HPP */