DGtal
0.9.3
|
#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 | |
Alias & | operator= (const Alias &other) |
Private Attributes | |
const Parameter | myParam |
const void *const | myPtr |
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.
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).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).T | is any type. |
It can be used as follows. Consider this simple example where class A is a big object.
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.
|
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.
|
inline |
Destructor. Does nothing.
Definition at line 199 of file Alias.h.
References DGtal::Alias< T >::Alias().
|
delete |
Constructor from const reference to an instance of T. Deleted.
Aliasing a const-ref is an error. Consider ConstAlias instead.
Referenced by DGtal::Alias< T >::Alias(), and DGtal::Alias< T >::~Alias().
|
delete |
Constructor from const pointer to an instance of T. Deleted.
Aliasing a const-ptr is an error. Consider ConstAlias instead.
|
inline |
Constructor from a reference to an instance of T. The object is pointed in 'this'.
t | any reference to an object of type T. |
Definition at line 220 of file Alias.h.
|
inline |
Constructor from a pointer to an instance of T. The object is pointed in 'this'.
t | any pointer to an object of type T. |
Definition at line 229 of file Alias.h.
References DGtal::Alias< T >::Alias().
|
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.
|
inline |
Constructor from a const reference to a shared pointer on T. The object is pointed in 'this'.
t | a const-reference to any shared pointer to an object of type T. |
Definition at line 244 of file Alias.h.
|
inline |
Constructor from a const reference to a shared or simple pointer on T. The object is pointed in 'this'.
t | a const-reference to any shared or simple pointer to an object of type T. |
Definition at line 253 of file Alias.h.
References DGtal::Alias< T >::Alias().
|
delete |
Constructor from right-reference value. Deleted.
Aliasing a rvalue ref has no meaning. Consider Clone instead.
|
inline |
Cast operator to a T pointer. The object is never duplicated. Allowed input parameters are:
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.
|
inline |
Cast operator to a shared pointer or to a single pointer. The object is never duplicated. Allowed input parameters are:
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.
|
inline |
Cast operator to a T reference. The object is never duplicated. Allowed input parameters are:
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.
|
private |
Assignment.
other | the object to copy. |
|
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 &(), DGtal::Alias< T >::operator CountedPtrOrPtr< T >(), and DGtal::Alias< T >::operator T &().
|
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 &(), DGtal::Alias< T >::operator CountedPtrOrPtr< T >(), and DGtal::Alias< T >::operator T &().