/usr/include/bobcat/autoptr is in libbobcat-dev 2.20.01-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | #include <algorithm>
#include <bobcat/refcount>
namespace FBB
{
template<typename Data>
class AutoPtr
{
class RefData: public RefCount
{
Data *d_data;
size_t d_size; // 0: single pointer, otherwise array
public:
RefData();
RefData(Data *d_data, size_t size);
RefData(RefData const &other) = delete;
virtual ~RefData();
Data *get(int idx);
Data *releaseOne(); // Return one allocated
// copy, lose share count
Data *releaseAll(); // Return allocated data
// lose all shares
void reset(Data *data, size_t size = 0);// reset to data[size]
// or data if size == 0
private:
virtual RefCount *clone() const;
void destroy();
};
RefData *d_data;
int d_base; // base is used with arrays. It provides a base
// location from where index operations start to
// count.
public:
AutoPtr(); // Default constructor
explicit AutoPtr(Data *data); // Receive one allocated Data element
AutoPtr(Data *data, size_t size); // Array of n Data elements
AutoPtr(AutoPtr &other); // Copy constructor
AutoPtr(AutoPtr &&other); // Move constructor
~AutoPtr();
AutoPtr &operator=(AutoPtr &other);
Data &operator*();
Data *operator->();
Data *get(); // same as operator->
Data *release(); // lose one reference count
Data *releaseAll(); // lose all references
void reset(Data *data, size_t size = 0); // lose one reference,
// reassign data[size] or
// data if size == 0
void resetAll(Data *data, size_t size = 0); // new data[size] or data
// if size == 0
Data &operator[](int idx);
AutoPtr &operator+=(int idx);
AutoPtr &operator-=(int idx);
AutoPtr operator+(int idx);
AutoPtr operator-(int idx);
private:
void destroy();
void copy(AutoPtr &other);
};
// RefData members:
template<typename Data>
AutoPtr<Data>::RefData::RefData()
:
d_data(0),
d_size(0)
{}
template<typename Data>
AutoPtr<Data>::RefData::RefData(Data *data, size_t size)
:
d_data(data),
d_size(size)
{}
template<typename Data>
inline AutoPtr<Data>::RefData::~RefData()
{
destroy();
}
template<typename Data>
RefCount *AutoPtr<Data>::RefData::clone() const
{
Data *data;
if (!d_size)
data = new Data(*d_data);
else
{
data = new Data[d_size];
std::copy(d_data, d_data + d_size, data);
}
return new RefData(d_data, d_size);
}
template<typename Data>
inline Data *AutoPtr<Data>::RefData::get(int idx)
{
return d_data + idx;
}
template<typename Data>
Data *AutoPtr<Data>::RefData::releaseOne()
{
Data *ret;
if (refcount() == 1)
{
ret = d_data;
d_data = 0;
}
else
{
if (!d_size)
ret = new Data(*d_data);
else
{
ret = new Data[d_size];
for (size_t idx = 0; idx < d_size; ++idx)
ret[idx] = d_data[idx];
}
}
release();
return ret;
}
template<typename Data>
Data *AutoPtr<Data>::RefData::releaseAll()
{
Data *ret = d_data;
d_data = 0;
release();
return ret;
}
template<typename Data>
void AutoPtr<Data>::RefData::reset(Data *data, size_t size)
{
destroy();
d_data = data;
d_size = size;
}
template<typename Data>
void AutoPtr<Data>::RefData::destroy()
{
if (d_size == 0)
delete d_data;
else
delete [] d_data; // maybe add facility for double pointers,
// which will delete the individual data
// elements?
}
// AutoPtr members
template<typename Data> // Default constructor
AutoPtr<Data>::AutoPtr()
:
d_data(new RefData()),
d_base(0)
{}
template<typename Data> // One data element
AutoPtr<Data>::AutoPtr(Data *data)
:
d_data(new RefData(data, 0)),
d_base(0)
{}
template<typename Data> // Array of `size' data elements
AutoPtr<Data>::AutoPtr(Data *data, size_t size)
:
d_data(new RefData(data, size)),
d_base(0)
{}
template<typename Data> // Copy constructor
inline AutoPtr<Data>::AutoPtr(AutoPtr &other)
{
copy(other);
}
template<typename Data> // Move constructor
inline AutoPtr<Data>::AutoPtr(AutoPtr &&tmp)
:
d_data(tmp.d_datao),
d_base(tmp.d_base)
{
tmp.d_data = 0;
}
template<typename Data> // destructor
inline AutoPtr<Data>::~AutoPtr()
{
if (d_data)
destroy();
}
template<typename Data> // Assignment operator
AutoPtr<Data> &AutoPtr<Data>::operator=(AutoPtr &other)
{
if (this != &other)
{
destroy();
copy(other);
}
return *this;
}
template<typename Data> // Dereference operator
inline Data &AutoPtr<Data>::operator*()
{
return *d_data->get(d_base);
}
template<typename Data> // Pointer operator
inline Data *AutoPtr<Data>::operator->()
{
return d_data->get(d_base);
}
template<typename Data> // get() member
inline Data *AutoPtr<Data>::get()
{
return d_data->get(d_base);
}
template<typename Data> // lose one share count, return alloc. copy
inline Data *AutoPtr<Data>::release()
{
return d_data->releaseOne();
}
template<typename Data> // lose all shares, return alloc. data
inline Data *AutoPtr<Data>::releaseAll()
{
return d_data->releaseAll();
}
template<typename Data> // lose one share count, point to alloc. copy
void AutoPtr<Data>::reset(Data *data, size_t size)
{
d_data->release();
d_data = new RefData(data, size);
d_base = 0;
}
template<typename Data> // redefine data for all sharing objects
void AutoPtr<Data>::resetAll(Data *data, size_t size)
{
d_data->reset(data, size);
d_base = 0;
}
template<typename Data>
void AutoPtr<Data>::copy(AutoPtr &other)
{
d_data = RefData::share(other.d_data);
d_base = other.d_base;
}
template<typename Data>
inline void AutoPtr<Data>::destroy()
{
d_data->release();
}
template<typename Data>
inline Data &AutoPtr<Data>::operator[](int idx)
{
return *d_data->get(d_base + idx);
}
template<typename Data>
AutoPtr<Data> &AutoPtr<Data>::operator+=(int idx)
{
d_base += idx;
return *this;
}
template<typename Data>
inline AutoPtr<Data> AutoPtr<Data>::operator+(int idx)
{
return AutoPtr<Data>(*this) += idx;
}
template<typename Data>
inline AutoPtr<Data> operator+(int idx, AutoPtr<Data> &rhs)
{
return AutoPtr<Data>(rhs) += idx;
}
template<typename Data>
inline AutoPtr<Data> &AutoPtr<Data>::operator-=(int idx)
{
return *this += -idx;
}
template<typename Data>
inline AutoPtr<Data> AutoPtr<Data>::operator-(int idx)
{
return AutoPtr<Data>(*this) -= idx;
}
} // FBB
|