DGtal  0.9.3beta
Public Member Functions | Protected Types | Private Member Functions | Private Attributes
DGtal::Alias< T > Class Template Reference

#include <DGtal/base/Alias.h>

Public Member Functions

 ~Alias ()
 
 Alias (const T &)=delete
 
 Alias (const T *)=delete
 
 Alias (T &t)
 
 Alias (T *t)
 
 Alias (const CowPtr< T > &)=delete
 
 Alias (const CountedPtr< T > &t)
 
 Alias (const CountedPtrOrPtr< T > &t)
 
 Alias (T &&)=delete
 
 operator T & () const
 
T * operator& () const
 
 operator CountedPtrOrPtr< T > () const
 

Protected Types

enum  Parameter {
  CONST_LEFT_VALUE_REF, LEFT_VALUE_REF, PTR, CONST_PTR,
  COW_PTR, COUNTED_PTR, RIGHT_VALUE_REF, COUNTED_PTR_OR_PTR,
  COUNTED_CONST_PTR_OR_CONST_PTR
}
 

Private Member Functions

Aliasoperator= (const Alias &other)
 

Private Attributes

const Parameter myParam
 
const void *const myPtr
 

Detailed Description

template<typename T>
class DGtal::Alias< T >

Aim: This class encapsulates its parameter class so that to indicate to the user that the object/pointer will be only aliased. Therefore the user is reminded that the argument parameter is given to the function without any additional cost and may be modified, while he is aware that the lifetime of the argument parameter must be at least as long as the object itself. Note that an instance of Alias<T> is itself a light object (it holds only an enum and a pointer).

Description of template class 'Alias'

(For a complete description, see Parameter passing, cloning and referencing).

It is used in methods or functions to encapsulate the parameter types. The following conversion from input parameter to data member or variable are possible:

Argument type T& T* CountedPtr<T> CountedPtrOrPtr<T>
To: T& Shared. O(1) Shared. O(1)
To: T* Shared. O(1) Shared. O(1)
To: CountedPtrOrPtr<T>Shared. O(1)Shared. O(1)Shared. O(1), secure Shared. O(1), secure

Argument conversion to member is automatic except when converting to a pointer T*: the address operator (operator&) must be used in this case.

For the last row (case where the programmer choose a CountedPtrOrPtr to hold the const alias), the user can thus enforce a secure aliasing by handling a variant of CountedPtr as argument. In this case, even if the aliased object is destroyed in the caller context, it still exists in the callee context.

Note
The usage of Alias<T> instead of T & or T * in parameters is recommended when the lifetime of the parameter must exceed the lifetime of the called method/function/constructor (often the case in constructor or init methods).
The usage of T & or T * instead of Alias<T> is recommended when the lifetime of the parameter is not required to exceed the lifetime of the called method/function/constructor (often the case in standard methods, where the parameter is only used at some point, but not referenced after in some data member).
Template Parameters
Tis any type.
See also
ConstAlias
Clone

It can be used as follows. Consider this simple example where class A is a big object.

const int N = 10000;
struct A { ...
int table[N];
};
// When looking at the signature of B1's constructor, there is an
// ambiguity on the role of parameter a and its life-time. Here
// a's lifetime should be longer than the construction. Generally
// the ambiguity is removed by adding comments or, for the
// experienced developper, by looking at other parts of the code.
// Only aliasing, but for a long lifetime.
struct B1 {
B1( A & a ) // ambiguous, cost is O(1) here and lifetime of a should exceed constructor.
: myA( a ) {}
...
A & myA;
};

Sometimes it is very important that the developper that uses the library is conscious that an object, say b, may require that an instance a given as parameter should have a lifetime longer than b itself (case for an instance of B1 above). Classes Clone, Alias, ConstAlias exist for these reasons. The class above may be rewritten as follows.

