DGtal
2.2.0
Toggle main menu visibility
Loading...
Searching...
No Matches
CountedPtr.h
1
16
17
#pragma once
18
32
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
49
namespace
DGtal
50
{
51
template
<
typename
T>
class
CountedPtrOrPtr
;
52
template
<
typename
T>
class
CountedConstPtrOrConstPtr
;
53
55
// template class CountedPtr
78
template
<
typename
T>
79
class
CountedPtr
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
128
~CountedPtr
()
129
{
130
release
();
131
}
132
142
CountedPtr
(
const
CountedPtr
& r)
noexcept
143
{
144
acquire
(r.myCounter);
145
}
146
159
CountedPtr
&
operator=
(
const
CountedPtr
& r)
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
}
258
private
:
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 Data ------------------------------
306
private
:
308
Counter*
myCounter
;
309
310
// ------------------------- Private Data --------------------------------
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)
DGtal::CountedConstPtrOrConstPtr
Aim: Smart or simple const pointer on T. It can be a smart pointer based on reference counts or a sim...
Definition
CountedConstPtrOrConstPtr.h:95
DGtal::CountedPtrOrPtr
Aim: Smart or simple pointer on T. It can be a smart pointer based on reference counts or a simple po...
Definition
CountedPtrOrPtr.h:96
DGtal::CountedPtr::drop
T * drop()
Definition
CountedPtr.h:249
DGtal::CountedPtr::operator->
T * operator->() const noexcept
Definition
CountedPtr.h:184
DGtal::CountedPtr::get
T * get() const noexcept
Definition
CountedPtr.h:195
DGtal::CountedPtr::CountedPtr
CountedPtr(const CountedPtr &r) noexcept
Definition
CountedPtr.h:142
DGtal::CountedPtr::unique
bool unique() const noexcept
Definition
CountedPtr.h:204
DGtal::CountedPtr::operator!=
bool operator!=(const T *other) const
Definition
CountedPtr.h:226
DGtal::Counter::isValid
bool isValid() const
DGtal::CountedPtr::count
unsigned int count() const
Definition
CountedPtr.h:236
DGtal::CountedPtr::CountedPtr
CountedPtr(T *p=0)
Definition
CountedPtr.h:119
DGtal::CountedPtr::~CountedPtr
~CountedPtr()
Definition
CountedPtr.h:128
DGtal::CountedPtr::operator=
CountedPtr & operator=(const CountedPtr &r)
Definition
CountedPtr.h:159
DGtal::Counter::myCounter
Counter * myCounter
Definition
CountedPtr.h:308
DGtal::CountedPtr::selfDisplay
void selfDisplay(std::ostream &out) const
DGtal::CountedPtr::operator==
bool operator==(const T *other) const
Definition
CountedPtr.h:215
DGtal::CountedPtr::operator*
T & operator*() const noexcept
Definition
CountedPtr.h:174
DGtal::Counter::acquire
void acquire(Counter *c) noexcept
Definition
CountedPtr.h:265
DGtal::Counter::release
void release()
Definition
CountedPtr.h:278
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition
ClosedIntegerHalfPlane.h:49
DGtal::operator<<
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
DGtal::CountedPtr::Counter::ptr
T * ptr
A pointer to a (shared) dynamically allocated object of type T.
Definition
CountedPtr.h:105
DGtal::CountedPtr::Counter::count
unsigned count
The number of CountedPtr pointing to this counter.
Definition
CountedPtr.h:107
DGtal::CountedPtr::Counter::Counter
Counter(T *p=0, unsigned c=1)
Definition
CountedPtr.h:103
src
DGtal
base
CountedPtr.h
Generated on
for DGtal by
1.18.0