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&) ;
97  void incRefCount () ;
98  int decRefCount () ;
99  int getRefCount () ;
100 
115  Subcontractor_t(Gyoto::FactoryMessenger*, std::vector<std::string> const &);
117 
118 };
119 
120 
134 template< class T >
136 {
137  private:
138 
142  T *obj;
143 
144  private:
148  void decRef ()
149  {
150  if (obj && obj->decRefCount() == 0) {
151 # if GYOTO_DEBUG_ENABLED
153 # endif
154  delete obj;
155  obj = NULL;
156  }
157  }
158 
159  public:
170  SmartPointer (T *orig = NULL) : obj(orig)
171  {
172  if (obj)
173  obj->incRefCount();
174  }
175 
191  {
192  obj = orig.obj;
193  if (obj)
194  obj->incRefCount ();
195  }
196 
213  template<class U>
215  {
216  obj = dynamic_cast<T*>(const_cast<U*>(orig()));
217  if (obj)
218  obj->incRefCount ();
219  }
220 
227  {
228  if (!obj)
229  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator*");
230  return *obj;
231  }
232 
238  const T& operator* () const
239  {
240  if (!obj)
241  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator*");
242  return *obj;
243  }
244 
251  {
252  if (!obj)
253  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator->");
254  return obj;
255  }
256 
262  T* operator-> () const
263  {
264  if (!obj)
265  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator->");
266  return obj;
267  }
268 
272  bool operator== (const SmartPointer< T >&right) { return obj == right.obj; }
273 
277  bool operator!= (const SmartPointer< T >&right) { return obj != right.obj; }
278 
284  {
285  if (this == &right)
286  return *this;
287 
288  if (right.obj)
289  right.obj->incRefCount ();
290 
291  decRef ();
292 
293  obj = right.obj;
294 
295  return *this;
296  }
297 
303  {
304  if (obj == right)
305  return *this;
306 
307  decRef ();
308 
309  obj = right;
310 
311  if (obj) obj->incRefCount();
312 
313  return *this;
314  }
315 
319  // template<class U> operator U() { return obj; }
320  operator T*() const { return obj; }
321 
322 #if 0
323  operator const T*() { return obj; }
324 
329  operator bool () const { return obj != NULL; }
330 
335  bool operator! () const { return obj == NULL; }
336 #endif
337 
338  ~SmartPointer () { decRef(); }
339 
340  public:
351  // const T* address() const { return obj; }
352  const T* operator()() const { return obj; }
353 
354 };
355 
356 #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:214
SmartPointee & operator=(const SmartPointee &)
Assignment.
T & operator*()
Dereference operator "*".
Definition: GyotoSmartPointer.h:226
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:277
#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:272
T * obj
Real pointer, don&#39;t mess with it.
Definition: GyotoSmartPointer.h:142
const T * operator()() const
Get standard, non-smart pointer to object. Use with care.
Definition: GyotoSmartPointer.h:352
SmartPointer(T *orig=NULL)
Constructor from a standard pointer-to-class.
Definition: GyotoSmartPointer.h:170
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:115
Namespace for the Gyoto library.
Definition: GyotoAstrobj.h:46
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:190
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:250
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:283
void decRef()
Decrement the reference counter. Warning: don&#39;t mess with it.
Definition: GyotoSmartPointer.h:148