DGtal  1.3.beta
ConstAlias.h
1 
17 #pragma once
18 
31 #if defined(ConstAlias_RECURSES)
32 #error Recursive header files inclusion detected in ConstAlias.h
33 #else // defined(ConstAlias_RECURSES)
34 
35 #define ConstAlias_RECURSES
36 
37 #if !defined ConstAlias_h
38 
39 #define ConstAlias_h
40 
42 // Inclusions
43 #include <iostream>
44 #include "DGtal/base/Common.h"
45 #include "DGtal/base/CountedPtr.h"
46 #include "DGtal/base/CountedPtrOrPtr.h"
47 #include "DGtal/base/CowPtr.h"
48 #include "DGtal/base/CountedConstPtrOrConstPtr.h"
50 
51 namespace DGtal
52 {
53 
55  // template class ConstAlias
185  template <typename T>
187  {
188 
189  // ----------------------- Internal classes ------------------------------
190  protected:
191 
196 
197  // ----------------------- Standard services ------------------------------
198  public:
199 
204  ConstAlias( const ConstAlias & other ) = default;
205 
209  inline ~ConstAlias() {}
210 
216  inline ConstAlias( const T& t )
217  : myParam( CONST_LEFT_VALUE_REF ), myPtr( static_cast<const void*>( &t ) ) {}
218 
224  inline ConstAlias( const T* ptrT )
225  : myParam( CONST_PTR ), myPtr( static_cast<const void*>( ptrT ) ) {}
226 
227 
233  ConstAlias( const CowPtr<T>& ) = delete;
234 
235 
241  inline ConstAlias( const CountedPtr<T>& shT )
242  : myParam( COUNTED_PTR ), myPtr( static_cast<const void*>( &shT ) ) {}
243 
249  inline ConstAlias( const CountedPtrOrPtr<T>& shT )
250  : myParam( COUNTED_PTR_OR_PTR ), myPtr( static_cast<const void*>( &shT ) ) {}
251 
258  : myParam( COUNTED_CONST_PTR_OR_CONST_PTR ), myPtr( static_cast<const void*>( &shT ) ) {}
259 
265  ConstAlias( T&& ) = delete;
266 
273  inline operator const T&() const
274  {
275  switch( myParam ) {
277  case CONST_PTR:
278  return *( static_cast< const T* >( myPtr ) );
279  default: ASSERT( false && "[ConstAlias::operator const T&() const] Invalid cast for given type. Consider passing a const left-value reference or a const pointer as a parameter." );
280  return *( static_cast< const T* >( myPtr ) );
281  }
282  }
283 
290  inline const T* operator&() const
291  {
292  switch( myParam ) {
294  case CONST_PTR:
295  return static_cast< const T* >( myPtr );
296  default: ASSERT( false && "[const T* ConstAlias::operator&() const] Invalid address operator for given type. Consider passing a const left-value reference or a const pointer as a parameter." );
297  return static_cast< const T* >( myPtr );
298  }
299  }
300 
311  inline operator CountedConstPtrOrConstPtr<T>() const
312  {
313  switch( myParam ) {
315  case CONST_PTR:
316  return CountedConstPtrOrConstPtr<T>( static_cast< const T* >( myPtr ), false );
317  case COUNTED_PTR:
318  return CountedConstPtrOrConstPtr<T>( *( static_cast< const CountedPtr<T>* >( myPtr ) ) );
319  case COUNTED_PTR_OR_PTR:
320  return CountedConstPtrOrConstPtr<T>( *( static_cast< const CountedPtrOrPtr<T>* >( myPtr ) ) );
322  return CountedConstPtrOrConstPtr<T>( *( static_cast< const CountedPtrOrPtr<T>* >( myPtr ) ) );
323  default: ASSERT( false && "[ConstAlias::operator CowPtr<T>() const] Invalid cast for given type. Consider passing a CountedPtr or a CowPtr as a parameter." );
324  return CountedConstPtrOrConstPtr<T>( 0, false );
325  }
326  }
327 
328  inline const T* operator->() const
329  {
330  switch( myParam ) {
332  case CONST_PTR:
333  return static_cast< const T* >( myPtr );
334  case COUNTED_PTR:
335  return ( static_cast< const CountedPtr<T>* >( myPtr ) )->operator->();
336  case COUNTED_PTR_OR_PTR:
337  return ( static_cast< const CountedPtrOrPtr<T>* >( myPtr ) )->operator->();
339  return ( static_cast< const CountedConstPtrOrConstPtr<T>* >( myPtr ) )->operator->();
340  default: ASSERT( false && "[ConstAlias::operator->() const] Invalid cast for given type. Consider passing a CountedPtr or a CowPtr as a parameter." );
341  return 0;
342  }
343  }
344 
345  // ------------------------- Private Datas --------------------------------
346  private:
350  const void* const myPtr;
351 
352 
353  // ------------------------- Internals ------------------------------------
354  private:
355 
362  ConstAlias & operator= ( const ConstAlias & other );
363 
364  }; // end of class ConstAlias
365 
366 } // namespace DGtal
367 
368 
370 // Includes inline functions.
371 
372 // //
374 
375 #endif // !defined ConstAlias_h
376 
377 #undef ConstAlias_RECURSES
378 #endif // else defined(ConstAlias_RECURSES)
DGtal::ConstAlias::ConstAlias
ConstAlias(const ConstAlias &other)=default
DGtal::ConstAlias::CONST_PTR
@ CONST_PTR
Definition: ConstAlias.h:193
DGtal::ConstAlias
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition: ConstAlias.h:186
DGtal::ConstAlias::Parameter
Parameter
Internal class that allows to distinguish the different types of parameters.
Definition: ConstAlias.h:193
DGtal::ConstAlias::myPtr
const void *const myPtr
Stores the address of the input parameter for further use.
Definition: ConstAlias.h:350
DGtal::ConstAlias::COUNTED_PTR
@ COUNTED_PTR
Definition: ConstAlias.h:194
DGtal::ConstAlias::COUNTED_PTR_OR_PTR
@ COUNTED_PTR_OR_PTR
Definition: ConstAlias.h:194
DGtal::ConstAlias::ConstAlias
ConstAlias(const T *ptrT)
Definition: ConstAlias.h:224
DGtal::ConstAlias::ConstAlias
ConstAlias(const CountedPtr< T > &shT)
Definition: ConstAlias.h:241
DGtal::ConstAlias::CONST_LEFT_VALUE_REF
@ CONST_LEFT_VALUE_REF
Definition: ConstAlias.h:193
DGtal::ConstAlias::ConstAlias
ConstAlias(const T &t)
Definition: ConstAlias.h:216
DGtal::ConstAlias::PTR
@ PTR
Definition: ConstAlias.h:193
DGtal::ConstAlias::ConstAlias
ConstAlias(const CountedPtrOrPtr< T > &shT)
Definition: ConstAlias.h:249
DGtal::ConstAlias::LEFT_VALUE_REF
@ LEFT_VALUE_REF
Definition: ConstAlias.h:193
DGtal::ConstAlias::myParam
const Parameter myParam
Characterizes the type of the input parameter at clone instanciation.
Definition: ConstAlias.h:348
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:94
DGtal::ConstAlias::operator->
const T * operator->() const
Definition: ConstAlias.h:328
DGtal::ConstAlias::RIGHT_VALUE_REF
@ RIGHT_VALUE_REF
Definition: ConstAlias.h:194
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::ConstAlias::ConstAlias
ConstAlias(const CountedConstPtrOrConstPtr< T > &shT)
Definition: ConstAlias.h:257
DGtal::CountedPtrOrPtr
Aim: Smart or simple pointer on T. It can be a smart pointer based on reference counts or a simple po...
Definition: CountedPtr.h:51
DGtal::ConstAlias::operator&
const T * operator&() const
Definition: ConstAlias.h:290
DGtal::CountedPtr
Aim: Smart pointer based on reference counts.
Definition: CountedPtr.h:79
DGtal::ConstAlias::COUNTED_CONST_PTR_OR_CONST_PTR
@ COUNTED_CONST_PTR_OR_CONST_PTR
Definition: ConstAlias.h:195
DGtal::ConstAlias::operator=
ConstAlias & operator=(const ConstAlias &other)
DGtal::ConstAlias::~ConstAlias
~ConstAlias()
Definition: ConstAlias.h:209
DGtal::ConstAlias::COW_PTR
@ COW_PTR
Definition: ConstAlias.h:194
DGtal::CowPtr
Aim: Copy on write shared pointer.
Definition: CowPtr.h:67