DGtal  1.4.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)
35 #define ConstAlias_RECURSES
36 
37 #if !defined ConstAlias_h
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)
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition: ConstAlias.h:187
ConstAlias(const CountedPtr< T > &shT)
Definition: ConstAlias.h:241
const Parameter myParam
Characterizes the type of the input parameter at clone instanciation.
Definition: ConstAlias.h:348
ConstAlias(const CountedConstPtrOrConstPtr< T > &shT)
Definition: ConstAlias.h:257
Parameter
Internal class that allows to distinguish the different types of parameters.
Definition: ConstAlias.h:193
@ COUNTED_CONST_PTR_OR_CONST_PTR
Definition: ConstAlias.h:195
ConstAlias(const ConstAlias &other)=default
const T * operator->() const
Definition: ConstAlias.h:328
const T * operator&() const
Definition: ConstAlias.h:290
const void *const myPtr
Stores the address of the input parameter for further use.
Definition: ConstAlias.h:350
ConstAlias(const CowPtr< T > &)=delete
ConstAlias(const CountedPtrOrPtr< T > &shT)
Definition: ConstAlias.h:249
ConstAlias(const T *ptrT)
Definition: ConstAlias.h:224
ConstAlias & operator=(const ConstAlias &other)
ConstAlias(const T &t)
Definition: ConstAlias.h:216
ConstAlias(T &&)=delete
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
DGtal is the top-level namespace which contains all DGtal functions and types.