DGtal 2.0.0
|
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). More...
#include <DGtal/base/Alias.h>
Public Member Functions | |
Alias (const Alias &other)=default | |
~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 } |
Internal class that allows to distinguish the different types of parameters. More... |
Private Member Functions | |
Alias & | operator= (const Alias &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 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 |
|
default |
Default copy constructor.
other | the object to copy. |
References Alias().
Referenced by Alias(), and operator=().
|
inline |
|
delete |
Constructor from const reference to an instance of T. Deleted.
Aliasing a const-ref is an error. Consider ConstAlias instead.
|
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 226 of file Alias.h.
References LEFT_VALUE_REF, myParam, and myPtr.
|
inline |
|
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 250 of file Alias.h.
References COUNTED_PTR, myParam, and myPtr.
|
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 259 of file Alias.h.
References COUNTED_PTR_OR_PTR, myParam, and myPtr.
|
delete |
Constructor from right-reference value. Deleted.
Aliasing a rvalue ref has no meaning. Consider Clone instead.
|
inline |
Cast operator to a shared pointer or to a single pointer. The object is never duplicated. Allowed input parameters are:
Definition at line 313 of file Alias.h.
References COUNTED_PTR, COUNTED_PTR_OR_PTR, LEFT_VALUE_REF, myParam, myPtr, and PTR.
|
inline |
Cast operator to a T reference. The object is never duplicated. Allowed input parameters are:
Definition at line 276 of file Alias.h.
References LEFT_VALUE_REF, myParam, myPtr, and PTR.
|
inline |
Cast operator to a T pointer. The object is never duplicated. Allowed input parameters are:
Definition at line 293 of file Alias.h.
References LEFT_VALUE_REF, myParam, myPtr, and PTR.
|
private |
Assignment.
other | the object to copy. |
References Alias().
|
private |
Characterizes the type of the input parameter at clone instanciation.
Definition at line 331 of file Alias.h.
Referenced by Alias(), Alias(), Alias(), Alias(), operator CountedPtrOrPtr< T >(), operator T&(), and operator&().
|
private |
Stores the address of the input parameter for further use.
Definition at line 333 of file Alias.h.
Referenced by Alias(), Alias(), Alias(), Alias(), operator CountedPtrOrPtr< T >(), operator T&(), and operator&().