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 namespace Gyoto {
43  class SmartPointee;
44  class FactoryMessenger;
45  template <class T> class SmartPointer;
46 }
47 
48 #include <GyotoError.h>
49 #include <GyotoUtils.h>
50 #include <stddef.h>
51 #include <iostream>
52 #include <typeinfo>
53 
75 {
76  private:
77  int refCount;
78 
79  public:
80  SmartPointee () ;
81  SmartPointee (const SmartPointee&) ;
82  void incRefCount () ;
83  int decRefCount () ;
84  int getRefCount () ;
85 
102 
103 };
104 
105 
118 template< class T >
120 {
121  private:
122 
126  T *obj;
127 
128  private:
132  void decRef ()
133  {
134  if (obj && obj->decRefCount() == 0) {
135  if (debug())
136  std::cerr << "DEBUG: SmartPointer<"
137  << typeid(obj).name()
138  <<">::decRef(): delete " << obj << "\n";
139  delete obj;
140  obj = NULL;
141  }
142  }
143 
144  public:
155  SmartPointer (T *orig = NULL) : obj(orig)
156  {
157  if (obj)
158  obj->incRefCount();
159  }
160 
176  {
177  obj = orig.obj;
178  if (obj)
179  obj->incRefCount ();
180  }
181 
198  template<class U>
200  {
201  obj = dynamic_cast<T*>(const_cast<U*>(orig()));
202  if (obj)
203  obj->incRefCount ();
204  }
205 
212  {
213  if (!obj)
214  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator*");
215  return *obj;
216  }
217 
223  const T& operator* () const
224  {
225  if (!obj)
226  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator*");
227  return *obj;
228  }
229 
236  {
237  if (!obj)
238  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator->");
239  return obj;
240  }
241 
242  T* operator-> () const
243  {
244  if (!obj)
245  Gyoto::throwError("Null Gyoto::SmartPointer dereference in operator->");
246  return obj;
247  }
248 
252  bool operator== (const SmartPointer< T >&right) { return obj == right.obj; }
253 
257  bool operator!= (const SmartPointer< T >&right) { return obj != right.obj; }
258 
264  {
265  if (this == &right)
266  return *this;
267 
268  if (right.obj)
269  right.obj->incRefCount ();
270 
271  decRef ();
272 
273  obj = right.obj;
274 
275  return *this;
276  }
277 
283  {
284  if (obj == right)
285  return *this;
286 
287  decRef ();
288 
289  obj = right;
290 
291  if (obj) obj->incRefCount();
292 
293  return *this;
294  }
295 
299  // template<class U> operator U() { return obj; }
300  operator T*() { return obj; }
301 
302  operator const T*() { return obj; }
303 
308  operator bool () { return obj != NULL; }
309 
314  bool operator! () const { return obj == NULL; }
315 
316  ~SmartPointer< T > () { decRef(); }
317 
318  public:
329  // const T* address() const { return obj; }
330  const T* operator()() const { return obj; }
331 
332 };
333 
334 #endif