GyotoSmartPointer.h
Go to the documentation of this file.
/*
Copyright 2011-2014, 2016, 2020 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>
#include <string>
#include <vector>
class Gyoto::SmartPointee
{
private:
int refCount;
# ifdef HAVE_PTHREAD
pthread_mutex_t mutex_;
#endif
public:
SmartPointee () ;
virtual ~SmartPointee() ;
SmartPointee (const SmartPointee&) ;
void incRefCount () ;
int decRefCount () ;
int getRefCount () ;
typedef Gyoto::SmartPointer<Gyoto::SmartPointee>
Subcontractor_t(Gyoto::FactoryMessenger*, std::vector<std::string> const &);
};
template< class T >
class Gyoto::SmartPointer
{
private:
T *obj;
private:
void decRef ()
{
if (obj && obj->decRefCount() == 0) {
# if GYOTO_DEBUG_ENABLED
GYOTO_DEBUG_EXPR(obj);
# endif
delete obj;
obj = NULL;
}
}
public:
SmartPointer (T *orig = NULL) : obj(orig)
{
if (obj)
obj->incRefCount();
}
SmartPointer (const SmartPointer< T > &orig)
{
obj = orig.obj;
if (obj)
obj->incRefCount ();
}
template<class U>
SmartPointer(const SmartPointer<U>& orig)
{
obj = dynamic_cast<T*>(const_cast<U*>(orig()));
if (obj)
obj->incRefCount ();
}
T& operator* ()
{
if (!obj)
Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator*");
return *obj;
}
const T& operator* () const
{
if (!obj)
Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator*");
return *obj;
}
T* operator-> ()
{
if (!obj)
Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator->");
return obj;
}
T* operator-> () const
{
if (!obj)
Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator->");
return obj;
}
bool operator== (const SmartPointer< T >&right) { return obj == right.obj; }
bool operator!= (const SmartPointer< T >&right) { return obj != right.obj; }
SmartPointer< T > &operator= (SmartPointer< T > &right)
{
if (this == &right)
return *this;
if (right.obj)
right.obj->incRefCount ();
decRef ();
obj = right.obj;
return *this;
}
SmartPointer< T > &operator= (T* right)
{
if (obj == right)
return *this;
decRef ();
obj = right;
if (obj) obj->incRefCount();
return *this;
}
// template<class U> operator U() { return obj; }
operator T*() const { return obj; }
#if 0
operator const T*() { return obj; }
operator bool () const { return obj != NULL; }
bool operator! () const { return obj == NULL; }
#endif
~SmartPointer< T > () { decRef(); }
public:
// const T* address() const { return obj; }
const T* operator()() const { return obj; }
};
#endif
GYOTO_DEBUG_EXPR
#define GYOTO_DEBUG_EXPR(a)
Output expression value in debug mode.
Definition GyotoDefs.h:280
GyotoError.h
Error handling.
GyotoUtils.h
GYOTO utilities.
Gyoto::FactoryMessenger
Factory / SmartPointee::Subcontractor_t interface.
Definition GyotoFactoryMessenger.h:92
Gyoto::SmartPointee
Can be pointed to by a SmartPointer.
Definition GyotoSmartPointer.h:81
Gyoto::SmartPointee::decRefCount
int decRefCount()
Decrement the reference counter and return current value. Warning: Don't mess with the counter.
Gyoto::SmartPointee::Subcontractor_t
Gyoto::SmartPointer< Gyoto::SmartPointee > Subcontractor_t(Gyoto::FactoryMessenger *, std::vector< std::string > const &)
A subcontractor builds an object upon order from the Factory.
Definition GyotoSmartPointer.h:114
Gyoto::SmartPointee::getRefCount
int getRefCount()
Get the current number of references.
Gyoto::SmartPointee::incRefCount
void incRefCount()
Increment the reference counter. Warning: Don't mess with the counter.
Gyoto::SmartPointee::refCount
int refCount
Reference counter.
Definition GyotoSmartPointer.h:83
Gyoto::SmartPointee::mutex_
pthread_mutex_t mutex_
A mutex.
Definition GyotoSmartPointer.h:89
Gyoto::SmartPointee::SmartPointee
SmartPointee(const SmartPointee &)
Copy constructor.
Gyoto::SmartPointer
Pointers performing reference counting.
Definition GyotoSmartPointer.h:135
Gyoto::SmartPointer::SmartPointer
SmartPointer(const SmartPointer< T > &orig)
Copy constructor from same type.
Definition GyotoSmartPointer.h:189
Gyoto::SmartPointer::decRef
void decRef()
Decrement the reference counter. Warning: don't mess with it.
Definition GyotoSmartPointer.h:147
Gyoto::SmartPointer::operator==
bool operator==(const SmartPointer< T > &right)
Comparison operator between two SmartPointer of same kind.
Definition GyotoSmartPointer.h:271
Gyoto::SmartPointer::operator*
T & operator*()
Dereference operator "*".
Definition GyotoSmartPointer.h:225
Gyoto::SmartPointer::SmartPointer
SmartPointer(T *orig=NULL)
Constructor from a standard pointer-to-class.
Definition GyotoSmartPointer.h:169
Gyoto::SmartPointer::operator->
T * operator->()
Dereference operator "->".
Definition GyotoSmartPointer.h:249
Gyoto::SmartPointer::operator()
const T * operator()() const
Get standard, non-smart pointer to object. Use with care.
Definition GyotoSmartPointer.h:351
Gyoto::SmartPointer::SmartPointer
SmartPointer(const SmartPointer< U > &orig)
Copy constructor from compatible type (used for casting).
Definition GyotoSmartPointer.h:213
Gyoto::SmartPointer< Gyoto::Astrobj::Generic >::obj
Gyoto::Astrobj::Generic * obj
Definition GyotoSmartPointer.h:141
Gyoto::SmartPointer::operator=
SmartPointer< T > & operator=(SmartPointer< T > &right)
Copy a SmartPointer to another (already defined) SmartPointer of same kind.
Definition GyotoSmartPointer.h:282
Gyoto::SmartPointer::operator!=
bool operator!=(const SmartPointer< T > &right)
Comparison operator between two SmartPointer of same kind.
Definition GyotoSmartPointer.h:276
Gyoto
Namespace for the Gyoto library.
Definition GyotoAstrobj.h:44
Gyoto::throwError
void throwError(std::string)
Throw a Gyoto::Error.