DGtal  0.9.2
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)
34 
35 #define Alias_RECURSES
36 
37 #if !defined Alias_h
38 
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 
199  inline ~Alias() {}
200 
206  Alias( const T& ) = delete;
207 
213  Alias( const T* ) = delete;
214 
220  inline Alias( T& t )
221  : myParam( LEFT_VALUE_REF ), myPtr( static_cast<const void*>( &t ) )
222  {}
223 
229  inline Alias( T* t )
230  : myParam( PTR ), myPtr( static_cast<const void*>( t ) ) {}
231 
237  Alias( const CowPtr<T>& ) = delete;
238 
244  inline Alias( const CountedPtr<T>& t )
245  : myParam( COUNTED_PTR ), myPtr( static_cast<const void*>( &t ) )
246  {}
247 
253  inline Alias( const CountedPtrOrPtr<T>& t )
254  : myParam( COUNTED_PTR_OR_PTR ), myPtr( static_cast<const void*>( &t ) )
255  {}
256 
262  Alias( T&& ) = delete;
263 
270  inline operator T&() const
271  {
272  switch( myParam ) {
273  case LEFT_VALUE_REF:
274  case PTR:
275  return *( const_cast< T* >( static_cast< const T* >( myPtr ) ) );
276  default: ASSERT( false && "[Alias::operator T&() const] Invalid cast for given type. Consider passing a left-value reference or a pointer as a parameter." );
277  return *( const_cast< T* >( static_cast< const T* >( myPtr ) ) );
278  }
279  }
280 
287  inline T* operator&() const
288  {
289  switch( myParam ) {
290  case LEFT_VALUE_REF:
291  case PTR:
292  return const_cast< T* >( static_cast< const T* >( myPtr ) );
293  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." );
294  return const_cast< T* >( static_cast< const T* >( myPtr ) );
295  }
296  }
297 
307  inline operator CountedPtrOrPtr<T>() const
308  {
309  switch( myParam ) {
310  case LEFT_VALUE_REF:
311  case PTR:
312  return CountedPtrOrPtr<T>( const_cast< T* >( static_cast< const T* >( myPtr ) ), false );
313  case COUNTED_PTR:
314  return CountedPtrOrPtr<T>( *( static_cast< const CountedPtr<T>* >( myPtr ) ) );
315  case COUNTED_PTR_OR_PTR:
316  return CountedPtrOrPtr<T>( *( static_cast< const CountedPtrOrPtr<T>* >( myPtr ) ) );
317  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." );
318  return CountedPtrOrPtr<T>( 0 );
319  }
320  }
321 
322  // ------------------------- Private Datas --------------------------------
323  private:
327  const void* const myPtr;
328 
329 
330  // ------------------------- Internals ------------------------------------
331  private:
332 
339  Alias & operator= ( const Alias & other );
340 
341  }; // end of class Alias
342 
343 } // namespace DGtal
344 
345 
347 // Includes inline functions.
348 
349 // //
351 
352 #endif // !defined Alias_h
353 
354 #undef Alias_RECURSES
355 #endif // else defined(Alias_RECURSES)
Aim: Smart pointer based on reference counts.
Definition: CountedPtr.h:79
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition: Alias.h:182
T * operator&() const
Definition: Alias.h:287
Alias(const T &)=delete
const void *const myPtr
Stores the address of the input parameter for further use.
Definition: Alias.h:327
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
Parameter
Internal class that allows to distinguish the different types of parameters.
Definition: Alias.h:189
Alias(T *t)
Definition: Alias.h:229
Alias(const CountedPtr< T > &t)
Definition: Alias.h:244
DGtal is the top-level namespace which contains all DGtal functions and types.
Alias & operator=(const Alias &other)
const Parameter myParam
Characterizes the type of the input parameter at clone instanciation.
Definition: Alias.h:325
Alias(const CountedPtrOrPtr< T > &t)
Definition: Alias.h:253
Aim: Copy on write shared pointer.
Definition: CowPtr.h:67
Alias(T &t)
Definition: Alias.h:220