| 1 |
#ifndef INCLUDED_REFCOUNTAUTOPTR_H_ |
| 2 |
#define INCLUDED_REFCOUNTAUTOPTR_H_ |
| 3 |
|
| 4 |
#include <cstddef> |
| 5 |
|
| 6 |
// Implementation of auto_ptr using reference counting. |
| 7 |
// This implementation was provided and offered for inclusion in the |
| 8 |
// C++ Annotations by Jesse van den Kieboom (jesse at icecrew.nl). |
| 9 |
|
| 10 |
template<typename Type> |
| 11 |
class auto_ptr |
| 12 |
{ |
| 13 |
class auto_ptr_data |
| 14 |
{ |
| 15 |
Type *d_ptr; |
| 16 |
size_t d_refcount; |
| 17 |
|
| 18 |
public: |
| 19 |
typedef Type element_type; |
| 20 |
|
| 21 |
auto_ptr_data(element_type *ptr = 0); |
| 22 |
~auto_ptr_data(); |
| 23 |
|
| 24 |
element_type *get() const; |
| 25 |
|
| 26 |
// Refcounting |
| 27 |
size_t refcount() const; |
| 28 |
auto_ptr_data *ref(); |
| 29 |
bool unref(); |
| 30 |
element_type *release(); |
| 31 |
private: |
| 32 |
auto_ptr_data(auto_ptr_data const &other); // NI |
| 33 |
|
| 34 |
void destroy(); |
| 35 |
}; |
| 36 |
|
| 37 |
auto_ptr_data *d_data; |
| 38 |
|
| 39 |
public: |
| 40 |
/// The pointed-to type. |
| 41 |
typedef Type element_type; |
| 42 |
|
| 43 |
explicit auto_ptr(element_type *ptr = 0); |
| 44 |
auto_ptr(auto_ptr const &other); |
| 45 |
|
| 46 |
~auto_ptr(); |
| 47 |
|
| 48 |
// Assignment operator |
| 49 |
auto_ptr &operator=(auto_ptr &other); |
| 50 |
|
| 51 |
// Dereference operators |
| 52 |
element_type &operator*() const; |
| 53 |
element_type *operator->() const; |
| 54 |
|
| 55 |
// Get and release |
| 56 |
element_type *get() const; |
| 57 |
element_type *release(); |
| 58 |
void reset(element_type *ptr = 0); |
| 59 |
private: |
| 60 |
void destroy(); |
| 61 |
}; |
| 62 |
|
| 63 |
/// auto_ptr_data |
| 64 |
template <typename Type> |
| 65 |
auto_ptr<Type>::auto_ptr_data::auto_ptr_data(element_type *ptr) |
| 66 |
: |
| 67 |
d_ptr(ptr), |
| 68 |
d_refcount(!ptr ? 0 : 1) |
| 69 |
{ |
| 70 |
} |
| 71 |
|
| 72 |
template <typename Type> |
| 73 |
auto_ptr<Type>::auto_ptr_data::~auto_ptr_data() |
| 74 |
{ |
| 75 |
// Destroy our pointer |
| 76 |
if (d_refcount) |
| 77 |
destroy(); |
| 78 |
} |
| 79 |
|
| 80 |
template <typename Type> |
| 81 |
inline size_t auto_ptr<Type>::auto_ptr_data::refcount() const |
| 82 |
{ |
| 83 |
return d_refcount; |
| 84 |
} |
| 85 |
|
| 86 |
template <typename Type> |
| 87 |
inline Type *auto_ptr<Type>::auto_ptr_data::get() const |
| 88 |
{ |
| 89 |
return d_ptr; |
| 90 |
} |
| 91 |
|
| 92 |
template <typename Type> |
| 93 |
Type *auto_ptr<Type>::auto_ptr_data::release() |
| 94 |
{ |
| 95 |
// This function releases the pointer so it's no longer |
| 96 |
// maintained by this object. |
| 97 |
|
| 98 |
if (!d_refcount) |
| 99 |
return 0; |
| 100 |
|
| 101 |
--d_refcount; |
| 102 |
|
| 103 |
Type *tmp = d_ptr; |
| 104 |
d_ptr = 0; |
| 105 |
|
| 106 |
return tmp; |
| 107 |
} |
| 108 |
|
| 109 |
// Refcounting |
| 110 |
template <typename Type> |
| 111 |
typename auto_ptr<Type>::auto_ptr_data |
| 112 |
*auto_ptr<Type>::auto_ptr_data::ref() |
| 113 |
{ |
| 114 |
if (d_ptr) |
| 115 |
++d_refcount; |
| 116 |
|
| 117 |
return this; |
| 118 |
} |
| 119 |
|
| 120 |
template <typename Type> |
| 121 |
bool auto_ptr<Type>::auto_ptr_data::unref() |
| 122 |
{ |
| 123 |
if (!d_refcount) |
| 124 |
return false; |
| 125 |
|
| 126 |
--d_refcount; |
| 127 |
|
| 128 |
if (!d_refcount) |
| 129 |
destroy(); |
| 130 |
|
| 131 |
return d_refcount != 0; |
| 132 |
} |
| 133 |
|
| 134 |
template <typename Type> |
| 135 |
void auto_ptr<Type>::auto_ptr_data::destroy() |
| 136 |
{ |
| 137 |
delete d_ptr; |
| 138 |
d_ptr = 0; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/// auto_ptr |
| 143 |
template <typename Type> |
| 144 |
inline auto_ptr<Type>::auto_ptr(element_type *ptr) |
| 145 |
{ |
| 146 |
d_data = new auto_ptr_data(ptr); |
| 147 |
} |
| 148 |
|
| 149 |
template <typename Type> |
| 150 |
inline auto_ptr<Type>::auto_ptr(auto_ptr const &other) |
| 151 |
: |
| 152 |
d_data(other.d_data->ref()) |
| 153 |
{} |
| 154 |
|
| 155 |
template <typename Type> |
| 156 |
inline auto_ptr<Type>::~auto_ptr() |
| 157 |
{ |
| 158 |
destroy(); |
| 159 |
} |
| 160 |
|
| 161 |
template <typename Type> |
| 162 |
auto_ptr<Type> &auto_ptr<Type>::operator=(auto_ptr &other) |
| 163 |
{ |
| 164 |
if (&other != this) |
| 165 |
{ |
| 166 |
destroy(); |
| 167 |
|
| 168 |
d_data = other.d_data->ref(); |
| 169 |
} |
| 170 |
|
| 171 |
return *this; |
| 172 |
} |
| 173 |
|
| 174 |
template <typename Type> |
| 175 |
inline Type &auto_ptr<Type>::operator*() const |
| 176 |
{ |
| 177 |
return *d_data->get(); |
| 178 |
} |
| 179 |
|
| 180 |
template <typename Type> |
| 181 |
inline Type *auto_ptr<Type>::operator->() const |
| 182 |
{ |
| 183 |
return d_data->get(); |
| 184 |
} |
| 185 |
|
| 186 |
template <typename Type> |
| 187 |
inline Type *auto_ptr<Type>::get() const |
| 188 |
{ |
| 189 |
return d_data->get(); |
| 190 |
} |
| 191 |
|
| 192 |
// Warning: when releasing a refcounting auto pointer the data |
| 193 |
// itself is released and no longer maintained by any of the |
| 194 |
// auto pointers. All these auto pointers will now return 0. |
| 195 |
// It's your responsibility to handle this properly (and to |
| 196 |
// free the released pointer of course) |
| 197 |
template <typename Type> |
| 198 |
Type *auto_ptr<Type>::release() |
| 199 |
{ |
| 200 |
Type *ptr = d_data->release(); |
| 201 |
|
| 202 |
return ptr; |
| 203 |
} |
| 204 |
|
| 205 |
template <typename Type> |
| 206 |
void auto_ptr<Type>::reset(Type *ptr) |
| 207 |
{ |
| 208 |
// Prevent creating a wild pointer by resetting |
| 209 |
if (ptr == d_data->get()) |
| 210 |
return; |
| 211 |
|
| 212 |
// Unref current data |
| 213 |
destroy(); |
| 214 |
|
| 215 |
// Set new data |
| 216 |
d_data = new auto_ptr_data(ptr); |
| 217 |
} |
| 218 |
|
| 219 |
template <typename Type> |
| 220 |
void auto_ptr<Type>::destroy() |
| 221 |
{ |
| 222 |
if (!d_data->unref()) |
| 223 |
delete d_data; |
| 224 |
} |
| 225 |
|
| 226 |
#endif |