34 #include "DGtal/base/Common.h"
35 #include "DGtal/base/CountedPtr.h"
39 using namespace DGtal;
50 unsigned int nbok = 0;
55 int *value=
new int(5);
57 nbok += p.unique() ? 1 : 0;
59 trace.
info() << p <<
" value=" << *p<< std::endl;
60 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
61 <<
"unique" << std::endl;
64 trace.
info() << p <<
" value=" << *p<< std::endl;
65 nbok += p.unique() ? 1 : 0;
67 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
68 <<
"unique" << std::endl;
78 unsigned int nbok = 0;
83 int *value=
new int(5);
85 nbok += p.unique() ? 1 : 0;
88 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
89 <<
"unique" << std::endl;
93 nbok += p.unique() ? 0: 1;
97 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
98 <<
"not unique anymore" << std::endl;
107 A(
int _a ) : a( _a )
110 trace.
info() <<
"#" << nb <<
" A::A( int ), a is " << a << std::endl;
112 A(
const A& other ) : a( other.a )
115 trace.
info() <<
"#" << nb <<
" A::A( const A& ), a is " << a << std::endl;
117 A& operator=(
const A& other )
119 if (
this != &other )
121 trace.
info() <<
"#" << nb <<
" A::op=( const A& ), a is " << a << std::endl;
127 trace.
info() <<
"#" << nb <<
" A::~A(), a was " << a << std::endl;
137 unsigned int nbok = 0;
141 trace.
beginBlock (
"An invalid CountedPtr does not create any instance." );
145 ++nb; nbok += A::nb == 0 ? 1 : 0;
146 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 0" << std::endl;
149 trace.
beginBlock (
"CountedPtr can be used as a simple pointer with automatic deallocation." );
152 ++nb; nbok += A::nb == 1 ? 1 : 0;
153 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 1" << std::endl;
155 ++nb; nbok += A::nb == 0 ? 1 : 0;
156 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 0" << std::endl;
159 trace.
beginBlock (
"CountedPtr can be initialized with = CountedPtr<A>( pointer )." );
162 ++nb; nbok += A::nb == 1 ? 1 : 0;
163 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 1" << std::endl;
165 ++nb; nbok += A::nb == 0 ? 1 : 0;
166 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 0" << std::endl;
173 ++nb; nbok += A::nb == 1 ? 1 : 0;
174 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 1" << std::endl;
175 ++nb; nbok += cptr.
get() == cptr2.
get() ? 1 : 0;
176 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr.get() == cptr2.get()" << std::endl;
177 ++nb; nbok += cptr.
count() == 2 ? 1 : 0;
178 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr.count() == 2" << std::endl;
179 ++nb; nbok += cptr2.
count() == 2 ? 1 : 0;
180 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr2.count() == 2" << std::endl;
182 ++nb; nbok += A::nb == 0 ? 1 : 0;
183 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 0" << std::endl;
190 ++nb; nbok += A::nb == 2 ? 1 : 0;
191 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 2" << std::endl;
192 ++nb; nbok += cptr.
get() != cptr2.
get() ? 1 : 0;
193 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr.get() != cptr2.get()" << std::endl;
195 ++nb; nbok += A::nb == 1 ? 1 : 0;
196 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 1" << std::endl;
197 ++nb; nbok += cptr.
get()->a == 12 ? 1 : 0;
198 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr.get()->a == 12" << std::endl;
199 ++nb; nbok += cptr.
get() == cptr2.
get() ? 1 : 0;
200 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr.get() == cptr2.get()" << std::endl;
201 ++nb; nbok += cptr.
count() == 2 ? 1 : 0;
202 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr.count() == 2" << std::endl;
203 ++nb; nbok += cptr2.
count() == 2 ? 1 : 0;
204 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr2.count() == 2" << std::endl;
206 ++nb; nbok += A::nb == 0 ? 1 : 0;
207 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 0" << std::endl;
218 int main(
int argc,
char** argv )
222 for (
int i = 0; i < argc; ++i )
229 trace.
emphase() << ( res ?
"Passed." :
"Error." ) << endl;
Aim: Smart pointer based on reference counts.
unsigned int count() const
void beginBlock(const std::string &keyword="")
DGtal is the top-level namespace which contains all DGtal functions and types.
int main(int argc, char **argv)
bool testCountedPtrMemory()
bool testCountedPtrCopy()