DGtal  0.9.2
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 
203  inline ~ConstAlias() {}
204 
210  inline ConstAlias( const T& t )
211  : myParam( CONST_LEFT_VALUE_REF ), myPtr( static_cast<const void*>( &t ) ) {}
212 
218  inline ConstAlias( const T* ptrT )
219  : myParam( CONST_PTR ), myPtr( static_cast<const void*>( ptrT ) ) {}
220 
221 
227  ConstAlias( const CowPtr<T>& ) = delete;
228 
229 
235  inline ConstAlias( const CountedPtr<T>& shT )
236  : myParam( COUNTED_PTR ), myPtr( static_cast<const void*>( &shT ) ) {}
237 
243  inline ConstAlias( const CountedPtrOrPtr<T>& shT )
244  : myParam( COUNTED_PTR_OR_PTR ), myPtr( static_cast<const void*>( &shT ) ) {}
245 
252  : myParam( COUNTED_CONST_PTR_OR_CONST_PTR ), myPtr( static_cast<const void*>( &shT ) ) {}
253 
259  ConstAlias( T&& ) = delete;
260 
267  inline operator const T&() const
268  {
269  switch( myParam ) {
271  case CONST_PTR:
272  return *( static_cast< const T* >( myPtr ) );
273  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." );
274  return *( static_cast< const T* >( myPtr ) );
275  }
276  }
277 
284  inline const T* operator&() const
285  {
286  switch( myParam ) {
288  case CONST_PTR:
289  return static_cast< const T* >( myPtr );
290  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." );
291  return static_cast< const T* >( myPtr );
292  }
293  }
294 
305  inline operator CountedConstPtrOrConstPtr<T>() const
306  {
307  switch( myParam ) {
309  case CONST_PTR:
310  return CountedConstPtrOrConstPtr<T>( static_cast< const T* >( myPtr ), false );
311  case COUNTED_PTR:
312  return CountedConstPtrOrConstPtr<T>( *( static_cast< const CountedPtr<T>* >( myPtr ) ) );
313  case COUNTED_PTR_OR_PTR:
314  return CountedConstPtrOrConstPtr<T>( *( static_cast< const CountedPtrOrPtr<T>* >( myPtr ) ) );
316  return CountedConstPtrOrConstPtr<T>( *( static_cast< const CountedPtrOrPtr<T>* >( myPtr ) ) );
317  default: ASSERT( false && "[ConstAlias::operator CowPtr<T>() const] Invalid cast for given type. Consider passing a CountedPtr or a CowPtr as a parameter." );
318  return CountedConstPtrOrConstPtr<T>( 0, false );
319  }
320  }
321 
322  inline const T* operator->() const
323  {
324  switch( myParam ) {
326  case CONST_PTR:
327  return static_cast< const T* >( myPtr );
328  case COUNTED_PTR:
329  return ( static_cast< const CountedPtr<T>* >( myPtr ) )->operator->();
330  case COUNTED_PTR_OR_PTR:
331  return ( static_cast< const CountedPtrOrPtr<T>* >( myPtr ) )->operator->();
333  return ( static_cast< const CountedConstPtrOrConstPtr<T>* >( myPtr ) )->operator->();
334  default: ASSERT( false && "[ConstAlias::operator->() const] Invalid cast for given type. Consider passing a CountedPtr or a CowPtr as a parameter." );
335  return 0;
336  }
337  }
338 
339  // ------------------------- Private Datas --------------------------------
340  private:
344  const void* const myPtr;
345 
346 
347  // ------------------------- Internals ------------------------------------
348  private:
349 
356  ConstAlias & operator= ( const ConstAlias & other );
357 
358  }; // end of class ConstAlias
359 
360 } // namespace DGtal
361 
362 
364 // Includes inline functions.
365 
366 // //
368 
369 #endif // !defined ConstAlias_h
370 
371 #undef ConstAlias_RECURSES
372 #endif // else defined(ConstAlias_RECURSES)
ConstAlias(const CountedPtr< T > &shT)
Definition: ConstAlias.h:235
const void *const myPtr
Stores the address of the input parameter for further use.
Definition: ConstAlias.h:344
Aim: Smart pointer based on reference counts.
Definition: CountedPtr.h:79
const Parameter myParam
Characterizes the type of the input parameter at clone instanciation.
Definition: ConstAlias.h:342
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition: ConstAlias.h:186
ConstAlias(const T *ptrT)
Definition: ConstAlias.h:218
Aim: Smart or simple const pointer on T. It can be a smart pointer based on reference counts or a sim...
ConstAlias & operator=(const ConstAlias &other)
ConstAlias(const T &t)
Definition: ConstAlias.h:210
ConstAlias(const CountedConstPtrOrConstPtr< T > &shT)
Definition: ConstAlias.h:251
Parameter
Internal class that allows to distinguish the different types of parameters.
Definition: ConstAlias.h:193
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
const T * operator&() const
Definition: ConstAlias.h:284
DGtal is the top-level namespace which contains all DGtal functions and types.
const T * operator->() const
Definition: ConstAlias.h:322
ConstAlias(const CountedPtrOrPtr< T > &shT)
Definition: ConstAlias.h:243
Aim: Copy on write shared pointer.
Definition: CowPtr.h:67