OpenTTD Source  1.11.0-beta2
countedptr.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #ifndef COUNTEDPTR_HPP
11 #define COUNTEDPTR_HPP
12 
24 template <class Tcls_>
25 class CCountedPtr {
27 public:
28  typedef Tcls_ Tcls;
29 
30 protected:
33 
34 public:
36  inline CCountedPtr(Tcls *pObj = nullptr) : m_pT(pObj)
37  {
38  AddRef();
39  }
40 
42  inline CCountedPtr(const CCountedPtr &src) : m_pT(src.m_pT)
43  {
44  AddRef();
45  }
46 
48  inline ~CCountedPtr()
49  {
50  Release();
51  }
52 
53 protected:
55  inline void AddRef()
56  {
57  if (m_pT != nullptr) m_pT->AddRef();
58  }
59 
60 public:
62  inline void Release()
63  {
64  if (m_pT != nullptr) {
65  Tcls *pT = m_pT;
66  m_pT = nullptr;
67  pT->Release();
68  }
69  }
70 
72  inline const Tcls *operator->() const
73  {
74  assert(m_pT != nullptr);
75  return m_pT;
76  }
77 
79  inline Tcls *operator->()
80  {
81  assert(m_pT != nullptr);
82  return m_pT;
83  }
84 
86  inline operator const Tcls*() const
87  {
88  assert(m_pT == nullptr);
89  return m_pT;
90  }
91 
93  inline operator Tcls*()
94  {
95  return m_pT;
96  }
97 
99  inline Tcls** operator&()
100  {
101  assert(m_pT == nullptr);
102  return &m_pT;
103  }
104 
107  {
108  Assign(pT);
109  return *this;
110  }
111 
113  inline CCountedPtr& operator=(const CCountedPtr &src)
114  {
115  Assign(src.m_pT);
116  return *this;
117  }
118 
120  inline void Assign(Tcls *pT);
121 
123  inline bool IsNull() const
124  {
125  return m_pT == nullptr;
126  }
127 
129  //inline bool operator == (const CCountedPtr &sp) const {return m_pT == sp.m_pT;}
130 
132  //inline bool operator != (const CCountedPtr &sp) const {return m_pT != sp.m_pT;}
133 
135  inline void Attach(Tcls *pT)
136  {
137  Release();
138  m_pT = pT;
139  }
140 
142  inline Tcls *Detach()
143  {
144  Tcls *pT = m_pT;
145  m_pT = nullptr;
146  return pT;
147  }
148 };
149 
150 template <class Tcls_>
152 {
153  /* if they are the same, we do nothing */
154  if (pT != m_pT) {
155  if (pT != nullptr) pT->AddRef(); // AddRef new pointer if any
156  Tcls *pTold = m_pT; // save original ptr
157  m_pT = pT; // update m_pT to new value
158  if (pTold != nullptr) pTold->Release(); // release old ptr if any
159  }
160 }
161 
167 template <class T> struct AdaptT {
168  T m_t;
169 
171  AdaptT(const T &t)
172  : m_t(t)
173  {}
174 
176  T& operator = (const T &t)
177  {
178  m_t = t;
179  return t;
180  }
181 
183  operator T& ()
184  {
185  return m_t;
186  }
187 
189  operator const T& () const
190  {
191  return m_t;
192  }
193 };
194 
205  int32 m_ref_cnt;
206 
208  : m_ref_cnt(0)
209  {}
210 
211  virtual ~SimpleCountedObject()
212  {}
213 
214  virtual int32 AddRef();
215  virtual int32 Release();
216  virtual void FinalRelease() {};
217 };
218 
219 #endif /* COUNTEDPTR_HPP */
AdaptT
Adapter wrapper for CCountedPtr like classes that can't be used directly by stl collections as item t...
Definition: countedptr.hpp:167
CCountedPtr::m_pT
Tcls * m_pT
here we hold our pointer to the target
Definition: countedptr.hpp:32
CCountedPtr::Assign
void Assign(Tcls *pT)
assignment operator helper
Definition: countedptr.hpp:151
SimpleCountedObject
Simple counted object.
Definition: countedptr.hpp:204
CCountedPtr::operator->
Tcls * operator->()
dereference of smart pointer - non const way
Definition: countedptr.hpp:79
CCountedPtr::Release
void Release()
release smart pointer (and decrement ref count) if not null
Definition: countedptr.hpp:62
CCountedPtr::IsNull
bool IsNull() const
one way how to test for nullptr value
Definition: countedptr.hpp:123
CCountedPtr::operator->
const Tcls * operator->() const
dereference of smart pointer - const way
Definition: countedptr.hpp:72
AdaptT::operator=
T & operator=(const T &t)
assignment operator
Definition: countedptr.hpp:176
CCountedPtr::CCountedPtr
CCountedPtr(const CCountedPtr &src)
copy constructor (invoked also when initializing from another smart ptr)
Definition: countedptr.hpp:42
AdaptT::AdaptT
AdaptT(const T &t)
construct by wrapping the given object
Definition: countedptr.hpp:171
CCountedPtr::operator=
CCountedPtr & operator=(Tcls *pT)
assignment operator from raw ptr
Definition: countedptr.hpp:106
CCountedPtr::operator=
CCountedPtr & operator=(const CCountedPtr &src)
assignment operator from another smart ptr
Definition: countedptr.hpp:113
CCountedPtr::Attach
void Attach(Tcls *pT)
another way how to test for nullptr value
Definition: countedptr.hpp:135
CCountedPtr::AddRef
void AddRef()
add one ref to the underlying object
Definition: countedptr.hpp:55
CCountedPtr::Tcls
Tcls_ Tcls
redefine the template argument to make it visible for derived classes
Definition: countedptr.hpp:28
CCountedPtr
CCountedPtr - simple reference counting smart pointer.
Definition: countedptr.hpp:25
CCountedPtr::Detach
Tcls * Detach()
detach pointer w/o decrementing ref count
Definition: countedptr.hpp:142
CCountedPtr::operator&
Tcls ** operator&()
operator & to support output arguments
Definition: countedptr.hpp:99
CCountedPtr::~CCountedPtr
~CCountedPtr()
destructor releasing the reference
Definition: countedptr.hpp:48
CCountedPtr::CCountedPtr
CCountedPtr(Tcls *pObj=nullptr)
default (nullptr) construct or construct from a raw pointer
Definition: countedptr.hpp:36