Loading [MathJax]/extensions/TeX/AMSsymbols.js
DGtal 2.0.0
Clone.h
1
16
17#pragma once
18
30
31#if defined(Clone_RECURSES)
32#error Recursive header files inclusion detected in Clone.h
33#else // defined(Clone_RECURSES)
35#define Clone_RECURSES
36
37#if !defined Clone_h
39#define Clone_h
40
42// Inclusions
43#include <iostream>
44#include "DGtal/base/Common.h"
45#include "DGtal/base/CountedPtr.h"
46#include "DGtal/base/CowPtr.h"
48
49namespace DGtal
50{
51
53 // template class Clone
264 template <typename T>
265 class Clone
266 {
267 // ----------------------- Internal classes ------------------------------
268 protected:
269
279 void display( const std::string& method, Parameter p ) const
280 {
281 std::string sp;
282 switch (p) {
283 case CONST_LEFT_VALUE_REF: sp = "CONST_LEFT_VALUE_REF"; break;
284 case LEFT_VALUE_REF: sp = "LEFT_VALUE_REF"; break;
285 case PTR: sp = "PTR"; break;
286 case CONST_PTR: sp = "CONST_PTR"; break;
287 case COW_PTR: sp = "COW_PTR"; break;
288 case COUNTED_PTR: sp = "COUNTED_PTR"; break;
289 case RIGHT_VALUE_REF: sp = "RIGHT_VALUE_REF"; break;
290 case COUNTED_PTR_OR_PTR: sp = "COUNTED_PTR_OR_PTR"; break;
291 case COUNTED_CONST_PTR_OR_CONST_PTR: sp = "COUNTED_CONST_PTR_OR_CONST_PTR"; break;
292 default: sp = "UNKNOWN"; break;
293 }
294 trace.info() << "[Clone<T>::" << method << " param="
295 << sp << "]" << std::endl;
296
297 }
298
299 struct TempPtr {
304 inline TempPtr( T* ptr ) : _ptr( ptr ) {}
308 inline ~TempPtr() { ASSERT( _ptr != 0 ); delete _ptr; }
311 };
312
313 // ----------------------- Standard services ------------------------------
314 public:
315
319 inline ~Clone() {}
320
325 inline Clone( const Clone & other )
326 : myParam( other.myParam ), myPtr( other.myPtr ) {}
327
333 inline Clone( const T & t )
334 : myParam( CONST_LEFT_VALUE_REF ), myPtr( static_cast<const void*>( &t ) ) {}
335
342 inline Clone( T* ptrT )
343 : myParam( PTR ), myPtr( static_cast<const void*>( ptrT ) ) {}
344
351 inline Clone( const CowPtr<T> & ptrT )
352 : myParam( COW_PTR ), myPtr( static_cast<const void*>( &ptrT ) ) {}
353
360 inline Clone( const CountedPtr<T> & ptrT )
361 : myParam( COUNTED_PTR ), myPtr( static_cast<const void*>( &ptrT ) ) {}
362
369 inline Clone( T && t )
370 : myParam( RIGHT_VALUE_REF ), myPtr( static_cast<const void*>( &t ) ) {}
371
372
383 inline operator T() const
384 {
385 // display( "operator T() const", myParam );
386 switch( myParam ) {
388 return T( * static_cast< const T* >( myPtr ) );
389 case PTR: {
390 TempPtr tmp( const_cast< T* >( static_cast< const T* >( myPtr ) ) );
391 return T( * static_cast< const T* >( myPtr ) );
392 } // destroy acquired pointer.
393 case COW_PTR:
394 return T( * static_cast< const CowPtr<T>* >( myPtr )->get() );
395 case COUNTED_PTR:
396 return T( * static_cast< const CountedPtr<T>* >( myPtr )->get() );
397 case RIGHT_VALUE_REF:
398 return T( std::move( * const_cast<T*>( static_cast< const T* >( myPtr ) ) ) );
399 default: ASSERT( false && "[Clone::operator T() const] Invalid cast for given type. " );
400 return T( * static_cast< const T* >( myPtr ) );
401 }
402 }
403
414 inline operator CowPtr<T>() const
415 {
416 // display( "operator CowPtr<T>() const", myParam );
417 switch( myParam ) {
419 return CowPtr<T>( new T( * static_cast< const T* >( myPtr ) ) );
420 case PTR:
421 return CowPtr<T>( const_cast<T*>( static_cast< const T* >( myPtr ) ) );
422 case COW_PTR:
423 return CowPtr<T>( * static_cast< const CowPtr<T>* >( myPtr ) );
424 case COUNTED_PTR:
425 return CowPtr<T>( * static_cast< const CountedPtr<T>* >( myPtr ), true );
426 case RIGHT_VALUE_REF:
427 return CowPtr<T>( new T( std::move( * const_cast<T*>( static_cast< const T* >( myPtr ) ) ) ) );
428 default: ASSERT( false && "[Clone::operator CowPtr<T>() const] Invalid cast for given type. " );
429 return CowPtr<T>( 0 );
430 }
431 }
432
443 inline operator CountedPtr<T>() const
444 {
445 // display( "operator CountedPtr<T>() const", myParam );
446 switch( myParam ) {
448 return CountedPtr<T>( new T( * static_cast< const T* >( myPtr ) ) );
449 case PTR:
450 return CountedPtr<T>( const_cast<T*>( static_cast< const T* >( myPtr ) ) );
451 case COW_PTR:
452 return CountedPtr<T>( new T( * static_cast< const CowPtr<T>* >( myPtr )->get() ) );
453 case COUNTED_PTR:
454 return CountedPtr<T>( * static_cast< const CountedPtr<T>* >( myPtr ) );
455 case RIGHT_VALUE_REF:
456 return CountedPtr<T>( new T( std::move( * const_cast<T*>( static_cast< const T* >( myPtr ) ) ) ) );
457 default: ASSERT( false && "[Clone::operator CountedPtr<T>() const] Invalid cast for given type. " );
458 return CountedPtr<T>( 0 );
459 }
460 }
461
475 inline T* operator&() const
476 {
477 // display( "T* operator &() const", myParam );
478 switch( myParam ) {
480 return new T( * static_cast< const T* >( myPtr ) );
481 case PTR:
482 return const_cast<T*>( static_cast< const T* >( myPtr ) );
483 case COW_PTR:
484 return new T( *( static_cast< const CowPtr<T>* >( myPtr )->get() ) );
485 case COUNTED_PTR:
486 return new T( *( static_cast< const CountedPtr<T>* >( myPtr )->get() ) );
487 case RIGHT_VALUE_REF:
488 return new T( std::move( * const_cast<T*>( static_cast< const T* >( myPtr ) ) ) );
489 default: ASSERT( false && "[T* Clone::operator&() const] Invalid address for given type. " );
490 return 0;
491 }
492 }
493
494 // ------------------------- Private Datas --------------------------------
495 private:
499 const void* const myPtr;
500
501
502 // ------------------------- Hidden services ------------------------------
503 private:
504
510
511
518 Clone & operator= ( const Clone & other );
519
520 // ------------------------- Internals ------------------------------------
521 private:
522
523 }; // end of class Clone
524
525} // namespace DGtal
526
527
529// Includes inline functions.
530
531// //
533
534#endif // !defined Clone_h
535
536#undef Clone_RECURSES
537#endif // else defined(Clone_RECURSES)
Clone & operator=(const Clone &other)
T * operator&() const
Definition Clone.h:475
Clone(T *ptrT)
Definition Clone.h:342
const Parameter myParam
Characterizes the type of the input parameter at clone instanciation.
Definition Clone.h:497
void display(const std::string &method, Parameter p) const
Definition Clone.h:279
Clone(const CowPtr< T > &ptrT)
Definition Clone.h:351
Clone(const Clone &other)
Definition Clone.h:325
const void *const myPtr
Stores the address of the input parameter for further use.
Definition Clone.h:499
Clone(const CountedPtr< T > &ptrT)
Definition Clone.h:360
Clone(const T &t)
Definition Clone.h:333
Clone(T &&t)
Definition Clone.h:369
Parameter
Internal class that allows to distinguish the different types of parameters.
Definition Clone.h:271
@ COUNTED_PTR_OR_PTR
Definition Clone.h:272
@ LEFT_VALUE_REF
Definition Clone.h:271
@ COUNTED_PTR
Definition Clone.h:272
@ COUNTED_CONST_PTR_OR_CONST_PTR
Definition Clone.h:273
@ RIGHT_VALUE_REF
Definition Clone.h:272
@ CONST_LEFT_VALUE_REF
Definition Clone.h:271
Aim: Smart pointer based on reference counts.
Definition CountedPtr.h:80
Aim: Copy on write shared pointer.
Definition CowPtr.h:68
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Internal class that is used for a late deletion of an acquired pointer.
Definition Clone.h:299
T * _ptr
Acquired pointer.
Definition Clone.h:310