4
// $Id: //poco/1.2/Foundation/include/Poco/AutoPtr.h#3 $
10
// Definition of the AutoPtr template class.
12
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
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:
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.
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.
39
#ifndef Foundation_AutoPtr_INCLUDED
40
#define Foundation_AutoPtr_INCLUDED
43
#include "Poco/Foundation.h"
44
#include "Poco/Exception.h"
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
60
/// The class must implement a public duplicate() method:
62
/// that increments the reference count by one.
63
/// The class must implement a public release() method:
65
/// that decrements the reference count by one, and,
66
/// if the reference count reaches zero, deletes the
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
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.
90
AutoPtr(C* ptr): _ptr(ptr)
94
AutoPtr(C* ptr, bool shared): _ptr(ptr)
96
if (shared && _ptr) _ptr->duplicate();
99
AutoPtr(const AutoPtr& ptr): _ptr(ptr._ptr)
101
if (_ptr) _ptr->duplicate();
104
template <class Other>
105
AutoPtr(const AutoPtr<Other>& ptr): _ptr(const_cast<Other*>(ptr.get()))
107
if (_ptr) _ptr->duplicate();
112
if (_ptr) _ptr->release();
115
AutoPtr& assign(C* ptr)
119
if (_ptr) _ptr->release();
125
AutoPtr& assign(C* ptr, bool shared)
129
if (_ptr) _ptr->release();
131
if (shared && _ptr) _ptr->duplicate();
136
AutoPtr& assign(const AutoPtr& ptr)
140
if (_ptr) _ptr->release();
142
if (_ptr) _ptr->duplicate();
147
template <class Other>
148
AutoPtr& assign(const AutoPtr<Other>& ptr)
150
if (ptr.get() != _ptr)
152
if (_ptr) _ptr->release();
153
_ptr = const_cast<Other*>(ptr.get());
154
if (_ptr) _ptr->duplicate();
159
AutoPtr& operator = (C* ptr)
164
AutoPtr& operator = (const AutoPtr& ptr)
169
template <class Other>
170
AutoPtr& operator = (const AutoPtr<Other>& ptr)
172
return assign<Other>(ptr);
175
void swap(AutoPtr& ptr)
177
std::swap(_ptr, ptr._ptr);
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());
189
Other* pOther = dynamic_cast<Other*>(_ptr);
190
return AutoPtr<Other>(pOther, true);
198
throw NullPointerException();
201
const C* operator -> () const
206
throw NullPointerException();
214
throw NullPointerException();
217
const C& operator * () const
222
throw NullPointerException();
240
operator const C* () const
245
bool operator ! () const
257
if (_ptr) _ptr->duplicate();
261
bool operator == (const AutoPtr& ptr) const
263
return _ptr == ptr._ptr;
266
bool operator == (const C* ptr) const
271
bool operator == (C* ptr) const
276
bool operator != (const AutoPtr& ptr) const
278
return _ptr != ptr._ptr;
281
bool operator != (const C* ptr) const
286
bool operator != (C* ptr) const
291
bool operator < (const AutoPtr& ptr) const
293
return _ptr < ptr._ptr;
296
bool operator < (const C* ptr) const
301
bool operator < (C* ptr) const
306
bool operator <= (const AutoPtr& ptr) const
308
return _ptr <= ptr._ptr;
311
bool operator <= (const C* ptr) const
316
bool operator <= (C* ptr) const
321
bool operator > (const AutoPtr& ptr) const
323
return _ptr > ptr._ptr;
326
bool operator > (const C* ptr) const
331
bool operator > (C* ptr) const
336
bool operator >= (const AutoPtr& ptr) const
338
return _ptr >= ptr._ptr;
341
bool operator >= (const C* ptr) const
346
bool operator >= (C* ptr) const
357
inline void swap(AutoPtr<C>& p1, AutoPtr<C>& p2)
366
#endif // Foundation_AutoPtr_INCLUDED