// Aliasing for a long lifetime is visible.
struct B1_v2_1 {
B1_v2_1( Alias<A> a ) // not ambiguous, cost is O(1) here and lifetime of a should be long enough
: myA( a ) {}
...
A & myA;
};
// Aliasing for a long lifetime is visible.
struct B1_v2_2 {
B1_v2_2( Alias<A> a ) // not ambiguous, cost is O(1) here and lifetime of a should be long enough
: myA( &a ) {} // Note the use of the address operator because of the pointer member
...
A* myA;
};
// Aliasing for a long lifetime is visible.
struct B1_v2_3 {
B1_v2_3( Alias<A> a ) // not ambiguous, cost is O(1) here and lifetime of a should be long enough
: myA( a ) {}
...
CountedPtrOrPtr<A> myA;
};
...
A a1;
CountedPtr<A> cptr_a1( new A( a1 ) );
B1 ( a1 ); // not duplicated
B1_v2_1 ( a1 ); // not duplicated
B1_v2_2 ( a1 ); // not duplicated
B1_v2_3 ( a1 ); // not duplicated
B1_v2_3 ( cptr_a1 ); // not duplicated, even better the user choose a secure variant of alias.
Note
The user should not use Alias<T> instead of T* for data members. It works in most cases, but there are some subtle differences between the two behaviors.
Alias have a default copy constructor, so as to let the user forward an Alias<T> parameter.

Definition at line 182 of file Alias.h.

Member Enumeration Documentation

template<typename T>
enum DGtal::Alias::Parameter
protected

Internal class that allows to distinguish the different types of parameters.

Enumerator
CONST_LEFT_VALUE_REF 
LEFT_VALUE_REF 
PTR 
CONST_PTR 
COW_PTR 
COUNTED_PTR 
RIGHT_VALUE_REF 
COUNTED_PTR_OR_PTR 
COUNTED_CONST_PTR_OR_CONST_PTR 

Definition at line 189 of file Alias.h.

Constructor & Destructor Documentation

template<typename T>
DGtal::Alias< T >::~Alias ( )
inline

Destructor. Does nothing.

Definition at line 199 of file Alias.h.

199 {}
template<typename T>
DGtal::Alias< T >::Alias ( const T &  )
delete

Constructor from const reference to an instance of T. Deleted.

Aliasing a const-ref is an error. Consider ConstAlias instead.

template<typename T>
DGtal::Alias< T >::Alias ( const T *  )
delete

Constructor from const pointer to an instance of T. Deleted.

Aliasing a const-ptr is an error. Consider ConstAlias instead.

template<typename T>
DGtal::Alias< T >::Alias ( T &  t)
inline

Constructor from a reference to an instance of T. The object is pointed in 'this'.

Parameters
tany reference to an object of type T.

Definition at line 220 of file Alias.h.

221  : myParam( LEFT_VALUE_REF ), myPtr( static_cast<const void*>( &t ) )
222  {}
const void *const myPtr
Stores the address of the input parameter for further use.
Definition: Alias.h:327
const Parameter myParam
Characterizes the type of the input parameter at clone instanciation.
Definition: Alias.h:325
template<typename T>
DGtal::Alias< T >::Alias ( T *  t)
inline

Constructor from a pointer to an instance of T. The object is pointed in 'this'.

Parameters
tany pointer to an object of type T.

Definition at line 229 of file Alias.h.

230  : myParam( PTR ), myPtr( static_cast<const void*>( t ) ) {}
const void *const myPtr
Stores the address of the input parameter for further use.
Definition: Alias.h:327
const Parameter myParam
Characterizes the type of the input parameter at clone instanciation.
Definition: Alias.h:325
template<typename T>
DGtal::Alias< T >::Alias ( const CowPtr< T > &  )
delete

Constructor from a const reference to a copy-on-write pointer on T. Deleted.

Aliasing a const-cow ptr is an error. Consider ConstAlias instead.

template<typename T>
DGtal::Alias< T >::Alias ( const CountedPtr< T > &  t)
inline

Constructor from a const reference to a shared pointer on T. The object is pointed in 'this'.

Parameters
ta const-reference to any shared pointer to an object of type T.

Definition at line 244 of file Alias.h.

245  : myParam( COUNTED_PTR ), myPtr( static_cast<const void*>( &t ) )
246  {}
const void *const myPtr
Stores the address of the input parameter for further use.
Definition: Alias.h:327
const Parameter myParam
Characterizes the type of the input parameter at clone instanciation.
Definition: Alias.h:325
template<typename T>
DGtal::Alias< T >::Alias ( const CountedPtrOrPtr< T > &  t)
inline

