/usr/include/Gyoto/GyotoSmartPointer.h is in libgyoto1-dev 0.1.0-2.
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 | /**
* \file GyotoSmartPointer.h
* \brief Reference-counting pointers
Template class Gyoto::SmartPointer<T> performs reference counting on
its pointee and makes sure the pointed to object is actually deleted
when the number of references to it drops to zero. The pointed-to
object must inherit from Gyoto::SmartPointee for this to work and be
referenced to only by SmartPointers.
@code
class Gyoto::Metric : public Gyoto::SmartPointee {...};
SmartPointer<Gyoto::Metric::Generic> ObjPtr (new Gyoto::Metric(...));
@endcode
*/
/*
Copyright 2011 Thibaut Paumard
This file is part of Gyoto.
Gyoto 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 3 of the License, or
(at your option) any later version.
Gyoto 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.
You should have received a copy of the GNU General Public License
along with Gyoto. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GyotoSmartPointer_H_
#define __GyotoSmartPointer_H_
#include "GyotoUtils.h"
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
namespace Gyoto {
class SmartPointee;
class FactoryMessenger;
template <class T> class SmartPointer;
}
#include <GyotoError.h>
#include <stddef.h>
#include <iostream>
#include <typeinfo>
/**
* \brief Can be pointed to by a SmartPointer
*
* A class can be pointed to by a SmartPointer when it inherits from
* class SmartPointee.
*
* The SmartPointee methods need to be public to be accessed by all
* the SmartPointer < T > classes. However, it is a bad idea to
* manipulate the counter directly. To protect these methods inside
* your derive object, you can do as in the following example:
*
* @code
* class Gyoto::Metric : protected Gyoto::SmartPointee
* {
* friend class Gyoto::SmartPointer<Gyoto::Metric::Generic>;
* ...
* };
* @endcode
*
*/
class Gyoto::SmartPointee
{
private:
int refCount; ///< Reference counter.
# ifdef HAVE_PTHREAD
/**
* When compiled with libpthread
*/
pthread_mutex_t mutex_; ///< A mutex
#endif
public:
SmartPointee () ;
SmartPointee (const SmartPointee&) ; ///< Copy constructor
void incRefCount () ; ///< Increment the reference counter. Warning: Don't mess with the counter.
int decRefCount () ; ///< Decrement the reference counter and return current value. Warning: Don't mess with the counter.
int getRefCount () ; ///< Get the current number of references
/**
* Various classes need to provide a subcontractor to be able to
* instanciate themselves upon order from the Factory. A
* subcontractor is a function (often a static member function)
* which accepts a pointer to a FactoryMessenger as unique
* parameter, communicates with the Factory using this messenger to
* read an XML description of the object to build, and returns this
* objet. SmartPointee::Subcontractor_t* is just generic enough a
* typedef to cast to and from other subcontractor types:
* Astrobj::Subcontractor_t, Metric::Subcontractor_t,
* Spectrum::Subcontractor_t. A subcontractor needs to be registered
* using the relevant Register() function: Astrobj::Register(),
* Metric::Register(), Spectrum::Register().
*/
typedef Gyoto::SmartPointer<Gyoto::SmartPointee>
Subcontractor_t(Gyoto::FactoryMessenger*);
///< A subcontractor builds an object upon order from the Factory
};
/**
* \brief Pointers performing reference counting
*
* Pointee must inherit from class SmartPointee.
*
* To create an object and a SmartPointer pointing to it:
*
* \code
* SmartPointer<Gyoto::Metric::Generic> ObjPtr (new Gyoto::Metric(...));
* \endcode
*
* \tparam T Sub-class of Gyoto::SmartPointee.
*/
template< class T >
class Gyoto::SmartPointer
{
private:
/**
* \brief Real pointer, don't mess with it.
*/
T *obj;
private:
/**
* \brief Decrement the reference counter. Warning: don't mess with it.
*/
void decRef ()
{
if (obj && obj->decRefCount() == 0) {
# if GYOTO_DEBUG_ENABLED
GYOTO_DEBUG_EXPR(obj);
# endif
delete obj;
obj = NULL;
}
}
public:
/**
* \brief Constructor from a standard pointer-to-class
*
* \param orig : a pointer to an instance of class T, created using new T().
*
* Example:
* \code
* SmartPointer<Gyoto::Metric::Generic> ObjPtr (new Gyoto::Metric(...)); // create SmartPointer ObjPtr
* \endcode
*/
SmartPointer (T *orig = NULL) : obj(orig)
{
if (obj)
obj->incRefCount();
}
/**
* \brief Copy constructor from same type
*
* \param orig : a SmartPointer to an instance of class T
*
* Example:
* \code
* SmartPointer<Gyoto::Metric::Generic> ObjPtr (new Gyoto::Metric(...)); // create SmartPointer ObjPtr
* SmartPointer<Gyoto::Metric::Generic> ObjPtr2 = ObjPtr; // create SmartPointer ObjPtr2
* \endcode
*
* ObjPtr and ObjPtr2 point to the same instance of class T. Copying
* increments the reference counter.
*/
SmartPointer (const SmartPointer< T > &orig)
{
obj = orig.obj;
if (obj)
obj->incRefCount ();
}
/**
* \brief Copy constructor from compatible type (used for casting)
*
* \param orig : a SmartPointer to an instance of another class U
*
* Example: MetricPtr is a SmartPoiter<Metric>, but really points to
* an instance of the child class Gyoto::Kerr:
*
* \code
* SmartPointer<Gyoto::Kerr> KerrPtr (MetricPtr);
* \endcode
*
* MetricPtr and KerrPtr point to the same instance of class
* Kerr. The methods specific to class Kerr are available only to
* KerrPtr.
*/
template<class U>
SmartPointer(const SmartPointer<U>& orig)
{
obj = dynamic_cast<T*>(const_cast<U*>(orig()));
if (obj)
obj->incRefCount ();
}
/**
* \brief Dereference operator "*"
*
* \return address of the pointed-to-object
*/
T& operator* ()
{
if (!obj)
Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator*");
return *obj;
}
/**
* \brief Dereference operator "*"
*
* \return address of the pointed-to-object
*/
const T& operator* () const
{
if (!obj)
Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator*");
return *obj;
}
/**
* \brief Dereference operator "->"
*
* Access to the pointed-to-object's members.
*/
T* operator-> ()
{
if (!obj)
Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator->");
return obj;
}
/**
* \brief Dereference operator "->" (const)
*
* Access to the pointed-to-object's members.
*/
T* operator-> () const
{
if (!obj)
Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator->");
return obj;
}
/**
* \brief Comparison operator between two SmartPointer of same kind
*/
bool operator== (const SmartPointer< T >&right) { return obj == right.obj; }
/**
* \brief Comparison operator between two SmartPointer of same kind
*/
bool operator!= (const SmartPointer< T >&right) { return obj != right.obj; }
/**
* \brief Copy a SmartPointer to another (already defined)
* SmartPointer of same kind
*/
SmartPointer< T > &operator= (SmartPointer< T > &right)
{
if (this == &right)
return *this;
if (right.obj)
right.obj->incRefCount ();
decRef ();
obj = right.obj;
return *this;
}
/**
* \brief Copy a normal pointer to an (already defined)
* SmartPointer of same kind
*/
SmartPointer< T > &operator= (T* right)
{
if (obj == right)
return *this;
decRef ();
obj = right;
if (obj) obj->incRefCount();
return *this;
}
/**
* \brief Cast SmartPointer to normal pointer
*/
// template<class U> operator U() { return obj; }
operator T*() const { return obj; }
#if 0
operator const T*() { return obj; }
/**
* \brief Check whether SmartPointer is valid
* \return 0 if SmartPointer is NULL, 1 if valid.
*/
operator bool () const { return obj != NULL; }
/**
* \brief Check whether SmartPointer is valid
* \return 1 if SmartPointer is NULL, 0 if valid.
*/
bool operator! () const { return obj == NULL; }
#endif
~SmartPointer< T > () { decRef(); }
public:
/**
* \brief Get standard, non-smart pointer to object. Use with care.
*
* This public method is needed to cast from a SmartPointer flavor
* to another. It should almost certainly never be used in user
* code, except perhaps for debugging purposes.
*
* \return usual pointer to object, T*.
*
*/
// const T* address() const { return obj; }
const T* operator()() const { return obj; }
};
#endif
|