DGtal  0.9.2
Clone.h
1 
17 #pragma once
18 
31 #if defined(Clone_RECURSES)
32 #error Recursive header files inclusion detected in Clone.h
33 #else // defined(Clone_RECURSES)
34 
35 #define Clone_RECURSES
36 
37 #if !defined Clone_h
38 
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 
49 namespace DGtal
50 {
51 
53  // template class Clone
265  template <typename T>
266  class Clone
267  {
268  // ----------------------- Internal classes ------------------------------
269  protected:
270 
275 
277  struct TempPtr {
282  inline TempPtr( T* ptr ) : _ptr( ptr ) {}
286  inline ~TempPtr() { ASSERT( _ptr != 0 ); delete _ptr; }
288  T* _ptr;
289  };
290 
291  // ----------------------- Standard services ------------------------------
292  public:
293 
297  inline ~Clone() {}
298 
303  inline Clone( const Clone & other )
304  : myParam( other.myParam ), myPtr( other.myPtr ) {}
305 
311  inline Clone( const T & t )
312  : myParam( CONST_LEFT_VALUE_REF ), myPtr( static_cast<const void*>( &t ) ) {}
313 
320  inline Clone( T* ptrT )
321  : myParam( PTR ), myPtr( static_cast<const void*>( ptrT ) ) {}
322 
329  inline Clone( const CowPtr<T> & ptrT )
330  : myParam( COW_PTR ), myPtr( static_cast<const void*>( &ptrT ) ) {}
331 
338  inline Clone( const CountedPtr<T> & ptrT )
339  : myParam( COUNTED_PTR ), myPtr( static_cast<const void*>( &ptrT ) ) {}
340 
347  inline Clone( T && t )
348  : myParam( RIGHT_VALUE_REF ), myPtr( static_cast<const void*>( &t ) ) {}
349 
350 
361  inline operator T() const
362  {
363  switch( myParam ) {
365  return T( * static_cast< const T* >( myPtr ) );
366  case PTR: {
367  TempPtr tmp( const_cast< T* >( static_cast< const T* >( myPtr ) ) );
368  return T( * static_cast< const T* >( myPtr ) );
369  } // destroy acquired pointer.
370  case COW_PTR:
371  return T( * static_cast< const CowPtr<T>* >( myPtr )->get() );
372  case COUNTED_PTR:
373  return T( * static_cast< const CountedPtr<T>* >( myPtr )->get() );
374  case RIGHT_VALUE_REF:
375  return T( std::move( * const_cast<T*>( static_cast< const T* >( myPtr ) ) ) );
376  default: ASSERT( false && "[Clone::operator T() const] Invalid cast for given type. " );
377  return T( * static_cast< const T* >( myPtr ) );
378  }
379  }
380 
391  inline operator CowPtr<T>() const
392  {
393  switch( myParam ) {
395  return CowPtr<T>( new T( * static_cast< const T* >( myPtr ) ) );
396  case PTR:
397  return CowPtr<T>( const_cast<T*>( static_cast< const T* >( myPtr ) ) );
398  case COW_PTR:
399  return CowPtr<T>( * static_cast< const CowPtr<T>* >( myPtr ) );
400  case COUNTED_PTR:
401  return CowPtr<T>( * static_cast< const CountedPtr<T>* >( myPtr ) );
402  case RIGHT_VALUE_REF:
403  return CowPtr<T>( new T( std::move( * const_cast<T*>( static_cast< const T* >( myPtr ) ) ) ) );
404  default: ASSERT( false && "[Clone::operator CowPtr<T>() const] Invalid cast for given type. " );
405  return CowPtr<T>( 0 );
406  }
407  }
408 
422  inline T* operator&() const
423  {
424  switch( myParam ) {
426  return new T( * static_cast< const T* >( myPtr ) );
427  case PTR:
428  return const_cast<T*>( static_cast< const T* >( myPtr ) );
429  case COW_PTR:
430  return new T( *( static_cast< const CowPtr<T>* >( myPtr )->get() ) );
431  case COUNTED_PTR:
432  return new T( *( static_cast< const CountedPtr<T>* >( myPtr )->get() ) );
433  case RIGHT_VALUE_REF:
434  return new T( std::move( * const_cast<T*>( static_cast< const T* >( myPtr ) ) ) );
435  default: ASSERT( false && "[T* Clone::operator&() const] Invalid address for given type. " );
436  return 0;
437  }
438  }
439 
440  // ------------------------- Private Datas --------------------------------
441  private:
445  const void* const myPtr;
446 
447 
448  // ------------------------- Hidden services ------------------------------
449  private:
450 
455  Clone();
456 
457 
464  Clone & operator= ( const Clone & other );
465 
466  // ------------------------- Internals ------------------------------------
467  private:
468 
469  }; // end of class Clone
470 
471 } // namespace DGtal
472 
473 
475 // Includes inline functions.
476 
477 // //
479 
480 #endif // !defined Clone_h
481 
482 #undef Clone_RECURSES
483 #endif // else defined(Clone_RECURSES)
Clone(T *ptrT)
Definition: Clone.h:320
Aim: Smart pointer based on reference counts.
Definition: CountedPtr.h:79
Clone(T &&t)
Definition: Clone.h:347
Clone(const T &t)
Definition: Clone.h:311
Parameter
Internal class that allows to distinguish the different types of parameters.
Definition: Clone.h:272
Clone & operator=(const Clone &other)
Clone(const CountedPtr< T > &ptrT)
Definition: Clone.h:338
Clone(const Clone &other)
Definition: Clone.h:303
Aim: This class encapsulates its parameter class to indicate that the given parameter is required to ...
Definition: Clone.h:266
T * _ptr
Acquired pointer.
Definition: Clone.h:288
Internal class that is used for a late deletion of an acquired pointer.
Definition: Clone.h:277
const Parameter myParam
Characterizes the type of the input parameter at clone instanciation.
Definition: Clone.h:443
DGtal is the top-level namespace which contains all DGtal functions and types.
T * operator&() const
Definition: Clone.h:422
const void *const myPtr
Stores the address of the input parameter for further use.
Definition: Clone.h:445
Aim: Copy on write shared pointer.
Definition: CowPtr.h:67
Clone(const CowPtr< T > &ptrT)
Definition: Clone.h:329