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/>.
8 /** @file countedptr.hpp CCountedPtr - smart pointer implementation. */
10 #ifndef COUNTEDPTR_HPP
11 #define COUNTEDPTR_HPP
14 * CCountedPtr - simple reference counting smart pointer.
16 * One of the standard ways how to maintain object's lifetime.
18 * See http://ootips.org/yonat/4dev/smart-pointers.html for more
19 * general info about smart pointers.
21 * This class implements ref-counted pointer for objects/interfaces that
22 * support AddRef() and Release() methods.
24 template <class Tcls_
>
26 /** redefine the template argument to make it visible for derived classes */
31 /** here we hold our pointer to the target */
35 /** default (nullptr) construct or construct from a raw pointer */
36 inline CCountedPtr(Tcls
*pObj
= nullptr) : m_pT(pObj
)
41 /** copy constructor (invoked also when initializing from another smart ptr) */
42 inline CCountedPtr(const CCountedPtr
&src
) : m_pT(src
.m_pT
)
47 /** destructor releasing the reference */
54 /** add one ref to the underlying object */
57 if (m_pT
!= nullptr) m_pT
->AddRef();
61 /** release smart pointer (and decrement ref count) if not null */
64 if (m_pT
!= nullptr) {
71 /** dereference of smart pointer - const way */
72 inline const Tcls
*operator->() const
74 assert(m_pT
!= nullptr);
78 /** dereference of smart pointer - non const way */
79 inline Tcls
*operator->()
81 assert(m_pT
!= nullptr);
85 /** raw pointer casting operator - const way */
86 inline operator const Tcls
*() const
88 assert(m_pT
== nullptr);
92 /** raw pointer casting operator - non-const way */
93 inline operator Tcls
*()
98 /** operator & to support output arguments */
99 inline Tcls
** operator&()
101 assert(m_pT
== nullptr);
105 /** assignment operator from raw ptr */
106 inline CCountedPtr
& operator=(Tcls
*pT
)
112 /** assignment operator from another smart ptr */
113 inline CCountedPtr
& operator=(const CCountedPtr
&src
)
119 /** assignment operator helper */
120 inline void Assign(Tcls
*pT
);
122 /** one way how to test for nullptr value */
123 inline bool IsNull() const
125 return m_pT
== nullptr;
128 /** assign pointer w/o incrementing ref count */
129 inline void Attach(Tcls
*pT
)
135 /** detach pointer w/o decrementing ref count */
136 inline Tcls
*Detach()
144 template <class Tcls_
>
145 inline void CCountedPtr
<Tcls_
>::Assign(Tcls
*pT
)
147 /* if they are the same, we do nothing */
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
157 * Adapter wrapper for CCountedPtr like classes that can't be used directly by stl
158 * collections as item type. For example CCountedPtr has overloaded operator & which
159 * prevents using CCountedPtr in stl collections (i.e. std::list<CCountedPtr<MyType> >)
161 template <class T
> struct AdaptT
{
164 /** construct by wrapping the given object */
169 /** assignment operator */
170 T
& operator = (const T
&t
)
176 /** type-cast operator (used when AdaptT is used instead of T) */
182 /** const type-cast operator (used when AdaptT is used instead of const T) */
183 operator const T
& () const
190 * Simple counted object. Use it as base of your struct/class if you want to use
191 * basic reference counting. Your struct/class will destroy and free itself when
192 * last reference to it is released (using Release() method). The initial reference
193 * count (when it is created) is zero (don't forget AddRef() at least one time if
194 * not using CCountedPtr<T>.
196 * @see misc/countedobj.cpp for implementation.
198 struct SimpleCountedObject
{
201 SimpleCountedObject()
205 virtual ~SimpleCountedObject()
208 virtual int32_t AddRef();
209 virtual int32_t Release();
210 virtual void FinalRelease() {};
213 #endif /* COUNTEDPTR_HPP */