AutoPtr.h
1
26
27#ifndef COSMO_AUTO_PTR_H
28#define COSMO_AUTO_PTR_H
29
30#include <Pt/Cosmo/Api.h>
31
32namespace Pt {
33
34namespace Cosmo {
35
38template<typename T>
39class AutoPtrImpl
40{
41 public:
42 AutoPtrImpl(T* ptr)
43 : _ptr(ptr)
44 { }
45
46 virtual ~AutoPtrImpl()
47 { }
48
49 void reset(T* ptr = 0)
50 {
51 if(_ptr)
52 onReset(_ptr);
53
54 _ptr = ptr;
55 }
56
57 void destroy()
58 {
59 onDestroy();
60 }
61
62 T* get()
63 {
64 return _ptr;
65 }
66
67 protected:
68 virtual void onReset(T* ptr) = 0;
69
70 virtual void onDestroy() = 0;
71
72 private:
73 T* _ptr;
74};
75
78template <typename Y, typename B, typename D>
79class AutoPtrData : public AutoPtrImpl<B>
80{
81 public:
82 AutoPtrData(Y* res, D del)
83 : AutoPtrImpl<B>(res)
84 , _del(del)
85 { }
86
87 protected:
88 virtual void onReset(B* ptr)
89 {
90 _del( static_cast<Y*>(ptr) );
91 }
92
93 virtual void onDestroy()
94 {
95 delete this;
96 }
97
98 private:
99 D _del;
100};
101
104template<typename T>
105struct AutoPtrRef
106{
107 AutoPtrImpl<T>* ptr;
108
109 explicit AutoPtrRef(AutoPtrImpl<T>* ap)
110 : ptr(ap)
111 { }
112};
113
121template<typename T>
123{
124 private:
125 template <typename Y>
126 class DefaultDelete
127 {
128 public:
129 void operator()(Y* res)
130 {
131 delete res;
132 }
133 };
134
135 public:
136 typedef T element_type;
137
138 public:
141 : _ptr(0)
142 { }
143
145 template <typename Y>
146 explicit AutoPtr(Y* ptr)
147 : _ptr(0)
148 {
149 DefaultDelete<Y> deleter;
150 _ptr = new AutoPtrData<Y, T, DefaultDelete<Y> >(ptr, deleter);
151 }
152
154 template <typename Y, typename Deleter>
155 AutoPtr(Y* ptr, Deleter deleter)
156 : _ptr(0)
157 {
158 _ptr = new AutoPtrData<Y, T, Deleter>(ptr, deleter);
159 }
160
162 AutoPtr(AutoPtrRef<T> rhs)
163 : _ptr(rhs.ptr)
164 { }
165
168 : _ptr(other._ptr)
169 { other._ptr = 0; }
170
173 {
174 reset();
175 }
176
179 {
180 if( this != &other )
181 {
182 reset();
183 _ptr = other._ptr;
184 other._ptr = 0;
185 }
186 return *this;
187 }
188
190 AutoPtr& operator= (AutoPtrRef<T> rhs)
191 {
192 this->reset();
193 _ptr = rhs.ptr;
194 return *this;
195 }
196
198 template<class Y>
199 operator AutoPtrRef<Y>()
200 {
201 AutoPtrImpl<Y>* tmp = _ptr;
202 _ptr = 0;
203 return AutoPtrRef<Y>(tmp);
204 }
205
207 T* get() const
208 {
209 return _ptr ? _ptr->get() : 0;
210 }
211
213 T& operator*() const
214 {
215 return *_ptr->get();
216 }
217
219 T* operator->() const
220 {
221 return _ptr->get();
222 }
223
225 bool operator !() const
226 {
227 return _ptr == 0;
228 }
229
231 operator bool() const
232 {
233 return _ptr != 0;
234 }
235
238 {
239 if( ! _ptr )
240 return 0;
241
242 T* tmp = _ptr->get();
243 _ptr->destroy();
244 _ptr = 0;
245
246 return tmp;
247 }
248
250 void reset()
251 {
252 if(_ptr)
253 {
254 _ptr->reset();
255 _ptr->destroy();
256 _ptr = 0;
257 }
258 }
259
261 template <typename Y>
262 void reset(Y* ptr)
263 {
264 if( ! ptr )
265 {
266 reset();
267 return;
268 }
269
270 if(_ptr)
271 {
272 _ptr->reset(ptr);
273 }
274 else
275 {
276 DefaultDelete<Y> deleter;
277 _ptr = new AutoPtrData<Y, T, DefaultDelete<Y> >(ptr, deleter);
278 }
279 }
280
281 private:
282 AutoPtr(AutoPtr &p);
283
285
286 private:
287 AutoPtrImpl<T>* _ptr;
288};
289
294template <typename T>
296{
297 return AutoPtr<T>( AutoPtrRef<T>(p) );
298}
299
304template <typename T, typename Y>
305bool operator==(const AutoPtr<T>& a, const AutoPtr<Y>& b)
306{ return a.get() == b.get(); }
307
312template <typename T, typename Y>
313bool operator==(const AutoPtr<T>& a, const Y* b)
314{ return a.get() == b; }
315
320template <typename T, typename Y>
321bool operator!=(const AutoPtr<T>& a, const AutoPtr<Y>& b)
322{ return a.get() != b.get(); }
323
328template <typename T, typename Y>
329bool operator!=(const AutoPtr<T>& a, const Y* b)
330{ return a.get() != b; }
331
336template <typename T, typename Y>
337bool operator<(const AutoPtr<T>& a, const AutoPtr<Y>& b)
338{ return a.get() < b.get(); }
339
344template <typename T, typename Y>
345bool operator<(const AutoPtr<T>& a, const Y* b)
346{ return a.get() < b; }
347
348} // namespace Cosmo
349
350} // namespace Pt
351
352#endif // include guard
Owning smart pointer.
Definition AutoPtr.h:123
AutoPtr(Y *ptr)
Constructor.
Definition AutoPtr.h:146
T & operator*() const
Access value.
Definition AutoPtr.h:213
bool operator!=(const AutoPtr< T > &a, const Y *b)
Equality comparison operator.
Definition AutoPtr.h:329
T * release()
Release ownership.
Definition AutoPtr.h:237
AutoPtr(Y *ptr, Deleter deleter)
Constructor.
Definition AutoPtr.h:155
bool operator==(const AutoPtr< T > &a, const Y *b)
Equality comparison operator.
Definition AutoPtr.h:313
~AutoPtr()
Destructor.
Definition AutoPtr.h:172
AutoPtr & operator=(AutoPtr &&other)
Move assignment operator.
Definition AutoPtr.h:178
AutoPtr(AutoPtr &&other)
Move constructor.
Definition AutoPtr.h:167
bool operator!() const
Returns true if nullptr.
Definition AutoPtr.h:225
bool operator!=(const AutoPtr< T > &a, const AutoPtr< Y > &b)
Equality comparison operator.
Definition AutoPtr.h:321
bool operator<(const AutoPtr< T > &a, const Y *b)
Less-than comparison operator.
Definition AutoPtr.h:345
AutoPtr< T > move(AutoPtr< T > &p)
Move to other instance.
Definition AutoPtr.h:295
bool operator==(const AutoPtr< T > &a, const AutoPtr< Y > &b)
Equality comparison operator.
Definition AutoPtr.h:305
T * operator->() const
Access value.
Definition AutoPtr.h:219
bool operator<(const AutoPtr< T > &a, const AutoPtr< Y > &b)
Less-than comparison operator.
Definition AutoPtr.h:337
void reset()
Reset value.
Definition AutoPtr.h:250
void reset(Y *ptr)
Reset value.
Definition AutoPtr.h:262
T * get() const
Access value.
Definition AutoPtr.h:207
AutoPtr()
Default constructor.
Definition AutoPtr.h:140
AutoPtr(AutoPtrRef< T > rhs)
Copy constructor (C++03 move via AutoPtrRef).
Definition AutoPtr.h:162
Cosmo Component Model.
Definition Api.h:53
Core module.
Definition Allocator.h:33