/usr/include/ETL/_smart_ptr.h is in etl-dev 0.04.19-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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | /* ========================================================================
** Extended Template and Library
** Template Smart Pointer Implementation
** $Id$
**
** Copyright (c) 2002 Robert B. Quattlebaum Jr.
**
** This package is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License as
** published by the Free Software Foundation; either version 2 of
** the License, or (at your option) any later version.
**
** This package is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details.
**
** === N O T E S ===========================================================
**
** This is an internal header file, included by other ETL headers.
** You should not attempt to use it directly.
**
** ========================================================================= */
/* === S T A R T =========================================================== */
#ifndef __ETL__SMART_PTR_H
#define __ETL__SMART_PTR_H
/* === H E A D E R S ======================================================= */
#include <cassert>
#include "_ref_count.h"
/* === M A C R O S ========================================================= */
/* === T Y P E D E F S ===================================================== */
/* === C L A S S E S & S T R U C T S ======================================= */
_ETL_BEGIN_NAMESPACE
template <class T>
struct generic_deleter
{
void operator()(T* x)const { delete x; }
};
template <class T>
struct array_deleter
{
void operator()(T* x)const { delete [] x; }
};
// ========================================================================
/*! \class smart_ptr _smart_ptr.h ETL/smart_ptr
** \brief Object Smart Pointer
** \see loose_smart_ptr
** \writeme
*/
template <class T, class D=generic_deleter<T> >
class smart_ptr
{
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef int count_type;
typedef int size_type;
typedef D destructor_type;
#ifdef DOXYGEN_SHOULD_SKIP_THIS // #ifdef is not a typo
private:
#endif
value_type *obj; //!< \internal Pointer to object
reference_counter refcount;
public:
// Private constructor for convenience
smart_ptr(value_type* obj,reference_counter refcount):obj(obj),refcount(refcount) { }
//! Default constructor - empty smart_ptr
smart_ptr():obj(0),refcount(false) {}
//! Constructor that constructs from a pointer to new object
/*! A new smart_ptr is created with a pointer
to a newly allocated object. We need
to be explicit with this so we don't
accidently have two smart_ptrs for one
object -- that would be bad. */
explicit smart_ptr(value_type* x):obj(x),refcount(x?true:false) { }
//! Template copy constructor
/*! This template constructor allows us to cast
smart_ptrs much like we would pointers. */
#ifdef _WIN32
template <class U>
smart_ptr(const smart_ptr<U> &x):obj((pointer)&*x.obj),refcount(x.refcount())
{ }
#endif
//! Default copy constructor
/*! The template above is not good enough
for all compilers. We need to explicitly
define the copy constructor for this
class to work on those compilers. */
smart_ptr(const smart_ptr<value_type> &x):obj(x.obj),refcount(x.refcount) { }
explicit smart_ptr(const value_type &x):obj(new value_type(x)) { }
//! smart_ptr is released on deletion
~smart_ptr() { if(refcount.unique()) destructor_type()(obj); }
//! Template Assignment operator
template <class U> const smart_ptr<value_type> &
operator=(const smart_ptr<U> &x)
{
if(x.get()==obj)
return *this;
reset();
if(x.obj)
{
obj=(pointer)x.get();
refcount=x.refcount;
}
return *this;
}
//! Assignment operator
const smart_ptr<value_type> &
operator=(const smart_ptr<value_type> &x)
{
if(x.get()==obj)
return *this;
reset();
if(x.obj)
{
obj=(pointer)x.get();
refcount=x.refcount;
}
return *this;
}
//! smart_ptr reset procedure
void
reset()
{
if(obj)
{
if(refcount.unique()) destructor_type()(obj);
refcount.detach();
obj=0;
}
}
void spawn() { operator=(smart_ptr(new T)); }
//! Returns number of instances
const count_type& count()const { return refcount; }
//! Returns true if there is only one instance of the object
bool unique()const { return refcount.unique(); }
//! Returns a constant handle to our object
smart_ptr<const value_type> constant() { return *this; }
reference operator*()const { assert(obj); return *obj; }
pointer operator->()const { assert(obj); return obj; }
operator smart_ptr<const value_type>()const
{ return smart_ptr<const value_type>(static_cast<const_pointer>(obj)); }
//! static_cast<> wrapper
template <class U> static
smart_ptr<T> cast_static(const smart_ptr<U> &x)
{ if(!x)return NULL; return smart_ptr<T>(static_cast<T*>(x.get()),x.refcount); }
//! dynamic_cast<> wrapper
template <class U> static
smart_ptr<T> cast_dynamic(const smart_ptr<U> &x)
{ if(!x)return 0; return smart_ptr<T>(dynamic_cast<T*>(x.get()),x.refcount); }
//! const_cast<> wrapper
template <class U> static
smart_ptr<T> cast_const(const smart_ptr<U> &x)
{ if(!x)return 0; return smart_ptr<T>(const_cast<T*>(x.get()),x.refcount); }
pointer get()const { return obj; }
//! More explicit bool cast
operator bool()const { return obj!=0; }
bool operator!()const { return !obj; }
//! Overloaded cast operator -- useful for implicit casts
template <class U>
operator smart_ptr<U>()
{
// This next line should provide a syntax check
// to make sure that this cast makes sense.
// If it doesn't, this should have a compiler error.
// Otherwise, it should get optimized right out
// of the code.
//(U*)obj;
return *reinterpret_cast<smart_ptr<U>*>(this);
}
}; // END of template class smart_ptr
// ========================================================================
/*! \class loose_smart_ptr _smart_ptr.h ETL/smart_ptr
** \brief Loose Object Smart Pointer
** \see smart_ptr
** \writeme
*/
template <class T>
class loose_smart_ptr
{
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef int count_type;
typedef int size_type;
private:
value_type *obj; //!< \internal Pointer to object
weak_reference_counter refcount; //!< \internal Pointer to object's reference counter
public:
//! Default constructor - empty smart_ptr
loose_smart_ptr():obj(0),refcount(0) {}
//! Default copy constructor
loose_smart_ptr(const loose_smart_ptr<value_type> &x):obj(x.get()),refcount(x.refcount) { }
loose_smart_ptr(const smart_ptr<value_type> &x):obj(x.get()),refcount(x.refcount) { }
void reset() { obj=0,refcount=0; }
operator smart_ptr<value_type>()
{
return smart_ptr<value_type>(static_cast<pointer>(obj),refcount);
}
operator smart_ptr<const value_type>()
{
return smart_ptr<const value_type>(static_cast<const_pointer>(obj),refcount);
}
//! Returns number of instances
const count_type& count()const { return refcount; }
bool unique()const { return refcount.unique(); }
reference operator*()const { assert(obj); return *obj; }
pointer operator->()const { assert(obj); return obj; }
pointer get()const { return obj; }
bool operator!()const { return !obj; }
};
template <class T,class U> bool
operator==(const smart_ptr<T> &lhs,const smart_ptr<U> &rhs)
{ return (lhs.get()==rhs.get()); }
template <class T,class U> bool
operator==(const loose_smart_ptr<T> &lhs,const loose_smart_ptr<U> &rhs)
{ return (lhs.get()==rhs.get()); }
template <class T,class U> bool
operator==(const smart_ptr<T> &lhs,const loose_smart_ptr<U> &rhs)
{ return (lhs.get()==rhs.get()); }
template <class T,class U> bool
operator==(const loose_smart_ptr<T> &lhs,const smart_ptr<U> &rhs)
{ return (lhs.get()==rhs.get()); }
template <class T> bool
operator==(const smart_ptr<T> &lhs,const T *rhs)
{ return (lhs.get()==rhs); }
template <class T> bool
operator==(const loose_smart_ptr<T> &lhs,const T *rhs)
{ return (lhs.get()==rhs); }
template <class T> bool
operator==(const T *lhs,const smart_ptr<T> &rhs)
{ return (lhs==rhs.get()); }
template <class T> bool
operator==(const T *lhs,const loose_smart_ptr<T> &rhs)
{ return (lhs==rhs.get()); }
template <class T,class U> bool
operator!=(const smart_ptr<T> &lhs,const smart_ptr<U> &rhs)
{ return (lhs.get()!=rhs.get()); }
template <class T,class U> bool
operator!=(const loose_smart_ptr<T> &lhs,const loose_smart_ptr<U> &rhs)
{ return (lhs.get()!=rhs.get()); }
template <class T,class U> bool
operator!=(const smart_ptr<T> &lhs,const loose_smart_ptr<U> &rhs)
{ return (lhs.get()!=rhs.get()); }
template <class T,class U> bool
operator!=(const loose_smart_ptr<T> &lhs,const smart_ptr<U> &rhs)
{ return (lhs.get()!=rhs.get()); }
template <class T> bool
operator!=(const smart_ptr<T> &lhs,const T *rhs)
{ return (lhs.get()!=rhs); }
template <class T> bool
operator!=(const loose_smart_ptr<T> &lhs,const T *rhs)
{ return (lhs.get()!=rhs); }
template <class T> bool
operator!=(const T *lhs,const smart_ptr<T> &rhs)
{ return (lhs!=rhs.get()); }
template <class T> bool
operator!=(const T *lhs,const loose_smart_ptr<T> &rhs)
{ return (lhs!=rhs.get()); }
template <class T,class U> bool
operator<(const smart_ptr<T> &lhs,const smart_ptr<U> &rhs)
{ return (lhs.get()<rhs.get()); }
template <class T,class U> bool
operator<(const loose_smart_ptr<T> &lhs,const loose_smart_ptr<U> &rhs)
{ return (lhs.get()<rhs.get()); }
template <class T,class U> bool
operator<(const smart_ptr<T> &lhs,const loose_smart_ptr<U> &rhs)
{ return (lhs.get()<rhs.get()); }
template <class T,class U> bool
operator<(const loose_smart_ptr<T> &lhs,const smart_ptr<U> &rhs)
{ return (lhs.get()<rhs.get()); }
template <class T> bool
operator<(const smart_ptr<T> &lhs,const T *rhs)
{ return (lhs.get()<rhs); }
template <class T> bool
operator<(const loose_smart_ptr<T> &lhs,const T *rhs)
{ return (lhs.get()<rhs); }
template <class T> bool
operator<(const T *lhs,const smart_ptr<T> &rhs)
{ return (lhs<rhs.get()); }
template <class T> bool
operator<(const T *lhs,const loose_smart_ptr<T> &rhs)
{ return (lhs<rhs.get()); }
_ETL_END_NAMESPACE
/* === E N D =============================================================== */
#endif
|