Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnSmartPointer.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnSmartPointer.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 ** DESCRIPTION:
11 ** Provides a smart pointer interface to avoid the need to deep copy large
12 ** constant (shared) objects. Example use:
13 ** drwnSmartPointer<TImage> A(new TImage());
14 ** drwnSmartPointer<TImage> B(A);
15 ** drwnSmartPointer<TImage> C = A;
16 ** DRWN_ASSERT((C == A) && (B == A));
17 ** C->showImage();
18 ** Unlike the STL's auto_ptr, it is safe to use smart pointers in STL
19 ** containers.
20 **
21 ** By default the smart pointer takes ownership of the object (and will
22 ** delete it when the reference count reaches zero). However, the smart
23 ** pointer can also be configured to access statically allocated objects
24 ** (e.g. on the stack). In this case the object is not deleted, but care
25 ** must be taken to ensure that the object does not go out of scope before
26 ** all smart pointers have been destroyed. Here's an example:
27 ** TImage I;
28 ** drwnSmartPointer<TImage> A(&I, false);
29 **
30 *****************************************************************************/
31 
32 #pragma once
33 
34 // drwnSmartPointer class ---------------------------------------------------
35 
63 template <typename T>
65  protected:
66  T* _objPtr;
67  unsigned *_refCount;
68  bool _bOwner;
69 
70  public:
72  inline drwnSmartPointer() :
73  _objPtr(NULL), _refCount(NULL), _bOwner(true) {
74  // do nothing
75  }
76 
78  inline explicit drwnSmartPointer(T* obj, bool bOwner = true) :
79  _objPtr(obj), _bOwner(bOwner) {
80  _refCount = (_objPtr == NULL) ? NULL : new unsigned(1);
81  }
82 
86  if (_refCount != NULL) {
87  ++*_refCount;
88  }
89  }
90 
91  inline ~drwnSmartPointer() {
92  if ((_refCount != NULL) && (--*_refCount == 0)) {
93  if (_bOwner) {
94  delete _objPtr;
95  }
96  delete _refCount;
97  _objPtr = NULL;
98  _refCount = NULL;
99  }
100  }
101 
104  if (p._refCount) {
105  ++*p._refCount;
106  }
107 
108  if ((_refCount != NULL) && (--*_refCount == 0)) {
109  if (_bOwner) {
110  delete _objPtr;
111  }
112  delete _refCount;
113  }
114 
115  _objPtr = p._objPtr;
116  _refCount = p._refCount;
117  _bOwner = p._bOwner;
118 
119  return *this;
120  }
121 
123  inline bool operator==(const drwnSmartPointer<T>& p) {
124  return (_refCount == p._refCount);
125  }
127  inline bool operator!=(const drwnSmartPointer<T>& p) {
128  return (_refCount != p._refCount);
129  }
131  inline bool operator==(const T* o) {
132  return (_objPtr == o);
133  }
135  inline bool operator!=(const T* o) {
136  return (_objPtr != o);
137  }
138 
140  inline T* operator->() { return _objPtr; }
142  inline const T* operator->() const { return _objPtr; }
143 
145  inline operator T*() { return _objPtr; }
147  inline operator const T*() const { return _objPtr; }
148 };
149 
150 // drwnSmartPointer comparison classes -------------------------------------
151 
153 template <typename T>
155  bool operator()(drwnSmartPointer<T>& a, drwnSmartPointer<T>& b) const {
156  return (*a < *b);
157  }
158 };
drwnSmartPointer< T > & operator=(const drwnSmartPointer< T > &p)
assignment operator
Definition: drwnSmartPointer.h:103
drwnSmartPointer(T *obj, bool bOwner=true)
create a smart pointer of type T to obj
Definition: drwnSmartPointer.h:78
T * _objPtr
pointer to the shared object
Definition: drwnSmartPointer.h:66
T * operator->()
de-reference the smart pointer
Definition: drwnSmartPointer.h:140
drwnSmartPointer()
default constructor (NULL smart pointer)
Definition: drwnSmartPointer.h:72
unsigned * _refCount
number of drwnSmartPointer objects that share _objPtr
Definition: drwnSmartPointer.h:67
comparison operator for objects held in a drwnSmartPointer
Definition: drwnSmartPointer.h:154
const T * operator->() const
de-reference the smart pointer
Definition: drwnSmartPointer.h:142
bool operator==(const T *o)
compare smart pointers with object pointer for equality
Definition: drwnSmartPointer.h:131
bool operator==(const drwnSmartPointer< T > &p)
compare two smart pointers for equality
Definition: drwnSmartPointer.h:123
bool operator!=(const drwnSmartPointer< T > &p)
compare two smart pointers for inequality
Definition: drwnSmartPointer.h:127
bool _bOwner
true if drwnSmartPointer is responsible for destroying _objPtr
Definition: drwnSmartPointer.h:68
bool operator!=(const T *o)
compare smart pointers with object pointer for inequality
Definition: drwnSmartPointer.h:135
Implements a shared pointer interface to avoid the need to deep copy constant (shared) objects...
Definition: drwnSmartPointer.h:64
drwnSmartPointer(const drwnSmartPointer< T > &p)
copy constructor
Definition: drwnSmartPointer.h:84