OpenTTD: src/misc/countedptr.hpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

countedptr.hpp
Go to the documentation of this file.
1 /* $Id: countedptr.hpp 27363 2015-08-08 13:19:38Z alberth $ */
2
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 */
9
12 #ifndef COUNTEDPTR_HPP
13 #define COUNTEDPTR_HPP
14
26 template <class Tcls_>
27 class CCountedPtr {
29 public:
30 typedef Tcls_ Tcls;
31
32 protected:
34 Tcls *m_pT;
35
36 public:
38 inline CCountedPtr(Tcls *pObj = NULL) : m_pT(pObj)
39 {
40 AddRef();
41 }
42
44 inline CCountedPtr(const CCountedPtr &src) : m_pT(src.m_pT)
45 {
46 AddRef();
47 }
48
50 inline ~CCountedPtr()
51 {
52 Release();
53 }
54
55 protected:
57 inline void AddRef()
58 {
59 if (m_pT != NULL) m_pT->AddRef();
60 }
61
62 public:
64 inline void Release()
65 {
66 if (m_pT != NULL) {
67 Tcls *pT = m_pT;
68 m_pT = NULL;
69 pT->Release();
70 }
71 }
72
74 inline const Tcls *operator->() const
75 {
76 assert(m_pT != NULL);
77 return m_pT;
78 }
79
81 inline Tcls *operator->()
82 {
83 assert(m_pT != NULL);
84 return m_pT;
85 }
86
88 inline operator const Tcls*() const
89 {
90 assert(m_pT == NULL);
91 return m_pT;
92 }
93
95 inline operator Tcls*()
96 {
97 return m_pT;
98 }
99
101 inline Tcls** operator&()
102 {
103 assert(m_pT == NULL);
104 return &m_pT;
105 }
106
108 inline CCountedPtr& operator=(Tcls *pT)
109 {
110 Assign(pT);
111 return *this;
112 }
113
115 inline CCountedPtr& operator=(const CCountedPtr &src)
116 {
117 Assign(src.m_pT);
118 return *this;
119 }
120
122 inline void Assign(Tcls *pT);
123
125 inline bool IsNull() const
126 {
127 return m_pT == NULL;
128 }
129
131 //inline bool operator == (const CCountedPtr &sp) const {return m_pT == sp.m_pT;}
132
134 //inline bool operator != (const CCountedPtr &sp) const {return m_pT != sp.m_pT;}
135
137 inline void Attach(Tcls *pT)
138 {
139 Release();
140 m_pT = pT;
141 }
142
144 inline Tcls *Detach()
145 {
146 Tcls *pT = m_pT;
147 m_pT = NULL;
148 return pT;
149 }
150 };
151
152 template <class Tcls_>
153 inline void CCountedPtr<Tcls_>::Assign(Tcls *pT)
154 {
155 /* if they are the same, we do nothing */
156 if (pT != m_pT) {
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
161 }
162 }
163
169 template <class T> struct AdaptT {
170 T m_t;
171
173 AdaptT(const T &t)
174 : m_t(t)
175 {}
176
178 T& operator = (const T &t)
179 {
180 m_t = t;
181 return t;
182 }
183
185 operator T& ()
186 {
187 return m_t;
188 }
189
191 operator const T& () const
192 {
193 return m_t;
194 }
195 };
196
206 struct SimpleCountedObject {
207 int32 m_ref_cnt;
208
209 SimpleCountedObject()
210 : m_ref_cnt(0)
211 {}
212
213 virtual ~SimpleCountedObject()
214 {}
215
216 virtual int32 AddRef();
217 virtual int32 Release();
218 virtual void FinalRelease() {};
219 };
220
221 #endif /* COUNTEDPTR_HPP */