DGtal 1.3.0
Loading...
Searching...
No Matches
CountedPtr.h
1
17#pragma once
18
33#if defined(CountedPtr_RECURSES)
34#error Recursive header files inclusion detected in CountedPtr.h
35#else // defined(CountedPtr_RECURSES)
37#define CountedPtr_RECURSES
38
39#if !defined CountedPtr_h
41#define CountedPtr_h
42
44// Inclusions
45#include <iostream>
46#include "DGtal/base/Common.h"
48
49namespace DGtal
50{
51 template <typename T> class CountedPtrOrPtr;
52 template <typename T> class CountedConstPtrOrConstPtr;
53
55 // template class CountedPtr
78 template <typename T>
80 {
81 public:
82
84 friend class CountedPtrOrPtr<T>;
86 friend class CountedConstPtrOrConstPtr<T>;
87
88 // ----------------------- Standard services ------------------------------
89 public:
93 struct Counter {
103 Counter(T* p = 0, unsigned c = 1) : ptr(p), count(c) {}
105 T* ptr;
107 unsigned count;
108 };
109
110
119 explicit CountedPtr(T* p = 0) : myCounter(0)
120 {
121 if (p) myCounter = new Counter(p);
122 }
123
129 {
130 release();
131 }
132
142 CountedPtr(const CountedPtr& r) noexcept
143 {
144 acquire(r.myCounter);
145 }
146
160 {
161 if (this != &r)
162 {
163 release();
164 acquire(r.myCounter);
165 }
166 return *this;
167 }
168
174 T& operator*() const noexcept
175 {
176 return *myCounter->ptr;
177 }
178
184 T* operator->() const noexcept
185 {
186 return myCounter->ptr;
187 }
188
195 T* get() const noexcept
196 {
197 return myCounter ? myCounter->ptr : 0;
198 }
199
204 bool unique() const noexcept
205 {
206 return (myCounter ? myCounter->count == 1 : true);
207 }
208
215 bool operator==( const T* other ) const
216 {
217 return get() == other;
218 }
219
226 bool operator!=( const T* other ) const
227 {
228 return get() != other;
229 }
230
236 unsigned int count() const
237 {
238 return myCounter->count;
239 }
240
249 inline T* drop()
250 {
251 ASSERT( isValid() );
252 ASSERT( unique() );
253 T* tmp = myCounter->ptr;
254 delete myCounter;
255 myCounter = 0;
256 return tmp;
257 }
258private:
265 void acquire(Counter* c) noexcept
266 { // increment the count
267 myCounter = c;
268 if (c) ++c->count;
269 }
270
278 void release()
279 { // decrement the count, delete if it is 0
280 if (myCounter) {
281 if (--myCounter->count == 0) {
282 delete myCounter->ptr;
283 delete myCounter;
284 }
285 myCounter = 0;
286 }
287 }
288
289
290 // ----------------------- Interface --------------------------------------
291 public:
292
297 void selfDisplay ( std::ostream & out ) const;
298
303 bool isValid() const;
304
305 // ------------------------- Protected Datas ------------------------------
306 private:
309
310 // ------------------------- Private Datas --------------------------------
311 private:
312
313 // ------------------------- Hidden services ------------------------------
314 protected:
315
316
317 // ------------------------- Internals ------------------------------------
318 private:
319
320 }; // end of class CountedPtr
321
322
329 template <typename T>
330 std::ostream&
331 operator<< ( std::ostream & out, const CountedPtr<T> & object );
332
333} // namespace DGtal
334
335
337// Includes inline functions.
338#include "DGtal/base/CountedPtr.ih"
339
340// //
342
343#endif // !defined CountedPtr_h
344
345#undef CountedPtr_RECURSES
346#endif // else defined(CountedPtr_RECURSES)
Aim: Smart or simple const pointer on T. It can be a smart pointer based on reference counts or a sim...
Aim: Smart or simple pointer on T. It can be a smart pointer based on reference counts or a simple po...
Aim: Smart pointer based on reference counts.
Definition: CountedPtr.h:80
T * operator->() const noexcept
Definition: CountedPtr.h:184
T * get() const noexcept
Definition: CountedPtr.h:195
CountedPtr(const CountedPtr &r) noexcept
Definition: CountedPtr.h:142
bool unique() const noexcept
Definition: CountedPtr.h:204
bool operator!=(const T *other) const
Definition: CountedPtr.h:226
bool isValid() const
unsigned int count() const
Definition: CountedPtr.h:236
CountedPtr(T *p=0)
Definition: CountedPtr.h:119
CountedPtr & operator=(const CountedPtr &r)
Definition: CountedPtr.h:159
Counter * myCounter
The counter object pointed by this smart pointer.
Definition: CountedPtr.h:308
void selfDisplay(std::ostream &out) const
bool operator==(const T *other) const
Definition: CountedPtr.h:215
T & operator*() const noexcept
Definition: CountedPtr.h:174
void acquire(Counter *c) noexcept
Definition: CountedPtr.h:265
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition: CountedPtr.h:105
unsigned count
The number of CountedPtr pointing to this counter.
Definition: CountedPtr.h:107
Counter(T *p=0, unsigned c=1)
Definition: CountedPtr.h:103