Gyoto
GyotoSmartPointer.h
Go to the documentation of this file.
1 
19 /*
20  Copyright 2011-2014, 2016, 2020 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 #include <string>
58 #include <vector>
59 
81 {
82  private:
83  int refCount;
84 
85 # ifdef HAVE_PTHREAD
86 
89  pthread_mutex_t mutex_;
90 #endif
91 
92  public:
93  SmartPointee () ;
94  virtual ~SmartPointee() ;
95  SmartPointee (const SmartPointee&) ;
96  void incRefCount () ;
97  int decRefCount () ;
98  int getRefCount () ;
99 
114  Subcontractor_t(Gyoto::FactoryMessenger*, std::vector<std::string> const &);
116 
117 };
118 
119 
133 template< class T >
135 {
136  private:
137 
141  T *obj;
142 
143  private:
147  void decRef ()
148  {
149  if (obj && obj->decRefCount() == 0) {
150 # if GYOTO_DEBUG_ENABLED
152 # endif
153  delete obj;
154  obj = NULL;
155  }
156  }
157 
158  public:
169  SmartPointer (T *orig = NULL) : obj(orig)
170  {
171  if (obj)
172  obj->incRefCount();
173  }
174 
190  {
191  obj = orig.obj;
192  if (obj)
193  obj->incRefCount ();
194  }
195 
212  template<class U>
214  {
215  obj = dynamic_cast<T*>(const_cast<U*>(orig()));
216  if (obj)
217  obj->incRefCount ();
218  }
219 
226  {
227  if (!obj)
228  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator*");
229  return *obj;
230  }
231 
237  const T& operator* () const
238  {
239  if (!obj)
240  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator*");
241  return *obj;
242  }
243 
250  {
251  if (!obj)
252  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator->");
253  return obj;
254  }
255 
261  T* operator-> () const
262  {
263  if (!obj)
264  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator->");
265  return obj;
266  }
267 
271  bool operator== (const SmartPointer< T >&right) { return obj == right.obj; }
272 
276  bool operator!= (const SmartPointer< T >&right) { return obj != right.obj; }
277 
283  {
284  if (this == &right)
285  return *this;
286 
287  if (right.obj)
288  right.obj->incRefCount ();
289 
290  decRef ();
291 
292  obj = right.obj;
293 
294  return *this;
295  }
296 
302  {
303  if (obj == right)
304  return *this;
305 
306  decRef ();
307 
308  obj = right;
309 
310  if (obj) obj->incRefCount();
311 
312  return *this;
313  }
314 
318  // template<class U> operator U() { return obj; }
319  operator T*() const { return obj; }
320 
321 #if 0
322  operator const T*() { return obj; }
323 
328  operator bool () const { return obj != NULL; }
329 
334  bool operator! () const { return obj == NULL; }
335 #endif
336 
337  ~SmartPointer< T > () { decRef(); }
338 
339  public:
350  // const T* address() const { return obj; }
351  const T* operator()() const { return obj; }
352 
353 };
354 
355 #endif
int getRefCount()
Get the current number of references.
SmartPointer(const SmartPointer< U > &orig)
Copy constructor from compatible type (used for casting)
Definition: GyotoSmartPointer.h:213
T & operator*()
Dereference operator "*".
Definition: GyotoSmartPointer.h:225
int decRefCount()
Decrement the reference counter and return current value. Warning: Don&#39;t mess with the counter...
Factory / SmartPointee::Subcontractor_t interface.
Definition: GyotoFactoryMessenger.h:92
bool operator!=(const SmartPointer< T > &right)
Comparison operator between two SmartPointer of same kind.
Definition: GyotoSmartPointer.h:276
#define GYOTO_DEBUG_EXPR(a)
Output expression value in debug mode.
Definition: GyotoDefs.h:280
pthread_mutex_t mutex_
A mutex.
Definition: GyotoSmartPointer.h:89
bool operator==(const SmartPointer< T > &right)
Comparison operator between two SmartPointer of same kind.
Definition: GyotoSmartPointer.h:271
T * obj
Real pointer, don&#39;t mess with it.
Definition: GyotoSmartPointer.h:141
const T * operator()() const
Get standard, non-smart pointer to object. Use with care.
Definition: GyotoSmartPointer.h:351
SmartPointer(T *orig=NULL)
Constructor from a standard pointer-to-class.
Definition: GyotoSmartPointer.h:169
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
Namespace for the Gyoto library.
Definition: GyotoAstrobj.h:43
void throwError(std::string)
Throw a Gyoto::Error.
int refCount
Reference counter.
Definition: GyotoSmartPointer.h:83
SmartPointer(const SmartPointer< T > &orig)
Copy constructor from same type.
Definition: GyotoSmartPointer.h:189
Pointers performing reference counting.
Definition: GyotoProperty.h:45
Can be pointed to by a SmartPointer.
Definition: GyotoSmartPointer.h:80
GYOTO utilities.
T * operator->()
Dereference operator "->".
Definition: GyotoSmartPointer.h:249
Error handling.
void incRefCount()
Increment the reference counter. Warning: Don&#39;t mess with the counter.
SmartPointer< T > & operator=(SmartPointer< T > &right)
Copy a SmartPointer to another (already defined) SmartPointer of same kind.
Definition: GyotoSmartPointer.h:282
void decRef()
Decrement the reference counter. Warning: don&#39;t mess with it.
Definition: GyotoSmartPointer.h:147