OpenCurves  0.9
refcountptr.h
1 //
2 // author Kazys Stepanas
3 //
4 // Copyright (c) CSIRO 2015
5 //
6 #ifndef REFCOUNTPTR_H_
7 #define REFCOUNTPTR_H_
8 
9 #include "plotsconfig.h"
10 
11 namespace plotutil
12 {
23  template <class T>
25  {
26  public:
28  RefCountPtr();
29 
32  RefCountPtr(T *ptr);
33 
37  template <class Q> RefCountPtr(Q *ptr);
38 
41  RefCountPtr(const RefCountPtr &other);
42 
46  template <class Q> RefCountPtr(const RefCountPtr<Q> &other);
47 
50  RefCountPtr(RefCountPtr &&other);
51 
53  ~RefCountPtr();
54 
57  void release();
58 
61  inline T *ptr() { return _ptr; }
62 
65  inline const T *ptr() const { return _ptr; }
66 
69  inline T &operator * () { return *_ptr; }
70 
73  inline const T &operator * () const { return *_ptr; }
74 
77  inline T *operator -> () { return _ptr; }
78 
81  inline const T *operator -> () const { return _ptr; }
82 
85  inline operator bool() const { return _ptr != nullptr; }
86 
89  inline bool operator!() const { return _ptr == nullptr; }
90 
94  inline bool operator == (const RefCountPtr &other) { return _ptr == other._ptr; }
95 
99  inline bool operator != (const RefCountPtr &other) { return _ptr != other._ptr; }
100 
103  RefCountPtr &operator = (const RefCountPtr &other);
104 
107  RefCountPtr &operator = (T *ptr);
108 
112  template <class Q>
113  RefCountPtr &operator = (Q *ptr);
114 
115  private:
118  void reference(T *ptr);
119 
120  T *_ptr;
121  };
122 
123  template <class T>
125  : _ptr(nullptr)
126  {
127  }
128 
129 
130  template <class T>
132  {
133  reference(ptr);
134  }
135 
136 
137  template <class T>
138  template <class Q>
140  {
141  reference(ptr);
142  }
143 
144 
145  template <class T>
147  {
148  reference(other._ptr);
149  }
150 
151  template <class T>
153  {
154  reference(other._ptr);
155  other._ptr = nullptr;
156  }
157 
158 
159  template <class T>
161  {
162  release();
163  }
164 
165 
166  template <class T>
168  {
169  if (_ptr)
170  {
171  _ptr->decReference();
172  _ptr = nullptr;
173  }
174  }
175 
176 
177  template <class T>
179  {
180  release();
181  reference(other._ptr);
182  return *this;
183  }
184 
185 
186  template <class T>
188  {
189  release();
190  reference(ptr);
191  return *this;
192  }
193 
194 
195  template <class T>
196  template <class Q>
198  {
199  release();
200  reference(ptr);
201  return *this;
202  }
203 
204 
205  template <class T>
206  inline void RefCountPtr<T>::reference(T *ptr)
207  {
208  _ptr = ptr;
209  if (_ptr)
210  {
211  _ptr->addReference();
212  }
213  }
214 }
215 
216 #endif // REFCOUNTPTR_H_
const T * ptr() const
Access the internal pointer.
Definition: refcountptr.h:65
A pointer wrapper for a reference counted object such as RefCountObject.
Definition: refcountptr.h:24
bool operator!() const
Negation operator.
Definition: refcountptr.h:89
RefCountPtr()
Default constructor to a null pointer.
Definition: refcountptr.h:124
void release()
Explicitly releases the internal pointer, making this object null.
Definition: refcountptr.h:167
RefCountPtr & operator=(const RefCountPtr &other)
Assignment operator.
Definition: refcountptr.h:178
T * operator->()
Dereference call operator.
Definition: refcountptr.h:77
bool operator!=(const RefCountPtr &other)
Inequality operator.
Definition: refcountptr.h:99
T & operator*()
Dereference operator.
Definition: refcountptr.h:69
T * ptr()
Access the internal pointer.
Definition: refcountptr.h:61
~RefCountPtr()
Destructor: calls release().
Definition: refcountptr.h:160
Utility functions for sampling and filtering plots and expression evaluation.
Definition: plotutil.h:13
bool operator==(const RefCountPtr &other)
Equality operator.
Definition: refcountptr.h:94