![]() |
aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
|
Smart pointersaGrUM's smart pointers keep track of the number of times the value they point to is referenced. More...
#include <agrum/tools/core/refPtr.h>
Public Member Functions | |
template<typename DownVal > | |
INLINE | RefPtr (const RefPtr< DownVal > &from) |
template<typename DownVal > | |
INLINE RefPtr< Val > & | operator= (const RefPtr< DownVal > &from) |
Constructors / Destructors | |
RefPtr (Val *val=0) | |
Default constructor. More... | |
RefPtr (const RefPtr< Val > &from) | |
Copy constructor. More... | |
template<typename DownVal > | |
RefPtr (const RefPtr< DownVal > &from) | |
Copy constructor for downcastable pointers. More... | |
~RefPtr () | |
Class destructor. More... | |
Accessors / Modifiers | |
operator bool () const | |
Checks whether a RefPtr points toward something. More... | |
void | clear () |
Makes the smart pointer point to 0. More... | |
unsigned int | refCount () const |
Returns the number of smart pointer referencing the contained pointer. More... | |
Operators | |
RefPtr< Val > & | operator= (const RefPtr< Val > &from) |
Copy operator. More... | |
RefPtr< Val > & | operator= (Val *from) |
Copy operator. More... | |
template<typename DownVal > | |
RefPtr< Val > & | operator= (const RefPtr< DownVal > &from) |
Copy operator for downcastable pointers. More... | |
bool | operator== (const RefPtr< Val > &from) const |
Checks whether two RefPtr<Val> are smart pointers for the same element. More... | |
bool | operator!= (const RefPtr< Val > &from) const |
Checks whether two RefPtr<Val> are smart pointers for different elements. More... | |
Val * | operator-> () const |
Dereferencing operator. More... | |
Val & | operator* () |
Dereferencing operator. More... | |
const Val & | operator* () const |
Const dereferencing operator. More... | |
Friends | |
void | swap (RefPtr< Val > &, RefPtr< Val > &) |
The swap function must access to gum::RefPtr private parts. More... | |
Internals | |
template<typename T > | |
class | RefPtr |
A friend to allow downcastings. More... | |
template<typename T > | |
class | HashFunc |
A friend for hashing quickly ref pointers. More... | |
Val * | _val_ |
The dumb pointer encapsulated into the "smart" pointer. More... | |
unsigned int * | _refcount_ |
A reference counter on *val. More... | |
void | _destroy_ (unsigned int *, Val *) |
A function to remove the content of the smart pointer, if any. More... | |
unsigned int * | _refCountPtr_ () const |
A function to return the refcount pointer. More... | |
Smart pointers
aGrUM's smart pointers keep track of the number of times the value they point to is referenced.
When all smart pointers on a given value have been deleted, the value itself is also deleted. Thus, using RefPtr, you do not have to worry anymore about memory leaks. Note however that smart pointers impose some constraints on the way you program. Here are some rules of thumb: when several smart pointers must point to the same value, use only once the constructor taking in argument *val, and use the copy constructor or the assignment operator for the other smart pointers, else all the smart pointers will think they point to different values and thus they will all try to deallocate the dumb pointer they encapsulate, hence resulting in segmentation faults. In fact, the correct way to use the *val constructor is writing things like
In particular, never deallocate yourself a dumb pointer you have encapsulated into a smart pointer.
Val | The type referenced by the gum::RefPtr. |
|
explicit |
Default constructor.
This constructor creates an object encapsulating the pointer passed in argument. No copy of the value pointed to by the pointer is performed. The RefPtr assumes that the value pointed to has been allocated on the heap using the new operator. If this is not the case, then using RefPtr will result in an undefined behavior when the RefPtr is destroyed (ok, we all know what it means: a segmentation fault). To avoid deleting several times the pointer encapsulated, the safe way to use the RefPtr is certainly through calls like:
Passing an already allocated pointer to the constructor is not forbidden. However, in this case, care should be taken not to allow external functions to delete the value pointed to by val. Moreover, care should be taken not to allow creating multiple RefPtr using this constructor on the same val. This would lead to unexpected results after deletion of the first RefPtr.
val | The dumb pointer encapsulated into the object (make sure it is allocated on the heap) |
std::bad_alloc | Raised if the complete RefPtr structure cannot be set properly. |
Definition at line 35 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE gum::RefPtr< Val >::RefPtr | ( | const RefPtr< Val > & | from | ) |
Copy constructor.
from | the smart pointer we wish to make a copy. |
Definition at line 42 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
gum::RefPtr< Val >::RefPtr | ( | const RefPtr< DownVal > & | from | ) |
Copy constructor for downcastable pointers.
from | the smart pointer we wish to make a copy. |
DownVal | The downcastable type. |
INLINE gum::RefPtr< Val >::~RefPtr | ( | ) |
Class destructor.
Decrements the ref count and deletes if necessary the dumb pointer.
Definition at line 165 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE gum::RefPtr< Val >::RefPtr | ( | const RefPtr< DownVal > & | from | ) |
Definition at line 53 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
private |
A function to remove the content of the smart pointer, if any.
Definition at line 63 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
private |
A function to return the refcount pointer.
Definition at line 244 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE void gum::RefPtr< Val >::clear | ( | ) |
Makes the smart pointer point to 0.
If necessary, the dumb pointer previously pointed to by the RefPtr is deallocated. In this case, an exception may be thrown by the destructor of the object pointed to. But, even in this case, the RefPtr guarrantees that after the completion of this method, the RefPtr will point toward 0.
Definition at line 221 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE gum::RefPtr< Val >::operator bool | ( | ) | const |
Checks whether a RefPtr points toward something.
This method enables writing code like if
(refptr) perform_operation()
Definition at line 214 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE bool gum::RefPtr< Val >::operator!= | ( | const RefPtr< Val > & | from | ) | const |
Checks whether two RefPtr<Val> are smart pointers for different elements.
Returns true if either the dumb pointers the smart pointers encapsulate are different or the reference counters are different (i.e., the smart pointers are not related through copy operators).
from | The gum::RefPtr to test for inequality. |
Definition at line 180 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE Val & gum::RefPtr< Val >::operator* | ( | ) |
Dereferencing operator.
This operator is provided for convenience but you should prefer using operator -> as this is the syntax you would use with the dumb pointer. Note however that it might be useful for built-in types such as int.
NullElement | Raised whenever the RefPtr points to 0. |
Definition at line 187 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE const Val & gum::RefPtr< Val >::operator* | ( | ) | const |
Const dereferencing operator.
This operator is provided for convenience but you should prefer using operator -> as this is the syntax you would use with the dumb pointer. Note however that it might be useful for built-in types such as int.
NullElement | Raised whenever the RefPtr points to 0. |
Definition at line 196 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE Val * gum::RefPtr< Val >::operator-> | ( | ) | const |
Dereferencing operator.
This operator allows developers to write code like refptr->member()
.
NullElement | Raised whenever the smart pointer points toward 0. |
Definition at line 205 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE RefPtr< Val >& gum::RefPtr< Val >::operator= | ( | const RefPtr< DownVal > & | from | ) |
Definition at line 144 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE RefPtr< Val > & gum::RefPtr< Val >::operator= | ( | const RefPtr< Val > & | from | ) |
Copy operator.
The operator= may throw exceptions when the dumb pointer previously pointed to by the RefPtr is deallocated (that is, the destructor of the object pointed to may throw an exception). However, even when this occurs, the RefPtr guarrantees that the copy operation is correctly performed, that is, after the completion of the function, the RefPtr points to the same element as from.
from | The smart pointer we wish to make a copy. |
Definition at line 79 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE RefPtr< Val > & gum::RefPtr< Val >::operator= | ( | Val * | from | ) |
Copy operator.
The operator= may throw exceptions when the dumb pointer previously pointed to by the RefPtr is deallocated (that is, the destructor of the object pointed to may throw an exception). However, even when this occurs, the RefPtr guarrantees that its state is coherent: either it could succeed to encapsulate the dumb pointer and this one is referenced once, or even encapsulating the new pointer failed and the RefPtr points toward the 0 pointer.
from | the dumb pointer we wish to encapsulate. |
Definition at line 104 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
RefPtr< Val >& gum::RefPtr< Val >::operator= | ( | const RefPtr< DownVal > & | from | ) |
Copy operator for downcastable pointers.
The operator= may throw exceptions when the dumb pointer previously pointed to by the RefPtr is deallocated (that is, the destructor of the object pointed to may throw an exception). However, even when this occurs, the RefPtr guarrantees that the copy operation is correctly performed, that is, after the completion of the function, the RefPtr points to the same element as from.
DownVal | The downcastable type. |
from | the smart pointer we wish to make a copy. |
INLINE bool gum::RefPtr< Val >::operator== | ( | const RefPtr< Val > & | from | ) | const |
Checks whether two RefPtr<Val> are smart pointers for the same element.
"Pointing toward the same element" is a little ambiguous: it does not mean that the smart pointers are pointing toward the same Val instance as several RefPtr<Val> created by the constructor with *val may point toward the same val element while being unrelated (they do not share the same reference). Instead, it means that the two smart pointers share the same reference counter, i.e., that at least one of the two smarts pointers has been created using the copy operator. As a consequence both pointers point toward the same Val instance (but the converse is false).
from | The gum::RefPtr to test for equality. |
Definition at line 173 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE unsigned int gum::RefPtr< Val >::refCount | ( | ) | const |
Returns the number of smart pointer referencing the contained pointer.
Definition at line 235 of file refPtr_tpl.h.
References gum::Set< Key, Alloc >::emplace().
The swap function must access to gum::RefPtr private parts.
Definition at line 251 of file refPtr_tpl.h.
|
private |
|
private |