Rearrange storage of reserved tracks for railway tiles
[openttd/fttd.git] / src / misc / countedptr.hpp
blob5dfc9a74c045eb1051ab82ede64f329d8e474d4f
1 /* $Id$ */
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 (NULL) construct or construct from a raw pointer */
38 inline CCountedPtr(Tcls *pObj = NULL) : m_pT(pObj) {AddRef();}
40 /** copy constructor (invoked also when initializing from another smart ptr) */
41 inline CCountedPtr(const CCountedPtr& src) : m_pT(src.m_pT) {AddRef();}
43 /** destructor releasing the reference */
44 inline ~CCountedPtr() {Release();}
46 protected:
47 /** add one ref to the underlaying object */
48 inline void AddRef() {if (m_pT != NULL) m_pT->AddRef();}
50 public:
51 /** release smart pointer (and decrement ref count) if not null */
52 inline void Release() {if (m_pT != NULL) {Tcls *pT = m_pT; m_pT = NULL; pT->Release();}}
54 /** dereference of smart pointer - const way */
55 inline const Tcls *operator -> () const {assert(m_pT != NULL); return m_pT;}
57 /** dereference of smart pointer - non const way */
58 inline Tcls *operator -> () {assert(m_pT != NULL); return m_pT;}
60 /** raw pointer casting operator - const way */
61 inline operator const Tcls*() const {assert(m_pT == NULL); return m_pT;}
63 /** raw pointer casting operator - non-const way */
64 inline operator Tcls*() {return m_pT;}
66 /** operator & to support output arguments */
67 inline Tcls** operator &() {assert(m_pT == NULL); return &m_pT;}
69 /** assignment operator from raw ptr */
70 inline CCountedPtr& operator = (Tcls *pT) {Assign(pT); return *this;}
72 /** assignment operator from another smart ptr */
73 inline CCountedPtr& operator = (const CCountedPtr& src) {Assign(src.m_pT); return *this;}
75 /** assignment operator helper */
76 inline void Assign(Tcls *pT);
78 /** one way how to test for NULL value */
79 inline bool IsNull() const {return m_pT == NULL;}
81 /** another way how to test for NULL value */
82 //inline bool operator == (const CCountedPtr& sp) const {return m_pT == sp.m_pT;}
84 /** yet another way how to test for NULL value */
85 //inline bool operator != (const CCountedPtr& sp) const {return m_pT != sp.m_pT;}
87 /** assign pointer w/o incrementing ref count */
88 inline void Attach(Tcls *pT) {Release(); m_pT = pT;}
90 /** detach pointer w/o decrementing ref count */
91 inline Tcls *Detach() {Tcls *pT = m_pT; m_pT = NULL; return pT;}
94 template <class Tcls_>
95 inline void CCountedPtr<Tcls_>::Assign(Tcls *pT)
97 /* if they are the same, we do nothing */
98 if (pT != m_pT) {
99 if (pT != NULL) pT->AddRef(); // AddRef new pointer if any
100 Tcls *pTold = m_pT; // save original ptr
101 m_pT = pT; // update m_pT to new value
102 if (pTold != NULL) pTold->Release(); // release old ptr if any
107 * Adapter wrapper for CCountedPtr like classes that can't be used directly by stl
108 * collections as item type. For example CCountedPtr has overloaded operator & which
109 * prevents using CCountedPtr in stl collections (i.e. std::list<CCountedPtr<MyType> >)
111 template <class T> struct AdaptT {
112 T m_t;
114 /** construct by wrapping the given object */
115 AdaptT(const T &t)
116 : m_t(t)
119 /** assignment operator */
120 T& operator = (const T &t)
122 m_t = t;
123 return t;
126 /** type-cast operator (used when AdaptT is used instead of T) */
127 operator T& ()
129 return m_t;
132 /** const type-cast operator (used when AdaptT is used instead of const T) */
133 operator const T& () const
135 return m_t;
141 * Simple counted object. Use it as base of your struct/class if you want to use
142 * basic reference counting. Your struct/class will destroy and free itself when
143 * last reference to it is released (using Release() method). The initial reference
144 * count (when it is created) is zero (don't forget AddRef() at least one time if
145 * not using CCountedPtr<T>.
147 * @see misc/countedobj.cpp for implementation.
149 struct SimpleCountedObject {
150 int32 m_ref_cnt;
152 SimpleCountedObject()
153 : m_ref_cnt(0)
156 virtual ~SimpleCountedObject()
159 virtual int32 AddRef();
160 virtual int32 Release();
161 virtual void FinalRelease() {};
167 #endif /* COUNTEDPTR_HPP */