File failed to load: https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/config/TeX-MML-AM_CHTML/MathJax.js
DGtal 2.0.0
testClone2.cpp
Go to the documentation of this file.
1
16
28
29//#define TRACE_BITS
30
31#include <cstdio>
32#include <cmath>
33#include <iostream>
34#include "DGtal/base/Common.h"
35#include "DGtal/base/CountedPtr.h"
36#include "DGtal/base/CountedPtrOrPtr.h"
37#include "DGtal/base/CountedConstPtrOrConstPtr.h"
38#include "DGtal/base/CowPtr.h"
39#include "DGtal/base/Clone.h"
40#include "DGtal/base/Alias.h"
41#include "DGtal/base/ConstAlias.h"
42#include "DGtal/helpers/StdDefs.h"
43
44using namespace DGtal;
45using namespace std;
46
47
48namespace DGtal {
49
50
61 template <typename T>
62 class NClone
63 {
64 // ----------------------- Standard services ------------------------------
65 public:
66
70 inline ~NClone() {}
71
75 inline NClone ( const NClone & ) { ASSERT( false ); }
76
83 inline NClone( const T & t ) : myCowPtr( const_cast<T*>( &t ) ) {}
84
92 inline NClone( CountedPtr<T> ptr ) : myCowPtr( ptr ) {}
93 inline NClone( CowPtr<T> ptr ) : myCowPtr( ptr ) {}
94
100 inline operator T() const
101 { return myCowPtr.unique()
102 ? T( *( const_cast< CowPtr<T>& >( myCowPtr ).drop() ) )
103 : T( *( myCowPtr.get() ) );
104 }
105 // { return T( *( const_cast< CowPtr<T>& >( myCowPtr ).drop() ) ); }
106
112 inline operator CowPtr<T>() const
113 { return myCowPtr.unique()
114 ? CowPtr<T>( new T( *( const_cast< CowPtr<T>& >( myCowPtr ).drop() ) ) )
115 : myCowPtr;
116 }
117 //{ return myCowPtr; }
118
119 //inline operator CountedPtr<T>() { return CountedPtr<T>( myCowPtr.get() ); }
120
121 // ------------------------- Protected Datas ------------------------------
122 private:
123 // ------------------------- Private Datas --------------------------------
124 private:
127
128
129 // ------------------------- Hidden services ------------------------------
130 private:
131
137
138
145 NClone & operator= ( const NClone & other );
146
147 // ------------------------- Internals ------------------------------------
148 private:
149
150 }; // end of class NClone
151
152} // namespace DGtal
153
154
155// Dummy class to test clones and aliases.
157{
158public:
159 typedef int Element;
160private:
162public:
164 {
165 std::cout << " ~DummyTbl() this=" << this << " data=" << data << std::endl;
166 if ( data != 0 ) {
167 std::cout << " - freed =" << allocated << std::endl;
168 delete[] data;
169 }
170 else
171 std::cout << " - nothing to do (already moved)" << std::endl;
172 ++nbDeleted;
173 }
174 DummyTbl( int i, int val )
175 : size( 0 ), allocated( i )
176 {
177 std::cout << " DummyTbl( " << i << " ) this=" << this << std::endl;
178 data = new Element[ allocated ];
179 data[ size++ ] = val;
180 std::cout << " - allocated=" << allocated << std::endl;
181 std::cout << " - copied =" << size << std::endl;
182 ++nbCreated;
183 }
184 DummyTbl( const DummyTbl & a ) : size( a.size ), allocated( a.allocated )
185 {
186 std::cout << " DummyTbl( const DummyTbl & a ) this=" << this << " a=" << &a << std::endl;
187 data = new Element[ allocated ];
188 for ( int i = 0; i < size; ++i ) data[ i ] = a.data[ i ];
189 std::cout << " - allocated=" << allocated << std::endl;
190 std::cout << " - copied =" << size << std::endl;
191 ++nbCreated;
192 }
193
194 DummyTbl( DummyTbl && a ) noexcept
195 : size( std::move( a.size ) ), allocated( std::move( a.allocated ) )
196 {
197 data = a.data; a.data = 0;
198 std::cout << " DummyTbl( DummyTbl && a ) this=" << this << " a=" << &a << std::endl;
199 std::cout << " - check data: a=" << allocated << " s=" << size << std::endl;
200 ++nbCreated;
201 ++nbMoved;
202 }
203
204 int value() const { return data[ 0 ]; }
205 void setValue( int v ) const { data[ 0 ] = v; }
206
207private:
209 {
210 data = a.data;
211 std::cout << " DummyTbl::operator=( const DummyTbl & a ) " << std::endl;
212 return *this;
213 }
214public:
215 static void reset() {
216 nbCreated = 0;
217 nbDeleted = 0;
218 }
219
221 int size;
223
224 static int nbCreated;
225 static int nbDeleted;
226 static int nbMoved;
227};
228
229int DummyTbl::nbCreated = 0;
230int DummyTbl::nbDeleted = 0;
231int DummyTbl::nbMoved = 0;
232
233
236 inline int value() const { return myDummyTbl.value(); }
238};
239
240struct CloneToCountedMember { // requires explicit duplication
241 inline CloneToCountedMember( Clone<DummyTbl> a1 ) // : myDummyTbl( a1 ) {} does not compile
242 : myDummyTbl( a1 ) {}
243 inline int value() const { return myDummyTbl->value(); }
245};
246
249 inline int value() const { return myDummyTbl->value(); }
250 inline void setValue( int v ) { myDummyTbl->setValue( v ); }
252};
253
255 inline CloneToPtrMember() : myDummyTbl( 0 ) {}
257 inline ~CloneToPtrMember() { if ( myDummyTbl != 0 ) delete myDummyTbl; else std::cerr << "[~CloneToPtrMember] error." << std::endl; }
258 inline int value() const { return myDummyTbl->value(); }
259 inline void setValue( int v ) { myDummyTbl->setValue( v ); }
261};
262
265 inline int value() const { return myDummyTbl.value(); }
267};
268
271 inline int value() const { return myDummyTbl->value(); }
273};
274
275
281
282struct AliasToConstRefMember { // restricted but valid.
284 inline int value() const { return myDummyTbl.value(); }
286};
287
290 inline int value() const { return myDummyTbl.value(); }
292};
293
296 inline int value() const { return myDummyTbl->value(); }
298};
299
305
306// struct ConstAliasToRefMember { // invalid
307// inline ConstAliasToRefMember( ConstAlias<DummyTbl> a1 ) : myDummyTbl( a1 ) {}
308// inline int value() const { return myDummyTbl.value(); }
309// DummyTbl& myDummyTbl;
310// };
311
312
313
314class MyPoint {
315public:
317 { nbDeleted++; }
318 MyPoint( const MyPoint & other )
319 : _x( other._x ), _y( other._y )
320 { nbCreated++; }
321 MyPoint( int x, int y ) : _x( x ), _y( y )
322 { nbCreated++; }
323 MyPoint operator-( const MyPoint & other ) const
324 {
325 return MyPoint( _x - other._x, _y - other._y );
326 }
327 double norm() const
328 {
329 double dx = (double) _x;
330 double dy = (double) _y;
331 return sqrt( dx * dx + dy * dy );
332 }
333 static void reset()
334 {
335 nbCreated = nbDeleted = 0;
336 }
337 int _x, _y;
338
339 static int nbCreated;
340 static int nbDeleted;
341};
342
343int MyPoint::nbCreated = 0;
344int MyPoint::nbDeleted = 0;
345
346class MyPointD {
347public:
349 { nbDeleted++; }
350 MyPointD( const MyPointD & other )
351 : _x( other._x ), _y( other._y )
352 { nbCreated++; }
353 MyPointD( int x, int y ) : _x( x ), _y( y )
354 { nbCreated++; }
355 MyPointD( double x, double y ) : _x( x ), _y( y )
356 { nbCreated++; }
357 MyPointD operator-( const MyPointD & other ) const
358 {
359 return MyPointD( _x - other._x, _y - other._y );
360 }
361 double norm() const
362 {
363 double dx = (double) _x;
364 double dy = (double) _y;
365 return sqrt( dx * dx + dy * dy );
366 }
367 static void reset()
368 {
369 nbCreated = nbDeleted = 0;
370 }
371 double _x, _y;
372
373 static int nbCreated;
374 static int nbDeleted;
375};
376
377int MyPointD::nbCreated = 0;
378int MyPointD::nbDeleted = 0;
379
380//typedef Z2i::Point Point;
382
383
385 TriangleByConstReference( const Point & a, const Point & b, const Point & c )
386 : _a( a ), _b( b ), _c( c ) {}
387 double perimeter() const
388 {
389 return (_a - _b).norm() + (_b - _c).norm() + (_c - _a).norm();
390 }
392};
393
396 : _a( a ), _b( b ), _c( c ) {}
397 double perimeter() const
398 {
399 return (_a - _b).norm() + (_b - _c).norm() + (_c - _a).norm();
400 }
402};
403
406 : _a( a ), _b( b ), _c( c ) {}
407 double perimeter() const
408 {
409 return (_a - _b).norm() + (_b - _c).norm() + (_c - _a).norm();
410 }
412};
413
416 : _a( a ), _b( b ), _c( c ) {}
417 double perimeter() const
418 {
419 return (*_a - *_b).norm() + (*_b - *_c).norm() + (*_c - *_a).norm();
420 }
422};
423
424template <typename Triangle>
425double
427{
428 double total = 0.0;
429 Point A( 0, 0 );
430 for ( int yb = 0; yb < size; ++yb )
431 for ( int xb = 0; xb < size; ++xb )
432 {
433 Point B( xb, yb );
434 for ( int yc = 0; yc < size; ++yc )
435 for ( int xc = 0; xc < size; ++xc )
436 {
437 Point C( xc, yc );
438 Triangle T( A, B, C );
439 total += T.perimeter();
440 }
441 }
442 return total;
443}
444
445template <typename Triangle>
446double
448{
449 double total = 0.0;
450 CowPtr<Point> A( new Point( 0, 0 ) );
451 for ( int yb = 0; yb < size; ++yb )
452 for ( int xb = 0; xb < size; ++xb )
453 {
454 CowPtr<Point> B( new Point( xb, yb ) );
455 for ( int yc = 0; yc < size; ++yc )
456 for ( int xc = 0; xc < size; ++xc )
457 {
458 CowPtr<Point> C( new Point( xc, yc ) );
459 Triangle T( A, B, C );
460 total += T.perimeter();
461 }
462 }
463 return total;
464}
465
476{
477 unsigned int nb = 0;
478 unsigned int nbok = 0;
479 DummyTbl a1( 100, 10 ); // +1/0
480 DummyTbl* ptr_a2 = new DummyTbl( 100, 18 ); // +1/0
481 CountedPtr<DummyTbl> counted_a1( new DummyTbl( 100, 12 ) ); // +1/0
483 trace.beginBlock ( "Testing class Alias." );
484
485 /*
486 - A& -> A& // no duplication
487 - A* -> A& // no duplication, exception if null
488 - A& -> A* // no duplication
489 - A* -> A* // no duplication
490 - CountedPtr<A> -> CountedPtr<A> // shared
491 */
492 trace.beginBlock ( "Alias: #DummyTbl with DummyTbl& to DummyTbl& member. no duplication (0/0)" );
493 AliasToRefMember c00( a1 ); // 0/0
494 trace.info() << "D: d1.value() = " << c00.value() << std::endl;
495 ++nb; nbok += DummyTbl::nbCreated==0 ? 1 : 0;
496 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
497 trace.info() << "(" << nbok << "/" << nb << ")"
498 << " nbCreated=" << DummyTbl::nbCreated
499 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
500 trace.endBlock();
501
502 trace.beginBlock ( "Alias: #DummyTbl with DummyTbl* to DummyTbl& member. no duplication (0/0)" );
503 AliasToRefMember c10( ptr_a2 ); // 0/0
504 trace.info() << "D: d1.value() = " << c10.value() << std::endl;
505 ++nb; nbok += DummyTbl::nbCreated==0 ? 1 : 0;
506 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
507 trace.info() << "(" << nbok << "/" << nb << ")"
508 << " nbCreated=" << DummyTbl::nbCreated
509 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
510 trace.endBlock();
511
512 trace.beginBlock ( "Alias: #DummyTbl with DummyTbl& to DummyTbl* member. no duplication (0/0)" );
513 AliasToPtrMember c01( a1 ); // 0/0
514 trace.info() << "D: d1.value() = " << c01.value() << std::endl;
515 ++nb; nbok += DummyTbl::nbCreated==0 ? 1 : 0;
516 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
517 trace.info() << "(" << nbok << "/" << nb << ")"
518 << " nbCreated=" << DummyTbl::nbCreated
519 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
520 trace.endBlock();
521
522 trace.beginBlock ( "Alias: #DummyTbl with DummyTbl* to DummyTbl* member. no duplication (0/0)" );
523 AliasToPtrMember c11( ptr_a2 ); // 0/0
524 trace.info() << "D: d1.value() = " << c11.value() << std::endl;
525 ++nb; nbok += DummyTbl::nbCreated==0 ? 1 : 0;
526 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
527 trace.info() << "(" << nbok << "/" << nb << ")"
528 << " nbCreated=" << DummyTbl::nbCreated
529 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
530 trace.endBlock();
531
532 trace.beginBlock ( "Alias: #DummyTbl with DummyTbl& to CountedPtrOrPtr<DummyTbl> member. no duplication (0/0)" );
533 AliasToCountedPtrOrPtrMember c06( a1 ); // 0/0
534 trace.info() << "D: d1.value() = " << c06.value() << std::endl;
535 trace.info() << c06.myDummyTbl << std::endl;
536 ++nb; nbok += DummyTbl::nbCreated==0 ? 1 : 0;
537 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
538 trace.info() << "(" << nbok << "/" << nb << ")"
539 << " nbCreated=" << DummyTbl::nbCreated
540 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
541 trace.endBlock();
542
543 trace.beginBlock ( "Alias: #DummyTbl with DummyTbl* to CountedPtrOrPtr<DummyTbl> member. no duplication (0/0)" );
544 AliasToCountedPtrOrPtrMember c16( a1 ); // 0/0
545 trace.info() << "D: d1.value() = " << c16.value() << std::endl;
546 trace.info() << c16.myDummyTbl << std::endl;
547 ++nb; nbok += DummyTbl::nbCreated==0 ? 1 : 0;
548 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
549 trace.info() << "(" << nbok << "/" << nb << ")"
550 << " nbCreated=" << DummyTbl::nbCreated
551 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
552 trace.endBlock();
553
554 // const DummyTbl& const_a1 = a1;
555 // AliasToRefMember c02( const_a1 ); // does not execute.
556 // const DummyTbl* const_ptr_a1 = &a1;
557 // AliasToRefMember c05( const_ptr_a1 ); // does not execute.
558
559 trace.endBlock();
560
561 delete ptr_a2;
562 return nbok == nb;
563}
564
575{
576 unsigned int nb = 0;
577 unsigned int nbok = 0;
578 DummyTbl a1( 100, 10 ); // +1/0
579 DummyTbl* ptr_a2 = new DummyTbl( 100, 18 ); // +1/0
580 CountedPtr<DummyTbl> counted_a1( new DummyTbl( 100, 12 ) ); // +1/0
581 CowPtr<DummyTbl> cow_a1( new DummyTbl( 100, 16 ) ); // +1/0
583 trace.beginBlock ( "Testing class ConstAlias." );
584
585 /*
586 - const A & -> const A & // no duplication
587 - const A* -> const A & // no duplication, exception if null
588 - const A & -> const A* // no duplication
589 - const A* -> const A* // no duplication
590 - CountedPtr<A> -> CowPtr<A> // potential lazy duplication
591 - CowPtr<A> -> CowPtr<A> // potential lazy duplication
592 */
593 trace.beginBlock ( "ConstAlias: #DummyTbl with const DummyTbl& to const DummyTbl& member. no duplication (0/0)" );
594 ConstAliasToConstRefMember c00( a1 ); // 0/0
595 trace.info() << "D: d1.value() = " << c00.value() << std::endl;
596 ++nb; nbok += DummyTbl::nbCreated==0 ? 1 : 0;
597 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
598 trace.info() << "(" << nbok << "/" << nb << ")"
599 << " nbCreated=" << DummyTbl::nbCreated
600 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
601 trace.endBlock();
602
603 trace.beginBlock ( "ConstAlias: #DummyTbl with const DummyTbl* to const DummyTbl& member. no duplication (0/0)" );
604 ConstAliasToConstRefMember c10( ptr_a2 ); // 0/0
605 trace.info() << "D: d1.value() = " << c10.value() << std::endl;
606 ++nb; nbok += DummyTbl::nbCreated==0 ? 1 : 0;
607 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
608 trace.info() << "(" << nbok << "/" << nb << ")"
609 << " nbCreated=" << DummyTbl::nbCreated
610 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
611 trace.endBlock();
612
613 trace.beginBlock ( "ConstAlias: #DummyTbl with const DummyTbl& to const DummyTbl* member. no duplication (0/0)" );
614 ConstAliasToConstPtrMember c01( a1 ); // 0/0
615 trace.info() << "D: d1.value() = " << c01.value() << std::endl;
616 ++nb; nbok += DummyTbl::nbCreated==0 ? 1 : 0;
617 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
618 trace.info() << "(" << nbok << "/" << nb << ")"
619 << " nbCreated=" << DummyTbl::nbCreated
620 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
621 trace.endBlock();
622
623 trace.beginBlock ( "ConstAlias: #DummyTbl with const DummyTbl* to const DummyTbl* member. no duplication (0/0)" );
624 ConstAliasToConstPtrMember c11( ptr_a2 ); // 0/0
625 trace.info() << "D: d1.value() = " << c11.value() << std::endl;
626 ++nb; nbok += DummyTbl::nbCreated==0 ? 1 : 0;
627 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
628 trace.info() << "(" << nbok << "/" << nb << ")"
629 << " nbCreated=" << DummyTbl::nbCreated
630 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
631 trace.endBlock();
632
633 trace.beginBlock ( "ConstAlias: #DummyTbl with const DummyTbl* to CountedConstPtrOrConstPtr<DummyTbl> member. No duplication (0/0)" );
635 trace.info() << "D: d1.value() = " << c17.value() << std::endl;
636 ++nb; nbok += DummyTbl::nbCreated==0 ? 1 : 0;
637 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
638 trace.info() << "(" << nbok << "/" << nb << ")"
639 << " nbCreated=" << DummyTbl::nbCreated
640 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
641 trace.endBlock();
642
643 trace.beginBlock ( "ConstAlias: #DummyTbl with const DummyTbl& to CountedConstPtrOrConstPtr<DummyTbl> member. No duplication (0/0)" );
645 trace.info() << "D: d1.value() = " << c07.value() << std::endl;
646 ++nb; nbok += DummyTbl::nbCreated==0 ? 1 : 0;
647 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
648 trace.info() << "(" << nbok << "/" << nb << ")"
649 << " nbCreated=" << DummyTbl::nbCreated
650 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
651 trace.endBlock();
652
653 trace.beginBlock ( "ConstAlias: #DummyTbl with CountedPtr<DummyTbl> to CountedConstPtrOrConstPtr<DummyTbl> member. No duplication (0/0)" );
654 ConstAliasToCountedConstPtrOrConstPtrMember c37( counted_a1 ); // 0/0
655 trace.info() << "D: d1.value() = " << c37.value() << std::endl;
656 ++nb; nbok += DummyTbl::nbCreated==0 ? 1 : 0;
657 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
658 trace.info() << "(" << nbok << "/" << nb << ")"
659 << " nbCreated=" << DummyTbl::nbCreated
660 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
661 trace.endBlock();
662
663 // These lines do not compile.
664 // DummyTbl& ref_a1 = ConstAlias<DummyTbl>( a1 );
665 // DummyTbl* ptr_a1( & ConstAlias<DummyTbl>( a1 ) );
666
667 trace.endBlock();
668
669 delete ptr_a2;
670 return nbok == nb;
671}
672
692{
693 unsigned int nb = 0;
694 unsigned int nbok = 0;
695 DummyTbl a1( 50, 10 ); // +1/0
696 CowPtr<DummyTbl> cow_a1( new DummyTbl( 50, 5 ) ); // +1/0
697 CountedPtr<DummyTbl> counted_a1( new DummyTbl( 50, 12 ) ); // +1/0
699 trace.beginBlock ( "Testing class Clone." );
700
701
702 trace.beginBlock ( "Clone: #DummyTbl with (const DummyTbl &) to DummyTbl member. Duplication (+1/0)" );
703 CloneToValueMember c00( a1 ); // +1/0
704 trace.info() << "D: d1.value() = " << c00.value() << std::endl;
705 ++nb; nbok += DummyTbl::nbCreated==1 ? 1 : 0;
706 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
707 trace.info() << "(" << nbok << "/" << nb << ")"
708 << " nbCreated=" << DummyTbl::nbCreated
709 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
710 trace.endBlock();
711
712 trace.beginBlock ( "Clone: #DummyTbl with (CountedPtr<DummyTbl>) to DummyTbl member. Duplication (+1/0)" );
713 CloneToValueMember c30( a1 ); // +1/0
714 trace.info() << "D: d1.value() = " << c30.value() << std::endl;
715 ++nb; nbok += DummyTbl::nbCreated==2 ? 1 : 0;
716 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
717 trace.info() << "(" << nbok << "/" << nb << ")"
718 << " nbCreated=" << DummyTbl::nbCreated
719 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
720 trace.endBlock();
721
722 trace.beginBlock ( "Clone: #DummyTbl with (const DummyTbl &) to CountedPtr<DummyTbl> member. Duplication (+1/0)" );
723 CloneToCountedMember c03( a1 ); // +1/0
724 trace.info() << "D: d1.value() = " << c03.value() << std::endl;
725 ++nb; nbok += DummyTbl::nbCreated==3 ? 1 : 0;
726 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
727 trace.info() << "(" << nbok << "/" << nb << ")"
728 << " nbCreated=" << DummyTbl::nbCreated
729 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
730 trace.endBlock();
731
732 trace.beginBlock ( "Clone: #DummyTbl with (const DummyTbl &) to CowPtr<DummyTbl> member. Duplication (+1/0)" );
733 CloneToCowMember c02( a1 ); // +1/0
734 trace.info() << "D: d1.value() = " << c02.value() << std::endl;
735 ++nb; nbok += DummyTbl::nbCreated==4 ? 1 : 0;
736 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
737 trace.info() << "(" << nbok << "/" << nb << ")"
738 << " nbCreated=" << DummyTbl::nbCreated
739 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
740 trace.endBlock();
741
742 trace.beginBlock ( "Clone: #DummyTbl with (CowPtr<DummyTbl> &) to CowPtr<DummyTbl> member. Lazy duplication (0/0)" );
743 CloneToCowMember c22( cow_a1 ); // +0/0
744 trace.info() << "D: d1.value() = " << c22.value() << std::endl;
745 ++nb; nbok += DummyTbl::nbCreated==4 ? 1 : 0;
746 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
747 trace.info() << "(" << nbok << "/" << nb << ")"
748 << " nbCreated=" << DummyTbl::nbCreated
749 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
750 c22.setValue( 17 );
751 trace.info() << "D: d1.setValue( 17 ) -> now duplicating " << std::endl;
752 trace.info() << "D: d1.value() = " << c22.value() << std::endl;
753 ++nb; nbok += DummyTbl::nbCreated==5 ? 1 : 0;
754 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
755 trace.info() << "(" << nbok << "/" << nb << ")"
756 << " nbCreated=" << DummyTbl::nbCreated
757 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
758 trace.endBlock();
759
760 trace.beginBlock ( "Clone: #DummyTbl with (CountedPtr<DummyTbl> &) to CowPtr<DummyTbl> member. Lazy duplication (0/0)" );
761 CloneToCowMember c32( counted_a1 ); // +0/0
762 trace.info() << "D: d1.value() = " << c32.value() << std::endl;
763 ++nb; nbok += DummyTbl::nbCreated==5 ? 1 : 0;
764 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
765 trace.info() << "(" << nbok << "/" << nb << ")"
766 << " nbCreated=" << DummyTbl::nbCreated
767 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
768 c32.setValue( 21 );
769 trace.info() << "D: d1.setValue( 21 ) -> now duplicating " << std::endl;
770 trace.info() << "D: d1.value() = " << c32.value() << std::endl;
771 ++nb; nbok += DummyTbl::nbCreated==6 ? 1 : 0;
772 ++nb; nbok += DummyTbl::nbDeleted==0 ? 1 : 0;
773 trace.info() << "(" << nbok << "/" << nb << ")"
774 << " nbCreated=" << DummyTbl::nbCreated
775 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
776 trace.endBlock();
777
778 trace.beginBlock ( "Clone: #DummyTbl with (DummyTbl*) to DummyTbl member. Acquisition, duplication, delete (+2/+1)" );
779 CloneToValueMember c10( new DummyTbl( 50, 2 ) ); // +2/+1
780 trace.info() << "D: d1.value() = " << c10.value() << std::endl;
781 ++nb; nbok += DummyTbl::nbCreated==8 ? 1 : 0;
782 ++nb; nbok += DummyTbl::nbDeleted==1 ? 1 : 0;
783 trace.info() << "(" << nbok << "/" << nb << ")"
784 << " nbCreated=" << DummyTbl::nbCreated
785 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
786 trace.endBlock();
787
788 trace.beginBlock ( "Clone: #DummyTbl with (DummyTbl*) to CowPtr<DummyTbl> member. Acquisition, no duplication (+1/0)" );
789 CloneToCowMember c12( new DummyTbl( 50, 15 ) ); // +1/0
790 trace.info() << "D: d1.value() = " << c12.value() << std::endl;
791 ++nb; nbok += DummyTbl::nbCreated==9 ? 1 : 0;
792 ++nb; nbok += DummyTbl::nbDeleted==1 ? 1 : 0;
793 trace.info() << "(" << nbok << "/" << nb << ")"
794 << " nbCreated=" << DummyTbl::nbCreated
795 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
796 trace.endBlock();
797
798 trace.beginBlock ( "Clone: #DummyTbl with (const DummyTbl&) to DummyTbl* member. Duplication (+1/0)" );
799 CloneToPtrMember c01( a1 ); // +1/0
800 trace.info() << "D: d1.value() = " << c01.value() << std::endl;
801 ++nb; nbok += DummyTbl::nbCreated==10 ? 1 : 0;
802 ++nb; nbok += DummyTbl::nbDeleted==1 ? 1 : 0;
803 trace.info() << "(" << nbok << "/" << nb << ")"
804 << " nbCreated=" << DummyTbl::nbCreated
805 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
806 trace.endBlock();
807
808 trace.beginBlock ( "Clone: #DummyTbl with (DummyTbl*) to DummyTbl* member. Acquisition (+1/0)" );
809 CloneToPtrMember c11( new DummyTbl( 50, 42 ) ); // +1/0
810 trace.info() << "D: d1.value() = " << c11.value() << std::endl;
811 ++nb; nbok += DummyTbl::nbCreated==11 ? 1 : 0;
812 ++nb; nbok += DummyTbl::nbDeleted==1 ? 1 : 0;
813 trace.info() << "(" << nbok << "/" << nb << ")"
814 << " nbCreated=" << DummyTbl::nbCreated
815 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
816 trace.endBlock();
817
818 trace.beginBlock ( "Clone: #DummyTbl with (CowPtr<DummyTbl>) to DummyTbl* member. Duplication (+1/0)" );
819 CloneToPtrMember c21( cow_a1 ); // +1/0
820 trace.info() << "D: d1.value() = " << c21.value() << std::endl;
821 ++nb; nbok += DummyTbl::nbCreated==12 ? 1 : 0;
822 ++nb; nbok += DummyTbl::nbDeleted==1 ? 1 : 0;
823 trace.info() << "(" << nbok << "/" << nb << ")"
824 << " nbCreated=" << DummyTbl::nbCreated
825 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
826 trace.endBlock();
827
828 trace.beginBlock ( "Clone: #DummyTbl with (CountedPtr<DummyTbl>) to DummyTbl* member. Duplication (+1/0)" );
829 CloneToPtrMember c31( counted_a1 ); // +1/0
830 trace.info() << "D: d1.value() = " << c31.value() << std::endl;
831 ++nb; nbok += DummyTbl::nbCreated==13 ? 1 : 0;
832 ++nb; nbok += DummyTbl::nbDeleted==1 ? 1 : 0;
833 trace.info() << "(" << nbok << "/" << nb << ")"
834 << " nbCreated=" << DummyTbl::nbCreated
835 << " nbDeleted=" << DummyTbl::nbDeleted << std::endl;
836 trace.endBlock();
837
838 trace.beginBlock ( "Clone: #DummyTbl with (DummyTbl &&) to DummyTbl member. Duplication by move (+2/+1/+1)" );
839 CloneToValueMember c40( DummyTbl( 50, -4 ) ); // +2/+1/+1
840 trace.info() << "D: d1.value() = " << c40.value() << std::endl;
841 ++nb; nbok += DummyTbl::nbCreated==15 ? 1 : 0;
842 ++nb; nbok += DummyTbl::nbDeleted==2 ? 1 : 0;
843 ++nb; nbok += DummyTbl::nbMoved==1 ? 1 : 0;
844 trace.info() << "(" << nbok << "/" << nb << ")"
845 << " nbCreated=" << DummyTbl::nbCreated
846 << " nbDeleted=" << DummyTbl::nbDeleted
847 << " nbMoved=" << DummyTbl::nbMoved
848 << std::endl;
849 trace.endBlock();
850
851 trace.beginBlock ( "Clone: #DummyTbl with (DummyTbl &&) to CowPtr<DummyTbl> member. Duplication by move (+2/+1/+1)" );
852 CloneToCowMember c42( DummyTbl( 50, -9 ) ); // +2/+1/+1
853 trace.info() << "D: d1.value() = " << c42.value() << std::endl;
854 ++nb; nbok += DummyTbl::nbCreated==17 ? 1 : 0;
855 ++nb; nbok += DummyTbl::nbDeleted==3 ? 1 : 0;
856 ++nb; nbok += DummyTbl::nbMoved==2 ? 1 : 0;
857 trace.info() << "(" << nbok << "/" << nb << ")"
858 << " nbCreated=" << DummyTbl::nbCreated
859 << " nbDeleted=" << DummyTbl::nbDeleted
860 << " nbMoved=" << DummyTbl::nbMoved
861 << std::endl;
862 trace.endBlock();
863
864 trace.beginBlock ( "Clone: #DummyTbl with (DummyTbl &&) to DummyTbl* member. Duplication by move (+2/+1/+1)" );
865 CloneToCowMember c41( DummyTbl( 50, -12 ) ); // +2/+1/+1
866 trace.info() << "D: d1.value() = " << c41.value() << std::endl;
867 ++nb; nbok += DummyTbl::nbCreated==19 ? 1 : 0;
868 ++nb; nbok += DummyTbl::nbDeleted==4 ? 1 : 0;
869 ++nb; nbok += DummyTbl::nbMoved==3 ? 1 : 0;
870 trace.info() << "(" << nbok << "/" << nb << ")"
871 << " nbCreated=" << DummyTbl::nbCreated
872 << " nbDeleted=" << DummyTbl::nbDeleted
873 << " nbMoved=" << DummyTbl::nbMoved
874 << std::endl;
875 trace.endBlock();
876
877 trace.endBlock();
878
879 return nbok == nb;
880}
881
883{
884 unsigned int nb = 0;
885 unsigned int nbok = 0;
886 int size = 10;
887 trace.beginBlock ( "Testing Clone timings." );
888
889 trace.beginBlock ( "Total perimeter of triangles with by-value parameter passing." );
890 double t1 = computeTriangles<TriangleByValue>( size );
891 trace.info() << "Perimeter is " << t1 << std::endl;
892 ++nb; nbok += Point::nbCreated == Point::nbDeleted ? 1 : 0;
893 trace.info() << "(" << nbok << "/" << nb << ")"
894 << " Point nbCreated=" << Point::nbCreated
895 << " nbDeleted=" << Point::nbDeleted << std::endl;
896 int nbC = Point::nbCreated;
897 Point::reset();
898 trace.endBlock();
899 trace.beginBlock ( "Total perimeter of triangles with by-const reference parameter passing." );
901 trace.info() << "Perimeter is " << t2 << std::endl;
902 ++nb; nbok += Point::nbCreated == Point::nbDeleted ? 1 : 0;
903 ++nb; nbok += Point::nbCreated < nbC ? 1 : 0;
904 trace.info() << "(" << nbok << "/" << nb << ")"
905 << " Point nbCreated=" << Point::nbCreated
906 << " nbDeleted=" << Point::nbDeleted << std::endl;
907 Point::reset();
908 trace.endBlock();
909 trace.beginBlock ( "Total perimeter of triangles with by Clone parameter passing." );
910 double t4 = computeTriangles<TriangleByClone>( size );
911 trace.info() << "Perimeter is " << t4 << std::endl;
912 ++nb; nbok += Point::nbCreated == Point::nbDeleted ? 1 : 0;
913 ++nb; nbok += Point::nbCreated <= nbC ? 1 : 0;
914 trace.info() << "(" << nbok << "/" << nb << ")"
915 << " Point nbCreated=" << Point::nbCreated
916 << " nbDeleted=" << Point::nbDeleted << std::endl;
917 Point::reset();
918 trace.endBlock();
919 trace.beginBlock ( "Total perimeter of triangles with by CloneAndCow parameter passing." );
920 double t5 = computeTriangles<TriangleByCloneAndCow>( size );
921 trace.info() << "Perimeter is " << t5 << std::endl;
922 ++nb; nbok += Point::nbCreated == Point::nbDeleted ? 1 : 0;
923 ++nb; nbok += Point::nbCreated < nbC ? 1 : 0;
924 trace.info() << "(" << nbok << "/" << nb << ")"
925 << " Point nbCreated=" << Point::nbCreated
926 << " nbDeleted=" << Point::nbDeleted << std::endl;
927 Point::reset();
928 trace.endBlock();
929 trace.beginBlock ( "Total perimeter of triangles with CowPtr by CloneAndCow parameter passing." );
931 trace.info() << "Perimeter is " << t6 << std::endl;
932 ++nb; nbok += Point::nbCreated == Point::nbDeleted ? 1 : 0;
933 ++nb; nbok += Point::nbCreated < nbC ? 1 : 0;
934 trace.info() << "(" << nbok << "/" << nb << ")"
935 << " Point nbCreated=" << Point::nbCreated
936 << " nbDeleted=" << Point::nbDeleted << std::endl;
937 Point::reset();
938 trace.endBlock();
939
940 trace.endBlock();
941
942 return nb == nbok;
943}
944
945int main()
946{
947 bool ok = true
948 && testCloneCases()
950 && testAliasCases()
952
953 return ok ? 0 : 1;
954}
955
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition Alias.h:183
Aim: This class encapsulates its parameter class to indicate that the given parameter is required to ...
Definition Clone.h:266
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition ConstAlias.h:187
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
Aim: Copy on write shared pointer.
Definition CowPtr.h:68
NClone(CowPtr< T > ptr)
NClone(CountedPtr< T > ptr)
NClone(const NClone &)
NClone(const T &t)
CowPtr< T > myCowPtr
The copy-on-write pointer to the T object that is to be duplicated.
NClone & operator=(const NClone &other)
double norm() const
double _y
MyPointD(int x, int y)
double _x
static int nbCreated
MyPointD(const MyPointD &other)
MyPointD operator-(const MyPointD &other) const
static void reset()
static int nbDeleted
MyPointD(double x, double y)
MyPoint operator-(const MyPoint &other) const
static void reset()
MyPoint(int x, int y)
MyPoint(const MyPoint &other)
static int nbCreated
static int nbDeleted
double norm() const
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
STL namespace.
const DummyTbl & myDummyTbl
AliasToConstRefMember(Alias< DummyTbl > a1)
CountedPtrOrPtr< DummyTbl > myDummyTbl
AliasToCountedPtrOrPtrMember(Alias< DummyTbl > a1)
DummyTbl * myDummyTbl
int value() const
AliasToPtrMember(Alias< DummyTbl > a1)
int value() const
DummyTbl & myDummyTbl
AliasToRefMember(Alias< DummyTbl > a1)
CloneToCountedMember(Clone< DummyTbl > a1)
CountedPtr< DummyTbl > myDummyTbl
void setValue(int v)
CloneToCowMember(Clone< DummyTbl > a1)
CowPtr< DummyTbl > myDummyTbl
int value() const
CloneToPtrMember(Clone< DummyTbl > a1)
DummyTbl * myDummyTbl
int value() const
void setValue(int v)
CloneToValueMember(Clone< DummyTbl > a1)
const DummyTbl * myDummyTbl
ConstAliasToConstPtrMember(ConstAlias< DummyTbl > a1)
const DummyTbl & myDummyTbl
ConstAliasToConstRefMember(ConstAlias< DummyTbl > a1)
ConstAliasToCountedConstPtrOrConstPtrMember(ConstAlias< DummyTbl > a1)
CountedConstPtrOrConstPtr< DummyTbl > myDummyTbl
static int nbDeleted
static void reset()
DummyTbl(const DummyTbl &a)
int value() const
void setValue(int v) const
static int nbMoved
DummyTbl & operator=(const DummyTbl &a)
DummyTbl(DummyTbl &&a) noexcept
Element * data
static int nbCreated
DummyTbl(int i, int val)
CowPtr< Point > _b
CowPtr< Point > _a
CowPtr< Point > _c
double perimeter() const
TriangleByCloneAndCow(Clone< Point > a, Clone< Point > b, Clone< Point > c)
double perimeter() const
TriangleByClone(Clone< Point > a, Clone< Point > b, Clone< Point > c)
TriangleByConstReference(const Point &a, const Point &b, const Point &c)
TriangleByValue(Point a, Point b, Point c)
double perimeter() const
bool testAliasCases()
MyPointD Point
bool testCloneTimings()
bool testCloneCases()
double computeTriangles(int size)
bool testConstAliasCases()
double computeTrianglesByCowPtr(int size)
int main()
HalfEdgeDataStructure::Triangle Triangle