DGtal  1.4.beta
Alias.h
1 
17 #pragma once
18 
31 #if defined(Alias_RECURSES)
32 #error Recursive header files inclusion detected in Alias.h
33 #else // defined(Alias_RECURSES)
35 #define Alias_RECURSES
36 
37 #if !defined Alias_h
39 #define Alias_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 Alias
181  template <typename T>
182  class Alias
183  {
184 
185  // ----------------------- Internal classes ------------------------------
186  protected:
187 
192 
193  // ----------------------- Standard services ------------------------------
194  public:
195 
200  Alias( const Alias & other ) = default;
201 
205  inline ~Alias() {}
206 
212  Alias( const T& ) = delete;
213 
219  Alias( const T* ) = delete;
220 
226  inline Alias( T& t )
227  : myParam( LEFT_VALUE_REF ), myPtr( static_cast<const void*>( &t ) )
228  {}
229 
235  inline Alias( T* t )
236  : myParam( PTR ), myPtr( static_cast<const void*>( t ) ) {}
237 
243  Alias( const CowPtr<T>& ) = delete;
244 
250  inline Alias( const CountedPtr<T>& t )
251  : myParam( COUNTED_PTR ), myPtr( static_cast<const void*>( &t ) )
252  {}
253 
259  inline Alias( const CountedPtrOrPtr<T>& t )
260  : myParam( COUNTED_PTR_OR_PTR ), myPtr( static_cast<const void*>( &t ) )
261  {}
262 
268  Alias( T&& ) = delete;
269 
276  inline operator T&() const
277  {
278  switch( myParam ) {
279  case LEFT_VALUE_REF:
280  case PTR:
281  return *( const_cast< T* >( static_cast< const T* >( myPtr ) ) );
282  default: ASSERT( false && "[Alias::operator T&() const] Invalid cast for given type. Consider passing a left-value reference or a pointer as a parameter." );
283  return *( const_cast< T* >( static_cast< const T* >( myPtr ) ) );
284  }
285  }
286 
293  inline T* operator&() const
294  {
295  switch( myParam ) {
296  case LEFT_VALUE_REF:
297  case PTR:
298  return const_cast< T* >( static_cast< const T* >( myPtr ) );
299  default: ASSERT( false && "[T* Alias::operator&() const] Invalid address operator for given type. Consider passing a left-value reference or a pointer as a parameter." );
300  return const_cast< T* >( static_cast< const T* >( myPtr ) );
301  }
302  }
303 
313  inline operator CountedPtrOrPtr<T>() const
314  {
315  switch( myParam ) {
316  case LEFT_VALUE_REF:
317  case PTR:
318  return CountedPtrOrPtr<T>( const_cast< T* >( static_cast< const T* >( myPtr ) ), false );
319  case COUNTED_PTR:
320  return CountedPtrOrPtr<T>( *( static_cast< const CountedPtr<T>* >( myPtr ) ) );
321  case COUNTED_PTR_OR_PTR:
322  return CountedPtrOrPtr<T>( *( static_cast< const CountedPtrOrPtr<T>* >( myPtr ) ) );
323  default: ASSERT( false && "[Alias::operator CountedPtrOrPtr<T>() const] Invalid cast for given type. Consider passing a reference, a pointer or a CountedPtr as a parameter." );
324  return CountedPtrOrPtr<T>( 0 );
325  }
326  }
327 
328  // ------------------------- Private Datas --------------------------------
329  private:
333  const void* const myPtr;
334 
335 
336  // ------------------------- Internals ------------------------------------
337  private:
338 
345  Alias & operator= ( const Alias & other );
346 
347  }; // end of class Alias
348 
349 } // namespace DGtal
350 
351 
353 // Includes inline functions.
354 
355 // //
357 
358 #endif // !defined Alias_h
359 
360 #undef Alias_RECURSES
361 #endif // else defined(Alias_RECURSES)
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition: Alias.h:183
Alias(const CowPtr< T > &)=delete
Alias(const Alias &other)=default
const Parameter myParam
Characterizes the type of the input parameter at clone instanciation.
Definition: Alias.h:331
Parameter
Internal class that allows to distinguish the different types of parameters.
Definition: Alias.h:189
@ CONST_LEFT_VALUE_REF
Definition: Alias.h:189
@ COUNTED_PTR
Definition: Alias.h:190
@ RIGHT_VALUE_REF
Definition: Alias.h:190
@ CONST_PTR
Definition: Alias.h:189
@ COUNTED_PTR_OR_PTR
Definition: Alias.h:190
@ COUNTED_CONST_PTR_OR_CONST_PTR
Definition: Alias.h:191
@ LEFT_VALUE_REF
Definition: Alias.h:189
T * operator&() const
Definition: Alias.h:293
Alias(const T *)=delete
Alias(T &&)=delete
Alias(const CountedPtrOrPtr< T > &t)
Definition: Alias.h:259
Alias(T &t)
Definition: Alias.h:226
const void *const myPtr
Stores the address of the input parameter for further use.
Definition: Alias.h:333
Alias(const T &)=delete
Alias & operator=(const Alias &other)
Alias(const CountedPtr< T > &t)
Definition: Alias.h:250
Alias(T *t)
Definition: Alias.h:235
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.