1 | // |
---|
2 | // AutoPtr.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/AutoPtr.h#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Core |
---|
8 | // Module: AutoPtr |
---|
9 | // |
---|
10 | // Definition of the AutoPtr template class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_AutoPtr_INCLUDED |
---|
40 | #define Foundation_AutoPtr_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/Exception.h" |
---|
45 | #include <algorithm> |
---|
46 | |
---|
47 | |
---|
48 | namespace Poco { |
---|
49 | |
---|
50 | |
---|
51 | template <class C> |
---|
52 | class AutoPtr |
---|
53 | /// AutoPtr is a "smart" pointer for classes implementing |
---|
54 | /// reference counting based garbage collection. |
---|
55 | /// To be usable with the AutoPtr template, a class must |
---|
56 | /// implement the following behaviour: |
---|
57 | /// A class must maintain a reference count. |
---|
58 | /// The constructors of the object initialize the reference |
---|
59 | /// count to one. |
---|
60 | /// The class must implement a public duplicate() method: |
---|
61 | /// void duplicate(); |
---|
62 | /// that increments the reference count by one. |
---|
63 | /// The class must implement a public release() method: |
---|
64 | /// void release() |
---|
65 | /// that decrements the reference count by one, and, |
---|
66 | /// if the reference count reaches zero, deletes the |
---|
67 | /// object. |
---|
68 | /// |
---|
69 | /// AutoPtr works in the following way: |
---|
70 | /// If an AutoPtr is assigned an ordinary pointer to |
---|
71 | /// an object (via the constructor or the assignment operator), |
---|
72 | /// it takes ownership of the object and the object's reference |
---|
73 | /// count remains unchanged. |
---|
74 | /// If the AutoPtr is assigned another AutoPtr, the |
---|
75 | /// object's reference count is incremented by one by |
---|
76 | /// calling duplicate() on its object. |
---|
77 | /// The destructor of AutoPtr calls release() on its |
---|
78 | /// object. |
---|
79 | /// AutoPtr supports dereferencing with both the -> |
---|
80 | /// and the * operator. An attempt to dereference a null |
---|
81 | /// AutoPtr results in a NullPointerException being thrown. |
---|
82 | /// AutoPtr also implements all relational operators. |
---|
83 | /// Note that AutoPtr allows casting of its encapsulated data types. |
---|
84 | { |
---|
85 | public: |
---|
86 | AutoPtr(): _ptr(0) |
---|
87 | { |
---|
88 | } |
---|
89 | |
---|
90 | AutoPtr(C* ptr): _ptr(ptr) |
---|
91 | { |
---|
92 | } |
---|
93 | |
---|
94 | AutoPtr(C* ptr, bool shared): _ptr(ptr) |
---|
95 | { |
---|
96 | if (shared && _ptr) _ptr->duplicate(); |
---|
97 | } |
---|
98 | |
---|
99 | AutoPtr(const AutoPtr& ptr): _ptr(ptr._ptr) |
---|
100 | { |
---|
101 | if (_ptr) _ptr->duplicate(); |
---|
102 | } |
---|
103 | |
---|
104 | template <class Other> |
---|
105 | AutoPtr(const AutoPtr<Other>& ptr): _ptr(const_cast<Other*>(ptr.get())) |
---|
106 | { |
---|
107 | if (_ptr) _ptr->duplicate(); |
---|
108 | } |
---|
109 | |
---|
110 | ~AutoPtr() |
---|
111 | { |
---|
112 | if (_ptr) _ptr->release(); |
---|
113 | } |
---|
114 | |
---|
115 | AutoPtr& assign(C* ptr) |
---|
116 | { |
---|
117 | if (_ptr != ptr) |
---|
118 | { |
---|
119 | if (_ptr) _ptr->release(); |
---|
120 | _ptr = ptr; |
---|
121 | } |
---|
122 | return *this; |
---|
123 | } |
---|
124 | |
---|
125 | AutoPtr& assign(C* ptr, bool shared) |
---|
126 | { |
---|
127 | if (_ptr != ptr) |
---|
128 | { |
---|
129 | if (_ptr) _ptr->release(); |
---|
130 | _ptr = ptr; |
---|
131 | if (shared && _ptr) _ptr->duplicate(); |
---|
132 | } |
---|
133 | return *this; |
---|
134 | } |
---|
135 | |
---|
136 | AutoPtr& assign(const AutoPtr& ptr) |
---|
137 | { |
---|
138 | if (&ptr != this) |
---|
139 | { |
---|
140 | if (_ptr) _ptr->release(); |
---|
141 | _ptr = ptr._ptr; |
---|
142 | if (_ptr) _ptr->duplicate(); |
---|
143 | } |
---|
144 | return *this; |
---|
145 | } |
---|
146 | |
---|
147 | template <class Other> |
---|
148 | AutoPtr& assign(const AutoPtr<Other>& ptr) |
---|
149 | { |
---|
150 | if (ptr.get() != _ptr) |
---|
151 | { |
---|
152 | if (_ptr) _ptr->release(); |
---|
153 | _ptr = const_cast<Other*>(ptr.get()); |
---|
154 | if (_ptr) _ptr->duplicate(); |
---|
155 | } |
---|
156 | return *this; |
---|
157 | } |
---|
158 | |
---|
159 | AutoPtr& operator = (C* ptr) |
---|
160 | { |
---|
161 | return assign(ptr); |
---|
162 | } |
---|
163 | |
---|
164 | AutoPtr& operator = (const AutoPtr& ptr) |
---|
165 | { |
---|
166 | return assign(ptr); |
---|
167 | } |
---|
168 | |
---|
169 | template <class Other> |
---|
170 | AutoPtr& operator = (const AutoPtr<Other>& ptr) |
---|
171 | { |
---|
172 | return assign<Other>(ptr); |
---|
173 | } |
---|
174 | |
---|
175 | void swap(AutoPtr& ptr) |
---|
176 | { |
---|
177 | std::swap(_ptr, ptr._ptr); |
---|
178 | } |
---|
179 | |
---|
180 | template <class Other> |
---|
181 | AutoPtr<Other> cast() const |
---|
182 | /// Casts the AutoPtr via a dynamic cast to the given type. |
---|
183 | /// Returns an AutoPtr containing NULL if the cast fails. |
---|
184 | /// Example: (assume class Sub: public Super) |
---|
185 | /// AutoPtr<Super> super(new Sub()); |
---|
186 | /// AutoPtr<Sub> sub = super.cast<Sub>(); |
---|
187 | /// poco_assert (sub.get()); |
---|
188 | { |
---|
189 | Other* pOther = dynamic_cast<Other*>(_ptr); |
---|
190 | return AutoPtr<Other>(pOther, true); |
---|
191 | } |
---|
192 | |
---|
193 | template <class Other> |
---|
194 | AutoPtr<Other> unsafeCast() const |
---|
195 | /// Casts the AutoPtr via a static cast to the given type. |
---|
196 | /// Example: (assume class Sub: public Super) |
---|
197 | /// AutoPtr<Super> super(new Sub()); |
---|
198 | /// AutoPtr<Sub> sub = super.unsafeCast<Sub>(); |
---|
199 | /// poco_assert (sub.get()); |
---|
200 | { |
---|
201 | Other* pOther = static_cast<Other*>(_ptr); |
---|
202 | return AutoPtr<Other>(pOther, true); |
---|
203 | } |
---|
204 | |
---|
205 | C* operator -> () |
---|
206 | { |
---|
207 | if (_ptr) |
---|
208 | return _ptr; |
---|
209 | else |
---|
210 | throw NullPointerException(); |
---|
211 | } |
---|
212 | |
---|
213 | const C* operator -> () const |
---|
214 | { |
---|
215 | if (_ptr) |
---|
216 | return _ptr; |
---|
217 | else |
---|
218 | throw NullPointerException(); |
---|
219 | } |
---|
220 | |
---|
221 | C& operator * () |
---|
222 | { |
---|
223 | if (_ptr) |
---|
224 | return *_ptr; |
---|
225 | else |
---|
226 | throw NullPointerException(); |
---|
227 | } |
---|
228 | |
---|
229 | const C& operator * () const |
---|
230 | { |
---|
231 | if (_ptr) |
---|
232 | return *_ptr; |
---|
233 | else |
---|
234 | throw NullPointerException(); |
---|
235 | } |
---|
236 | |
---|
237 | C* get() |
---|
238 | { |
---|
239 | return _ptr; |
---|
240 | } |
---|
241 | |
---|
242 | const C* get() const |
---|
243 | { |
---|
244 | return _ptr; |
---|
245 | } |
---|
246 | |
---|
247 | operator C* () |
---|
248 | { |
---|
249 | return _ptr; |
---|
250 | } |
---|
251 | |
---|
252 | operator const C* () const |
---|
253 | { |
---|
254 | return _ptr; |
---|
255 | } |
---|
256 | |
---|
257 | bool operator ! () const |
---|
258 | { |
---|
259 | return _ptr == 0; |
---|
260 | } |
---|
261 | |
---|
262 | bool isNull() const |
---|
263 | { |
---|
264 | return _ptr == 0; |
---|
265 | } |
---|
266 | |
---|
267 | C* duplicate() |
---|
268 | { |
---|
269 | if (_ptr) _ptr->duplicate(); |
---|
270 | return _ptr; |
---|
271 | } |
---|
272 | |
---|
273 | bool operator == (const AutoPtr& ptr) const |
---|
274 | { |
---|
275 | return _ptr == ptr._ptr; |
---|
276 | } |
---|
277 | |
---|
278 | bool operator == (const C* ptr) const |
---|
279 | { |
---|
280 | return _ptr == ptr; |
---|
281 | } |
---|
282 | |
---|
283 | bool operator == (C* ptr) const |
---|
284 | { |
---|
285 | return _ptr == ptr; |
---|
286 | } |
---|
287 | |
---|
288 | bool operator != (const AutoPtr& ptr) const |
---|
289 | { |
---|
290 | return _ptr != ptr._ptr; |
---|
291 | } |
---|
292 | |
---|
293 | bool operator != (const C* ptr) const |
---|
294 | { |
---|
295 | return _ptr != ptr; |
---|
296 | } |
---|
297 | |
---|
298 | bool operator != (C* ptr) const |
---|
299 | { |
---|
300 | return _ptr != ptr; |
---|
301 | } |
---|
302 | |
---|
303 | bool operator < (const AutoPtr& ptr) const |
---|
304 | { |
---|
305 | return _ptr < ptr._ptr; |
---|
306 | } |
---|
307 | |
---|
308 | bool operator < (const C* ptr) const |
---|
309 | { |
---|
310 | return _ptr < ptr; |
---|
311 | } |
---|
312 | |
---|
313 | bool operator < (C* ptr) const |
---|
314 | { |
---|
315 | return _ptr < ptr; |
---|
316 | } |
---|
317 | |
---|
318 | bool operator <= (const AutoPtr& ptr) const |
---|
319 | { |
---|
320 | return _ptr <= ptr._ptr; |
---|
321 | } |
---|
322 | |
---|
323 | bool operator <= (const C* ptr) const |
---|
324 | { |
---|
325 | return _ptr <= ptr; |
---|
326 | } |
---|
327 | |
---|
328 | bool operator <= (C* ptr) const |
---|
329 | { |
---|
330 | return _ptr <= ptr; |
---|
331 | } |
---|
332 | |
---|
333 | bool operator > (const AutoPtr& ptr) const |
---|
334 | { |
---|
335 | return _ptr > ptr._ptr; |
---|
336 | } |
---|
337 | |
---|
338 | bool operator > (const C* ptr) const |
---|
339 | { |
---|
340 | return _ptr > ptr; |
---|
341 | } |
---|
342 | |
---|
343 | bool operator > (C* ptr) const |
---|
344 | { |
---|
345 | return _ptr > ptr; |
---|
346 | } |
---|
347 | |
---|
348 | bool operator >= (const AutoPtr& ptr) const |
---|
349 | { |
---|
350 | return _ptr >= ptr._ptr; |
---|
351 | } |
---|
352 | |
---|
353 | bool operator >= (const C* ptr) const |
---|
354 | { |
---|
355 | return _ptr >= ptr; |
---|
356 | } |
---|
357 | |
---|
358 | bool operator >= (C* ptr) const |
---|
359 | { |
---|
360 | return _ptr >= ptr; |
---|
361 | } |
---|
362 | |
---|
363 | private: |
---|
364 | C* _ptr; |
---|
365 | }; |
---|
366 | |
---|
367 | |
---|
368 | template <class C> |
---|
369 | inline void swap(AutoPtr<C>& p1, AutoPtr<C>& p2) |
---|
370 | { |
---|
371 | p1.swap(p2); |
---|
372 | } |
---|
373 | |
---|
374 | |
---|
375 | } // namespace Poco |
---|
376 | |
---|
377 | |
---|
378 | #endif // Foundation_AutoPtr_INCLUDED |
---|