Show snow on depot tiles. (Looks weird my ass)
[openttd-joker.git] / src / misc / countedptr.hpp
blobe099c04f147792a520314ab88d35bf60cf10d401
1 /* $Id: countedptr.hpp 24900 2013-01-08 22:46:42Z planetmaker $ */
3 /*
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/>.
8 */
10 /** @file countedptr.hpp CCountedPtr - smart pointer implementation. */
12 #ifndef COUNTEDPTR_HPP
13 #define COUNTEDPTR_HPP
15 /**
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_>
27 class CCountedPtr {
28 /** redefine the template argument to make it visible for derived classes */
29 public:
30 typedef Tcls_ Tcls;
32 protected:
33 /** here we hold our pointer to the target */
34 Tcls *m_pT;
36 public:
37 /** default (nullptr) construct or construct from a raw pointer */
38 inline CCountedPtr(Tcls *pObj = nullptr) : m_pT(pObj)
40 AddRef();
43 /** copy constructor (invoked also when initializing from another smart ptr) */
44 inline CCountedPtr(const CCountedPtr &src) : m_pT(src.m_pT)
46 AddRef();
49 /** destructor releasing the reference */
50 inline ~CCountedPtr()
52 Release();
55 protected:
56 /** add one ref to the underlaying object */
57 inline void AddRef()
59 if (m_pT != nullptr) m_pT->AddRef();
62 public:
63 /** release smart pointer (and decrement ref count) if not null */
64 inline void Release()
66 if (m_pT != nullptr) {
67 Tcls *pT = m_pT;
68 m_pT = nullptr;
69 pT->Release();
73 /** dereference of smart pointer - const way */
74 inline const Tcls *operator->() const
76 assert(m_pT != nullptr);
77 return m_pT;
80 /** dereference of smart pointer - non const way */
81 inline Tcls *operator->()
83 assert(m_pT != nullptr);
84 return m_pT;
87 /** raw pointer casting operator - const way */
88 inline operator const Tcls*() const
90 assert(m_pT == nullptr);
91 return m_pT;
94 /** raw pointer casting operator - non-const way */
95 inline operator Tcls*()
97 return m_pT;
100 /** operator & to support output arguments */
101 inline Tcls** operator&()
103 assert(m_pT == nullptr);
104 return &m_pT;
107 /** assignment operator from raw ptr */
108 inline CCountedPtr& operator=(Tcls *pT)
110 Assign(pT);
111 return *this;
114 /** assignment operator from another smart ptr */
115 inline CCountedPtr& operator=(const CCountedPtr &src)
117 Assign(src.m_pT);
118 return *this;
121 /** assignment operator helper */
122 inline void Assign(Tcls *pT);
124 /** one way how to test for nullptr value */
125 inline bool IsNull() const
127 return m_pT == nullptr;
130 /** another way how to test for nullptr value */
131 //inline bool operator == (const CCountedPtr &sp) const {return m_pT == sp.m_pT;}
133 /** yet another way how to test for nullptr 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)
139 Release();
140 m_pT = pT;
143 /** detach pointer w/o decrementing ref count */
144 inline Tcls *Detach()
146 Tcls *pT = m_pT;
147 m_pT = nullptr;
148 return pT;
152 template <class Tcls_>
153 inline void CCountedPtr<Tcls_>::Assign(Tcls *pT)
155 /* if they are the same, we do nothing */
156 if (pT != m_pT) {
157 if (pT != nullptr) 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 != nullptr) 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 {
170 T m_t;
172 /** construct by wrapping the given object */
173 AdaptT(const T &t)
174 : m_t(t)
177 /** assignment operator */
178 T& operator = (const T &t)
180 m_t = t;
181 return t;
184 /** type-cast operator (used when AdaptT is used instead of T) */
185 operator T& ()
187 return m_t;
190 /** const type-cast operator (used when AdaptT is used instead of const T) */
191 operator const T& () const
193 return m_t;
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 {
207 int32 m_ref_cnt;
209 SimpleCountedObject()
210 : m_ref_cnt(0)
213 virtual ~SimpleCountedObject()
216 virtual int32 AddRef();
217 virtual int32 Release();
218 virtual void FinalRelease() {};
221 #endif /* COUNTEDPTR_HPP */