DGtal 1.4.0
|
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/pointer will be only const aliased (and hence left unchanged). Therefore the user is reminded that the argument parameter is given to the function without any additional cost and may not 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 ConstAlias<T> is itself a light object (it holds only an enum and a pointer). More...
#include <DGtal/base/ConstAlias.h>
Public Member Functions | |
ConstAlias (const ConstAlias &other)=default | |
~ConstAlias () | |
ConstAlias (const T &t) | |
ConstAlias (const T *ptrT) | |
ConstAlias (const CowPtr< T > &)=delete | |
ConstAlias (const CountedPtr< T > &shT) | |
ConstAlias (const CountedPtrOrPtr< T > &shT) | |
ConstAlias (const CountedConstPtrOrConstPtr< T > &shT) | |
ConstAlias (T &&)=delete | |
operator const T & () const | |
const T * | operator& () const |
operator CountedConstPtrOrConstPtr< T > () const | |
const T * | operator-> () 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 } |
Internal class that allows to distinguish the different types of parameters. More... | |
Private Member Functions | |
ConstAlias & | operator= (const ConstAlias &other) |
Private Attributes | |
const Parameter | myParam |
Characterizes the type of the input parameter at clone instanciation. | |
const void *const | myPtr |
Stores the address of the input parameter for further use. | |
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/pointer will be only const aliased (and hence left unchanged). Therefore the user is reminded that the argument parameter is given to the function without any additional cost and may not 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 ConstAlias<T> is itself a light object (it holds only an enum and a pointer).
Description of template class 'ConstAlias'
(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 | const T& | const T* | CountedPtr<T> | CountedPtrOrPtr<T> | CountedConstPtrOrConstPtr<T> |
---|---|---|---|---|---|
To: const T& | Shared. O(1) | Shared. O(1) | |||
To: const T* | Shared. O(1) | Shared. O(1) | |||
To: CountedConstPtrOrConstPtr<T> | Shared. O(1) | Shared. O(1) | Shared. O(1), secure | Shared. O(1), secure | Shared. O(1), secure |
Argument conversion to member is automatic except when converting to a pointer const
T*
: the address operator (operator&
) must be used in this case.
For the last row (case where the programmer choose a CountedConstPtrOrConstPtr to hold the const alias), the user can thus enforce a secure const 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.
const
T
&
or const
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).const
T
&
or const
T
*
instead of ConstAlias<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.
const
T
& for data members. It works in most cases, but there are some subtle differences between the two behaviors.Definition at line 186 of file ConstAlias.h.
|
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 193 of file ConstAlias.h.
|
default |
Default copy constructor.
other | object to copy. |
|
inline |
|
inline |
Constructor from const reference to an instance of T. The object is pointed in 'this'.
t | any const reference to an object of type T. |
Definition at line 216 of file ConstAlias.h.
|
inline |
Constructor from const pointer to an instance of T. The object is pointed in 'this'.
ptrT | any const pointer to an object of type T. |
Definition at line 224 of file ConstAlias.h.
|
delete |
Constructor from a const reference to a copy-on-write pointer on T. Deleted.
Const-aliasing a copy-on-write pointer has no meaning. Consider Clone instead.
|
inline |
Constructor from a const reference to a shared pointer on T. The object is pointed in 'this'.
shT | any const reference to a shared pointer to an object of type T. |
Definition at line 241 of file ConstAlias.h.
|
inline |
Constructor from a const reference to a shared or simple const pointer on T. The object is pointed in 'this'.
shT | any const reference to a shared or simple const pointer to an object of type T. |
Definition at line 249 of file ConstAlias.h.
|
inline |
Constructor from a const reference to a shared or simple const pointer on T. The object is pointed in 'this'.
shT | any const reference to a shared or simple const pointer to an object of type T. |
Definition at line 257 of file ConstAlias.h.
|
delete |
Constructor from right-reference value. Delete.
Const-aliasing a rvalue ref has no meaning. Consider Clone instead.
|
inline |
Cast operator to a T const-reference. The object is never duplicated. Allowed input parameters are:
Definition at line 273 of file ConstAlias.h.
References DGtal::ConstAlias< T >::CONST_LEFT_VALUE_REF, DGtal::ConstAlias< T >::CONST_PTR, DGtal::ConstAlias< T >::myParam, and DGtal::ConstAlias< T >::myPtr.
|
inline |
Cast operator to a shared or simple const pointer. The object is never duplicated. Allowed input parameters are:
Definition at line 311 of file ConstAlias.h.
References DGtal::ConstAlias< T >::CONST_LEFT_VALUE_REF, DGtal::ConstAlias< T >::CONST_PTR, DGtal::ConstAlias< T >::COUNTED_CONST_PTR_OR_CONST_PTR, DGtal::ConstAlias< T >::COUNTED_PTR, DGtal::ConstAlias< T >::COUNTED_PTR_OR_PTR, DGtal::ConstAlias< T >::myParam, and DGtal::ConstAlias< T >::myPtr.
|
inline |
Cast operator to a T const-pointer. The object is never duplicated. Allowed input parameters are:
Definition at line 290 of file ConstAlias.h.
References DGtal::ConstAlias< T >::CONST_LEFT_VALUE_REF, DGtal::ConstAlias< T >::CONST_PTR, DGtal::ConstAlias< T >::myParam, and DGtal::ConstAlias< T >::myPtr.
|
inline |
Definition at line 328 of file ConstAlias.h.
References DGtal::ConstAlias< T >::CONST_LEFT_VALUE_REF, DGtal::ConstAlias< T >::CONST_PTR, DGtal::ConstAlias< T >::COUNTED_CONST_PTR_OR_CONST_PTR, DGtal::ConstAlias< T >::COUNTED_PTR, DGtal::ConstAlias< T >::COUNTED_PTR_OR_PTR, DGtal::ConstAlias< T >::myParam, and DGtal::ConstAlias< T >::myPtr.
|
private |
Assignment.
other | the object to copy. |
|
private |
Characterizes the type of the input parameter at clone instanciation.
Definition at line 348 of file ConstAlias.h.
Referenced by DGtal::ConstAlias< T >::operator const T &(), DGtal::ConstAlias< T >::operator CountedConstPtrOrConstPtr< T >(), DGtal::ConstAlias< T >::operator&(), and DGtal::ConstAlias< T >::operator->().
|
private |
Stores the address of the input parameter for further use.
Definition at line 350 of file ConstAlias.h.
Referenced by DGtal::ConstAlias< T >::operator const T &(), DGtal::ConstAlias< T >::operator CountedConstPtrOrConstPtr< T >(), DGtal::ConstAlias< T >::operator&(), and DGtal::ConstAlias< T >::operator->().