Constructor from a const reference to a shared or simple pointer on T. The object is pointed in 'this'.

Parameters
ta const-reference to any shared or simple pointer to an object of type T.

Definition at line 253 of file Alias.h.

254  : myParam( COUNTED_PTR_OR_PTR ), myPtr( static_cast<const void*>( &t ) )
255  {}
const void *const myPtr
Stores the address of the input parameter for further use.
Definition: Alias.h:327
const Parameter myParam
Characterizes the type of the input parameter at clone instanciation.
Definition: Alias.h:325
template<typename T>
DGtal::Alias< T >::Alias ( T &&  )
delete

Constructor from right-reference value. Deleted.

Aliasing a rvalue ref has no meaning. Consider Clone instead.

Member Function Documentation

template<typename T>
DGtal::Alias< T >::operator CountedPtrOrPtr< T > ( ) const
inline

Cast operator to a shared pointer or to a single pointer. The object is never duplicated. Allowed input parameters are:

  • T& -> CountedPtrOrPtr<T> // shared
  • T* -> CountedPtrOrPtr<T> // shared
  • CountedPtr<T> -> CountedPtrOrPtr<T> // shared
  • CountedPtrOrPtr<T> -> CountedPtrOrPtr<T> // shared

Definition at line 307 of file Alias.h.

References DGtal::Alias< T >::COUNTED_PTR, DGtal::Alias< T >::COUNTED_PTR_OR_PTR, DGtal::Alias< T >::LEFT_VALUE_REF, DGtal::Alias< T >::myParam, DGtal::Alias< T >::myPtr, and DGtal::Alias< T >::PTR.

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  }
const void *const myPtr
Stores the address of the input parameter for further use.
Definition: Alias.h:327
const Parameter myParam
Characterizes the type of the input parameter at clone instanciation.
Definition: Alias.h:325
template<typename T>
DGtal::Alias< T >::operator T & ( ) const
inline

Cast operator to a T reference. The object is never duplicated. Allowed input parameters are:

  • T& -> T& // no duplication
  • T* -> T& // no duplication, exception if null

Definition at line 270 of file Alias.h.

References DGtal::Alias< T >::LEFT_VALUE_REF, DGtal::Alias< T >::myParam, DGtal::Alias< T >::myPtr, and DGtal::Alias< T >::PTR.

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  }
const void *const myPtr
Stores the address of the input parameter for further use.
Definition: Alias.h:327
const Parameter myParam
Characterizes the type of the input parameter at clone instanciation.
Definition: Alias.h:325
template<typename T>
T* DGtal::Alias< T >::operator& ( ) const
inline

Cast operator to a T pointer. The object is never duplicated. Allowed input parameters are:

  • T& -> T* // no duplication
  • T* -> T* // no duplication

Definition at line 287 of file Alias.h.

References DGtal::Alias< T >::LEFT_VALUE_REF, DGtal::Alias< T >::myParam, DGtal::Alias< T >::myPtr, and DGtal::Alias< T >::PTR.

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  }
const void *const myPtr
Stores the address of the input parameter for further use.
Definition: Alias.h:327
const Parameter myParam
Characterizes the type of the input parameter at clone instanciation.
Definition: Alias.h:325
template<typename T>
Alias& DGtal::Alias< T >::operator= ( const Alias< T > &  other)
private

Assignment.

Parameters
otherthe object to copy.
Returns
a reference on 'this'. Forbidden (otherwise the user might be tempted to use it as a member).

Field Documentation

template<typename T>
const Parameter DGtal::Alias< T >::myParam
private

Characterizes the type of the input parameter at clone instanciation.

Definition at line 325 of file Alias.h.

Referenced by DGtal::Alias< T >::operator CountedPtrOrPtr< T >(), DGtal::Alias< T >::operator T &(), and DGtal::Alias< T >::operator&().

template<typename T>
const void* const DGtal::Alias< T >::myPtr
private

Stores the address of the input parameter for further use.

Definition at line 327 of file Alias.h.

Referenced by DGtal::Alias< T >::operator CountedPtrOrPtr< T >(), DGtal::Alias< T >::operator T &(), and DGtal::Alias< T >::operator&().


The documentation for this class was generated from the following file: