OpenTTD Source  20240919-master-gdf0233f4c2
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 void Attach(Tcls *pT)
130  {
131  Release();
132  m_pT = pT;
133  }
134 
136  inline Tcls *Detach()
137  {
138  Tcls *pT = m_pT;
139  m_pT = nullptr;
140  return pT;
141  }
142 };
143 
144 template <class Tcls_>
146 {
147  /* if they are the same, we do nothing */
148  if (pT != m_pT) {
149  if (pT != nullptr) pT->AddRef(); // AddRef new pointer if any
150  Tcls *pTold = m_pT; // save original ptr
151  m_pT = pT; // update m_pT to new value
152  if (pTold != nullptr) pTold->Release(); // release old ptr if any
153  }
154 }
155 
161 template <class T> struct AdaptT {
162  T m_t;
163 
165  AdaptT(const T &t)
166  : m_t(t)
167  {}
168 
170  T& operator = (const T &t)
171  {
172  m_t = t;
173  return t;
174  }
175 
177  operator T& ()
178  {
179  return m_t;
180  }
181 
183  operator const T& () const
184  {
185  return m_t;
186  }
187 };
188 
199  int32_t m_ref_cnt;
200 
202  : m_ref_cnt(0)
203  {}
204 
205  virtual ~SimpleCountedObject()
206  {}
207 
208  virtual int32_t AddRef();
209  virtual int32_t Release();
210  virtual void FinalRelease() {};
211 };
212 
213 #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:161
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:145
SimpleCountedObject
Simple counted object.
Definition: countedptr.hpp:198
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:170
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:165
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)
assign pointer w/o incrementing ref count
Definition: countedptr.hpp:129
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:136
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