UFO: Alien Invasion
Loading...
Searching...
No Matches
sharedptr.h
Go to the documentation of this file.
1
5
6#pragma once
7
8#include <assert.h>
9
11public:
13 {
14 }
15};
16
17template<class T>
19public:
21 _ptr(ptr)
22 {
23 }
25 {
26 // Checks if the supplied type is not just a plain
27 // forward definition, taken from boost::checked_delete
28 // This makes the user really aware what he tries to do
29 // when using this with an incomplete type.
30 typedef char completeCheck[sizeof(T) ? 1 : -1];(void)sizeof(completeCheck);
31 delete _ptr;
32 }
33private:
34 T *_ptr;
35};
36
37template<class T, class D>
39public:
41 _ptr(ptr), _deleter(d)
42 {
43 }
48private:
49 T *_ptr;
51};
52
94template<class T>
95class SharedPtr {
96#if !((__GNUC__ == 2) && (__GNUC_MINOR__ >= 95))
97 template<class T2> friend class SharedPtr;
98#endif
99public:
100 typedef int RefValue;
101 typedef T ValueType;
102 typedef T *PointerType;
103 typedef const T *ConstPointerType;
104 typedef T &ReferenceType;
105 typedef const T &ConstReferenceType;
106
108 _refCount(0), _deletion(0), _pointer(0)
109 {
110 }
111
112 template<class T2>
113 explicit SharedPtr (T2 *p) :
115 {
116 }
117
118 template<class T2, class D>
119 SharedPtr (T2 *p, D d) :
121 {
122 }
123
124 SharedPtr (const SharedPtr &r) :
126 {
127 if (_refCount)
128 ++(*_refCount);
129 }
130 template<class T2>
133 {
134 if (_refCount)
135 ++(*_refCount);
136 }
137
139 {
140 decRef();
141 }
142
144 {
145 if (r._refCount)
146 ++(*r._refCount);
147 decRef();
148
151 _pointer = r._pointer;
152
153 return *this;
154 }
155
156 template<class T2>
158 {
159 if (r._refCount)
160 ++(*r._refCount);
161 decRef();
162
165 _pointer = r._pointer;
166
167 return *this;
168 }
169
171 {
172 assert(_pointer);
173 return *_pointer;
174 }
176 {
177 assert(_pointer);
178 return _pointer;
179 }
180
181 inline bool operator< (ConstPointerType other) const
182 {
183 return *_pointer < *other;
184 }
185
186 inline bool operator< (ConstReferenceType other) const
187 {
188 return *_pointer < other;
189 }
190
197 inline PointerType get () const
198 {
199 return _pointer;
200 }
201
206 inline operator bool () const
207 {
208 return _pointer != 0;
209 }
210
216 inline bool unique () const
217 {
218 return refCount() == 1;
219 }
220
224 void reset ()
225 {
226 decRef();
227 _deletion = 0;
228 _refCount = 0;
229 _pointer = 0;
230 }
231
232 template<class T2>
233 bool operator== (const SharedPtr<T2> &r) const
234 {
235 return _pointer == r.get();
236 }
237
238 template<class T2>
239 bool operator!= (const SharedPtr<T2> &r) const
240 {
241 return _pointer != r.get();
242 }
243
249 {
250 return _refCount ? *_refCount : 0;
251 }
252#if !((__GNUC__ == 2) && (__GNUC_MINOR__ >= 95))
253private:
254#endif
255 void decRef ()
256 {
257 if (_refCount) {
258 --(*_refCount);
259 if (!*_refCount) {
260 delete _refCount;
261 delete _deletion;
262 _deletion = 0;
263 _refCount = 0;
264 _pointer = 0;
265 }
266 }
267 }
268
272};
SharedPtrDeletionDeleterImpl(T *ptr, D d)
Definition sharedptr.h:40
SharedPtrDeletionImpl(T *ptr)
Definition sharedptr.h:20
virtual ~SharedPtrDeletionInternal()
Definition sharedptr.h:12
const uiNode & ConstReferenceType
Definition sharedptr.h:105
bool operator<(ConstPointerType other) const
Definition sharedptr.h:181
SharedPtrDeletionInternal * _deletion
Definition sharedptr.h:270
PointerType get() const
Definition sharedptr.h:197
PointerType _pointer
Definition sharedptr.h:271
bool operator!=(const SharedPtr< T2 > &r) const
Definition sharedptr.h:239
bool operator==(const SharedPtr< T2 > &r) const
Definition sharedptr.h:233
SharedPtr & operator=(const SharedPtr &r)
Definition sharedptr.h:143
void reset()
Definition sharedptr.h:224
RefValue * _refCount
Definition sharedptr.h:269
void decRef()
Definition sharedptr.h:255
const uiNode * ConstPointerType
Definition sharedptr.h:103
SharedPtr(T2 *p, D d)
Definition sharedptr.h:119
RefValue refCount() const
Definition sharedptr.h:248
PointerType operator->() const
Definition sharedptr.h:175
SharedPtr(T2 *p)
Definition sharedptr.h:113
SharedPtr(const SharedPtr< T2 > &r)
Definition sharedptr.h:131
bool unique() const
Definition sharedptr.h:216
uiNode & ReferenceType
Definition sharedptr.h:104
friend class SharedPtr
Definition sharedptr.h:97
SharedPtr(const SharedPtr &r)
Definition sharedptr.h:124
ReferenceType operator*() const
Definition sharedptr.h:170
QGL_EXTERN void(APIENTRY *qglActiveTexture)(GLenum texture)