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 24900 2013-01-08 22:46:42Z planetmaker $ */
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) {AddRef();}
39
41 inline CCountedPtr(const CCountedPtr& src) : m_pT(src.m_pT) {AddRef();}
42
44 inline ~CCountedPtr() {Release();}
45
46 protected:
48 inline void AddRef() {if (m_pT != NULL) m_pT->AddRef();}
49
50 public:
52 inline void Release() {if (m_pT != NULL) {Tcls *pT = m_pT; m_pT = NULL; pT->Release();}}
53
55 inline const Tcls *operator -> () const {assert(m_pT != NULL); return m_pT;}
56
58 inline Tcls *operator -> () {assert(m_pT != NULL); return m_pT;}
59
61 inline operator const Tcls*() const {assert(m_pT == NULL); return m_pT;}
62
64 inline operator Tcls*() {return m_pT;}
65
67 inline Tcls** operator &() {assert(m_pT == NULL); return &m_pT;}
68
70 inline CCountedPtr& operator = (Tcls *pT) {Assign(pT); return *this;}
71
73 inline CCountedPtr& operator = (const CCountedPtr& src) {Assign(src.m_pT); return *this;}
74
76 inline void Assign(Tcls *pT);
77
79 inline bool IsNull() const {return m_pT == NULL;}
80
82 //inline bool operator == (const CCountedPtr& sp) const {return m_pT == sp.m_pT;}
83
85 //inline bool operator != (const CCountedPtr& sp) const {return m_pT != sp.m_pT;}
86
88 inline void Attach(Tcls *pT) {Release(); m_pT = pT;}
89
91 inline Tcls *Detach() {Tcls *pT = m_pT; m_pT = NULL; return pT;}
92 };
93
94 template <class Tcls_>
95 inline void CCountedPtr<Tcls_>::Assign(Tcls *pT)
96 {
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
103 }
104 }
105
111 template <class T> struct AdaptT {
112 T m_t;
113
115 AdaptT(const T &t)
116 : m_t(t)
117 {}
118
120 T& operator = (const T &t)
121 {
122 m_t = t;
123 return t;
124 }
125
127 operator T& ()
128 {
129 return m_t;
130 }
131
133 operator const T& () const
134 {
135 return m_t;
136 }
137 };
138
139
149 struct SimpleCountedObject {
150 int32 m_ref_cnt;
151
152 SimpleCountedObject()
153 : m_ref_cnt(0)
154 {}
155
156 virtual ~SimpleCountedObject()
157 {}
158
159 virtual int32 AddRef();
160 virtual int32 Release();
161 virtual void FinalRelease() {};
162 };
163
164
165
166
167 #endif /* COUNTEDPTR_HPP */