Gyoto
GyotoSmartPointer.h
Go to the documentation of this file.
1 
19 /*
20  Copyright 2011 Thibaut Paumard
21 
22  This file is part of Gyoto.
23 
24  Gyoto is free software: you can redistribute it and/or modify
25  it under the terms of the GNU General Public License as published by
26  the Free Software Foundation, either version 3 of the License, or
27  (at your option) any later version.
28 
29  Gyoto is distributed in the hope that it will be useful,
30  but WITHOUT ANY WARRANTY; without even the implied warranty of
31  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32  GNU General Public License for more details.
33 
34  You should have received a copy of the GNU General Public License
35  along with Gyoto. If not, see <http://www.gnu.org/licenses/>.
36  */
37 
38 
39 #ifndef __GyotoSmartPointer_H_
40 #define __GyotoSmartPointer_H_
41 
42 #include "GyotoUtils.h"
43 #ifdef HAVE_PTHREAD
44 #include <pthread.h>
45 #endif
46 
47 namespace Gyoto {
48  class SmartPointee;
49  class FactoryMessenger;
50  template <class T> class SmartPointer;
51 }
52 
53 #include <GyotoError.h>
54 #include <stddef.h>
55 #include <iostream>
56 #include <typeinfo>
57 
79 {
80  private:
81  int refCount;
82 
83 # ifdef HAVE_PTHREAD
84 
87  pthread_mutex_t mutex_;
88 #endif
89 
90  public:
91  SmartPointee () ;
92  virtual ~SmartPointee() ;
93  SmartPointee (const SmartPointee&) ;
94  void incRefCount () ;
95  int decRefCount () ;
96  int getRefCount () ;
97 
112  Subcontractor_t(Gyoto::FactoryMessenger*);
114 
115 };
116 
117 
131 template< class T >
133 {
134  private:
135 
139  T *obj;
140 
141  private:
145  void decRef ()
146  {
147  if (obj && obj->decRefCount() == 0) {
148 # if GYOTO_DEBUG_ENABLED
149  GYOTO_DEBUG_EXPR(obj);
150 # endif
151  delete obj;
152  obj = NULL;
153  }
154  }
155 
156  public:
167  SmartPointer (T *orig = NULL) : obj(orig)
168  {
169  if (obj)
170  obj->incRefCount();
171  }
172 
188  {
189  obj = orig.obj;
190  if (obj)
191  obj->incRefCount ();
192  }
193 
210  template<class U>
212  {
213  obj = dynamic_cast<T*>(const_cast<U*>(orig()));
214  if (obj)
215  obj->incRefCount ();
216  }
217 
223  T& operator* ()
224  {
225  if (!obj)
226  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator*");
227  return *obj;
228  }
229 
235  const T& operator* () const
236  {
237  if (!obj)
238  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator*");
239  return *obj;
240  }
241 
247  T* operator-> ()
248  {
249  if (!obj)
250  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator->");
251  return obj;
252  }
253 
259  T* operator-> () const
260  {
261  if (!obj)
262  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator->");
263  return obj;
264  }
265 
269  bool operator== (const SmartPointer< T >&right) { return obj == right.obj; }
270 
274  bool operator!= (const SmartPointer< T >&right) { return obj != right.obj; }
275 
281  {
282  if (this == &right)
283  return *this;
284 
285  if (right.obj)
286  right.obj->incRefCount ();
287 
288  decRef ();
289 
290  obj = right.obj;
291 
292  return *this;
293  }
294 
299  SmartPointer< T > &operator= (T* right)
300  {
301  if (obj == right)
302  return *this;
303 
304  decRef ();
305 
306  obj = right;
307 
308  if (obj) obj->incRefCount();
309 
310  return *this;
311  }
312 
316  // template<class U> operator U() { return obj; }
317  operator T*() const { return obj; }
318 
319 #if 0
320  operator const T*() { return obj; }
321 
326  operator bool () const { return obj != NULL; }
327 
332  bool operator! () const { return obj == NULL; }
333 #endif
334 
335  ~SmartPointer< T > () { decRef(); }
336 
337  public:
348  // const T* address() const { return obj; }
349  const T* operator()() const { return obj; }
350 
351 };
352 
353 #endif
int getRefCount()
Get the current number of references.
const T * operator()() const
Get standard, non-smart pointer to object. Use with care.
Definition: GyotoSmartPointer.h:349
SmartPointer(const SmartPointer< U > &orig)
Copy constructor from compatible type (used for casting)
Definition: GyotoSmartPointer.h:211
int decRefCount()
Decrement the reference counter and return current value. Warning: Don&#39;t mess with the counter...
Gyoto::SmartPointer< Gyoto::SmartPointee > Subcontractor_t(Gyoto::FactoryMessenger *)
A subcontractor builds an object upon order from the Factory.
Definition: GyotoSmartPointer.h:112
#define GYOTO_DEBUG_EXPR(a)
Output expression value in debug mode.
Definition: GyotoDefs.h:265
pthread_mutex_t mutex_
A mutex.
Definition: GyotoSmartPointer.h:87
T * obj
Real pointer, don&#39;t mess with it.
Definition: GyotoSmartPointer.h:139
SmartPointer(T *orig=NULL)
Constructor from a standard pointer-to-class.
Definition: GyotoSmartPointer.h:167
Namespace for the Gyoto library.
Definition: GyotoAstrobj.h:43
void throwError(std::string)
Throw a Gyoto::Error.
int refCount
Reference counter.
Definition: GyotoSmartPointer.h:81
SmartPointer(const SmartPointer< T > &orig)
Copy constructor from same type.
Definition: GyotoSmartPointer.h:187
Pointers performing reference counting.
Definition: GyotoProperty.h:41
Can be pointed to by a SmartPointer.
Definition: GyotoSmartPointer.h:78
GYOTO utilities.
Error handling.
void incRefCount()
Increment the reference counter. Warning: Don&#39;t mess with the counter.
void decRef()
Decrement the reference counter. Warning: don&#39;t mess with it.
Definition: GyotoSmartPointer.h:145