UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sharedptr.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <assert.h>
9 
11 public:
13  {
14  }
15 };
16 
17 template<class T>
19 public:
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  }
33 private:
34  T *_ptr;
35 };
36 
37 template<class T, class D>
39 public:
41  _ptr(ptr), _deleter(d)
42  {
43  }
45  {
46  _deleter(_ptr);
47  }
48 private:
49  T *_ptr;
51 };
52 
94 template<class T>
95 class SharedPtr {
96 #if !((__GNUC__ == 2) && (__GNUC_MINOR__ >= 95))
97  template<class T2> friend class SharedPtr;
98 #endif
99 public:
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) :
114  _refCount(new RefValue(1)), _deletion(new SharedPtrDeletionImpl<T2>(p)), _pointer(p)
115  {
116  }
117 
118  template<class T2, class D>
119  SharedPtr (T2 *p, D d) :
120  _refCount(new RefValue(1)), _deletion(new SharedPtrDeletionDeleterImpl<T2, D>(p, d)), _pointer(p)
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 
149  _refCount = r._refCount;
150  _deletion = r._deletion;
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 
163  _refCount = r._refCount;
164  _deletion = r._deletion;
165  _pointer = r._pointer;
166 
167  return *this;
168  }
169 
170  inline ReferenceType operator* () const
171  {
172  assert(_pointer);
173  return *_pointer;
174  }
175  inline PointerType operator-> () const
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 
248  RefValue refCount () const
249  {
250  return _refCount ? *_refCount : 0;
251  }
252 #if !((__GNUC__ == 2) && (__GNUC_MINOR__ >= 95))
253 private:
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 
269  RefValue *_refCount;
271  PointerType _pointer;
272 };
SharedPtr(const SharedPtr &r)
Definition: sharedptr.h:124
bool unique() const
Definition: sharedptr.h:216
T * PointerType
Definition: sharedptr.h:102
SharedPtr(T2 *p)
Definition: sharedptr.h:113
SharedPtrDeletionImpl(T *ptr)
Definition: sharedptr.h:20
void decRef()
Definition: sharedptr.h:255
~SharedPtr()
Definition: sharedptr.h:138
bool operator<(ConstPointerType other) const
Definition: sharedptr.h:181
PointerType operator->() const
Definition: sharedptr.h:175
int RefValue
Definition: sharedptr.h:100
SharedPtr(const SharedPtr< T2 > &r)
Definition: sharedptr.h:131
RefValue refCount() const
Definition: sharedptr.h:248
PointerType get() const
Definition: sharedptr.h:197
const T * ConstPointerType
Definition: sharedptr.h:103
SharedPtr(T2 *p, D d)
Definition: sharedptr.h:119
const T & ConstReferenceType
Definition: sharedptr.h:105
const GLuint *typedef void(APIENTRY *GenRenderbuffersEXT_t)(GLsizei
Definition: r_gl.h:189
void reset()
Definition: sharedptr.h:224
RefValue * _refCount
Definition: sharedptr.h:269
virtual ~SharedPtrDeletionInternal()
Definition: sharedptr.h:12
SharedPtr & operator=(const SharedPtr &r)
Definition: sharedptr.h:143
bool operator!=(const SharedPtr< T2 > &r) const
Definition: sharedptr.h:239
ReferenceType operator*() const
Definition: sharedptr.h:170
bool operator==(const SharedPtr< T2 > &r) const
Definition: sharedptr.h:233
T & ReferenceType
Definition: sharedptr.h:104
PointerType _pointer
Definition: sharedptr.h:271
SharedPtrDeletionInternal * _deletion
Definition: sharedptr.h:270
SharedPtrDeletionDeleterImpl(T *ptr, D d)
Definition: sharedptr.h:40