Image Component Library (ICL)
SmartPtrBase.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/SmartPtrBase.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter, Robert Haschke **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/Macros.h>
34 #include <cstdlib>
35 
37 //#define ICL_USE_STD_SHARED_POINTER_IF_POSSIBLE
38 #if __cplusplus >= 201103L
39 #ifdef ICL_USE_STD_SHARED_POINTER_IF_POSSIBLE
40 #define ICL_USE_STD_SHARED_PTR
41 #endif
42 #endif
43 
44 #ifdef ICL_USE_STD_SHARED_PTR
45 #include <memory>
46 #endif
47 
48 namespace icl{
49  namespace utils{
50 
52  class DelOpBase { public: virtual ~DelOpBase(){} };
53 
55  struct PointerDelOp : public DelOpBase{ template<class T> static void delete_func(T *t){ delete t; } };
56 
58  struct ArrayDelOp : public DelOpBase{ template<class T> static void delete_func(T *t){ delete[] t; } };
59 
61  struct FreeDelOp : public DelOpBase{ static void delete_func(void *v){ free(v); } };
62 
63 
64 #ifdef ICL_USE_STD_SHARED_PTR
65  //#warning "Using new std::shared_ptr - based SmartPtr class "
66  template<class T, class delOp = ArrayDelOp >
67  class SmartPtrBase{
68 
69  std::shared_ptr<T> impl;
70 
71  template<class DerivedT>
73  impl = r.impl;
74  return *this;
75  }
76 
77  public:
79  template<class A, class B> friend class SmartPtrBase;
80 
82  inline SmartPtrBase(){}
83 
85  template<class DerivedT>
86  SmartPtrBase(DerivedT *ptData, bool bOwn=true) :
87  impl(ptData, [bOwn](T *t){ if(bOwn) delOp::delete_func(t); }){
88  }
89 
91 
92  template<class DerivedT>
93  SmartPtrBase(const SmartPtrBase<DerivedT,delOp> &r) :
94  impl(std::static_pointer_cast<T,DerivedT>(r.impl)){
95  }
96 
98 
99  SmartPtrBase(const SmartPtrBase<T,delOp> &r): impl(r.impl){}
100 
102 
110  template<class DerivedT>
111  SmartPtrBase<T,delOp> &operator=(const SmartPtrBase<DerivedT,delOp>& r){
112  return assign(r);
113  }
114 
116 
119  SmartPtrBase<T,delOp> &operator=(const SmartPtrBase<T,delOp>& r){
120  return assign(r);
121  }
122 
124 
125  template<class DerivedT>
126  SmartPtrBase<T,delOp> &operator=(DerivedT *p){
127  return this->operator=(SmartPtrBase<T,delOp>(p));
128  }
129 
131  SmartPtrBase<T,delOp> &operator=(T *p){
132  return this->operator=(SmartPtrBase<T,delOp>(p));
133  }
134 
136  virtual ~SmartPtrBase() {}
137 
139 
142  T &operator* () { return *impl; }
143 
145 
148  const T &operator* () const { return *impl; }
149 
150 
152 
155  T* get () { return impl.get(); }
156 
158 
161  const T* get () const { return impl.get(); }
162 
163 
165 
168  T *operator-> () { return impl.get(); }
169 
171 
174  const T *operator-> () const { return impl.get(); }
175 
176 
178  operator bool() const { return (bool)impl; }
179 
181  int use_count() const { return impl.use_count(); }
182 
184 
190  void setNull() {
191  impl.reset();
192  }
193  };
194 #else
195 
196 
198 
221  template<class T, class delOp = ArrayDelOp >
223  protected:
224  T *e;
225  int *c;
226  bool d;
228  void inc() {
230  if(c) (*c)++;
231  }
232 
234  void dec() {
235  if(c && *c) {
236  if ( --(*c) == 0) {
237  // if(d) delete[] e;
238  if(d) delOp::delete_func(e);
239  delete c;
240  c = 0;
241  }
242  }
243  }
244 
246  void set(T *e,int *c, bool d) {
247  this->e=e;
248  this->c=c;
249  this->d=d;
250  }
251 
253  template<class DerivedT>
255  if(r.e == e) return *this;
256  dec();
257  set(r.e,r.c,r.d);
258  inc();
259  return *this;
260  }
261 
262  public:
263 
265  template<class A, class B> friend class SmartPtrBase;
266 
268  SmartPtrBase(): e(0),c(0),d(0){
269  }
270 
272  template<class DerivedT>
273  SmartPtrBase(DerivedT *ptData, bool bOwn=true): e(ptData), c(new int(1)),d(bOwn){
274  }
275 
277 
278  template<class DerivedT>
279  SmartPtrBase(const SmartPtrBase<DerivedT,delOp> &r): e(&dynamic_cast<T&>(*r.e)), c(r.c), d(r.d){
280  // SmartPtrBase(const SmartPtrBase<DerivedT,delOp> &r): e(r.e), c(r.c), d(r.d){
281  inc();
282  }
283 
285 
286  SmartPtrBase(const SmartPtrBase<T,delOp> &r): e(r.e), c(r.c), d(r.d){
287  inc();
288  }
289 
291 
299  template<class DerivedT>
301  return assign(r);
302 
303  }
304 
306 
310  return assign(r);
311  }
312 
314 
315  template<class DerivedT>
317  return this->operator=(SmartPtrBase<T,delOp>(p));
318  }
319 
322  return this->operator=(SmartPtrBase<T,delOp>(p));
323  }
324 
326  virtual ~SmartPtrBase() { dec(); }
327 
329 
332  T &operator* () { ICLASSERT(e); return *e; }
333 
335 
338  const T &operator* () const { ICLASSERT(e); return *e; }
339 
340 
342 
345  T* get () { return e; }
346 
348 
351  const T* get () const { return e; }
352 
353 
355 
358  T *operator-> () { ICLASSERT(e); return e; }
359 
361 
364  const T *operator-> () const { ICLASSERT(e); return e; }
365 
366 
368  operator bool() const { return (e != 0); }
369 
371  int use_count() const { return c ? *c : 0; }
372 
374 
380  void setNull() {
381  dec();
382  set(0,0,0);
383  }
384  };
385 #endif
386  } // namespace utils
387 }
388 
SmartPtrBase(const SmartPtrBase< T, delOp > &r)
Create a copy of given smart pointer.
Definition: SmartPtrBase.h:286
undocument this line if you encounter any issues!
Definition: Any.h:37
virtual ~DelOpBase()
Definition: SmartPtrBase.h:52
SmartPtrBase< T, delOp > & operator=(T *p)
allows for direct assignment of pointers to a SmartPtr object
Definition: SmartPtrBase.h:321
T * operator->()
returns the currently hold element
Definition: SmartPtrBase.h:358
Pointer delete operation class for the SmartPtr class.
Definition: SmartPtrBase.h:55
Pure Interface class for DelOps.
Definition: SmartPtrBase.h:52
int * c
Definition: SmartPtrBase.h:225
SmartPtrBase(DerivedT *ptData, bool bOwn=true)
ptData is given, reference counter is set to 1
Definition: SmartPtrBase.h:273
void set(T *e, int *c, bool d)
sets e and c
Definition: SmartPtrBase.h:246
SmartPtrBase< T, delOp > & assign(const SmartPtrBase< DerivedT, delOp > &r)
utility assignment method used in the SmartPtrBase assignment operators
Definition: SmartPtrBase.h:254
void setNull()
sets the smart pointer to null
Definition: SmartPtrBase.h:380
virtual ~SmartPtrBase()
decreases the reference counter (cleanup on demand)
Definition: SmartPtrBase.h:326
SmartPtrBase< T, delOp > & operator=(DerivedT *p)
allows for direct assignment of pointers to a SmartPtr object
Definition: SmartPtrBase.h:316
static void delete_func(void *v)
Definition: SmartPtrBase.h:61
static void delete_func(T *t)
Definition: SmartPtrBase.h:58
T * get()
returns the pointer to the data
Definition: SmartPtrBase.h:345
T & operator *()
returns a reference of the currently hold element
Definition: SmartPtrBase.h:332
const T * get() const
returns the pointer to the data (const)
Definition: SmartPtrBase.h:351
void dec()
save reference counter decrement (cleanup on demand)
Definition: SmartPtrBase.h:234
T * e
Definition: SmartPtrBase.h:224
SmartPtrBase(const SmartPtrBase< DerivedT, delOp > &r)
Create a copy of given smart pointer with more general type.
Definition: SmartPtrBase.h:279
Array delete operation class for the SmartPtr class.
Definition: SmartPtrBase.h:58
int use_count() const
current reference count
Definition: SmartPtrBase.h:371
#define ICLASSERT(X)
Definition: Macros.h:135
SmartPtrBase()
e and c will become NULL
Definition: SmartPtrBase.h:268
C-Style delete operation class for the SmartPtr class.
Definition: SmartPtrBase.h:61
SmartPtrBase< T, delOp > & operator=(const SmartPtrBase< T, delOp > &r)
explicit implmentation of the same type assignment operator
Definition: SmartPtrBase.h:309
Base class for reference counting smart-pointers.
Definition: SmartPtrBase.h:222
static void delete_func(T *t)
Definition: SmartPtrBase.h:55
SmartPtrBase< T, delOp > & operator=(const SmartPtrBase< DerivedT, delOp > &r)
sets the pointer to hold another reference
Definition: SmartPtrBase.h:300
bool d
Definition: SmartPtrBase.h:226
void inc()
save reference counter increment
Definition: SmartPtrBase.h:229