1 /* $Id: countedptr.hpp 24900 2013-01-08 22:46:42Z planetmaker $ */
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file countedptr.hpp CCountedPtr - smart pointer implementation. */
12 #ifndef COUNTEDPTR_HPP
13 #define COUNTEDPTR_HPP
16 * CCountedPtr - simple reference counting smart pointer.
18 * One of the standard ways how to maintain object's lifetime.
20 * See http://ootips.org/yonat/4dev/smart-pointers.html for more
21 * general info about smart pointers.
23 * This class implements ref-counted pointer for objects/interfaces that
24 * support AddRef() and Release() methods.
26 template <class Tcls_
>
28 /** redefine the template argument to make it visible for derived classes */
33 /** here we hold our pointer to the target */
37 /** default (NULL) construct or construct from a raw pointer */
38 inline CCountedPtr(Tcls
*pObj
= NULL
) : m_pT(pObj
)
43 /** copy constructor (invoked also when initializing from another smart ptr) */
44 inline CCountedPtr(const CCountedPtr
&src
) : m_pT(src
.m_pT
)
49 /** destructor releasing the reference */
56 /** add one ref to the underlaying object */
59 if (m_pT
!= NULL
) m_pT
->AddRef();
63 /** release smart pointer (and decrement ref count) if not null */
73 /** dereference of smart pointer - const way */
74 inline const Tcls
*operator->() const
80 /** dereference of smart pointer - non const way */
81 inline Tcls
*operator->()
87 /** raw pointer casting operator - const way */
88 inline operator const Tcls
*() const
94 /** raw pointer casting operator - non-const way */
95 inline operator Tcls
*()
100 /** operator & to support output arguments */
101 inline Tcls
** operator&()
103 assert(m_pT
== NULL
);
107 /** assignment operator from raw ptr */
108 inline CCountedPtr
& operator=(Tcls
*pT
)
114 /** assignment operator from another smart ptr */
115 inline CCountedPtr
& operator=(const CCountedPtr
&src
)
121 /** assignment operator helper */
122 inline void Assign(Tcls
*pT
);
124 /** one way how to test for NULL value */
125 inline bool IsNull() const
130 /** another way how to test for NULL value */
131 //inline bool operator == (const CCountedPtr &sp) const {return m_pT == sp.m_pT;}
133 /** yet another way how to test for NULL value */
134 //inline bool operator != (const CCountedPtr &sp) const {return m_pT != sp.m_pT;}
136 /** assign pointer w/o incrementing ref count */
137 inline void Attach(Tcls
*pT
)
143 /** detach pointer w/o decrementing ref count */
144 inline Tcls
*Detach()
152 template <class Tcls_
>
153 inline void CCountedPtr
<Tcls_
>::Assign(Tcls
*pT
)
155 /* if they are the same, we do nothing */
157 if (pT
!= NULL
) pT
->AddRef(); // AddRef new pointer if any
158 Tcls
*pTold
= m_pT
; // save original ptr
159 m_pT
= pT
; // update m_pT to new value
160 if (pTold
!= NULL
) pTold
->Release(); // release old ptr if any
165 * Adapter wrapper for CCountedPtr like classes that can't be used directly by stl
166 * collections as item type. For example CCountedPtr has overloaded operator & which
167 * prevents using CCountedPtr in stl collections (i.e. std::list<CCountedPtr<MyType> >)
169 template <class T
> struct AdaptT
{
172 /** construct by wrapping the given object */
177 /** assignment operator */
178 T
& operator = (const T
&t
)
184 /** type-cast operator (used when AdaptT is used instead of T) */
190 /** const type-cast operator (used when AdaptT is used instead of const T) */
191 operator const T
& () const
198 * Simple counted object. Use it as base of your struct/class if you want to use
199 * basic reference counting. Your struct/class will destroy and free itself when
200 * last reference to it is released (using Release() method). The initial reference
201 * count (when it is created) is zero (don't forget AddRef() at least one time if
202 * not using CCountedPtr<T>.
204 * @see misc/countedobj.cpp for implementation.
206 struct SimpleCountedObject
{
209 SimpleCountedObject()
213 virtual ~SimpleCountedObject()
216 virtual int32
AddRef();
217 virtual int32
Release();
218 virtual void FinalRelease() {};
221 #endif /* COUNTEDPTR_HPP */