aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gum::RefPtr< Val > Class Template Reference

Smart pointers. More...

#include <agrum/base/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.
 RefPtr (const RefPtr< Val > &from)
 Copy constructor.
template<typename DownVal>
 RefPtr (const RefPtr< DownVal > &from)
 Copy constructor for downcastable pointers.
 ~RefPtr ()
 Class destructor.
Accessors / Modifiers
 operator bool () const
 Checks whether a RefPtr points toward something.
void clear ()
 Makes the smart pointer point to 0.
unsigned int refCount () const
 Returns the number of smart pointer referencing the contained pointer.
Operators
RefPtr< Val > & operator= (const RefPtr< Val > &from)
 Copy operator.
RefPtr< Val > & operator= (Val *from)
 Copy operator.
template<typename DownVal>
RefPtr< Val > & operator= (const RefPtr< DownVal > &from)
 Copy operator for downcastable pointers.
bool operator== (const RefPtr< Val > &from) const
 Checks whether two RefPtr<Val> are smart pointers for the same element.
bool operator!= (const RefPtr< Val > &from) const
 Checks whether two RefPtr<Val> are smart pointers for different elements.
Val * operator-> () const
 Dereferencing operator.
Val & operator* ()
 Dereferencing operator.
const Val & operator* () const
 Const dereferencing operator.

Friends

void swap (RefPtr< Val > &ptr1, RefPtr< Val > &ptr2)
 The swap function must access to gum::RefPtr private parts.

Internals

template<typename T>
class RefPtr
 A friend to allow downcastings.
template<typename T>
class HashFunc
 A friend for hashing quickly ref pointers.
Val * _val_
 The dumb pointer encapsulated into the "smart" pointer.
unsigned int * _refcount_
 A reference counter on *val.
void _destroy_ (unsigned int *, Val *)
 A function to remove the content of the smart pointer, if any.
unsigned int * _refCountPtr_ () const
 A function to return the refcount pointer.

Detailed Description

template<typename Val>
class gum::RefPtr< Val >

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

RefPtr ( new myObject )
friend class RefPtr
A friend to allow downcastings.
Definition refPtr.h:351

In particular, never deallocate yourself a dumb pointer you have encapsulated into a smart pointer.

Usage example:
// creation of smart pointer
RefPtr<int> ptr1 (new int (4));
// copying (and sharing) this pointer into new smart pointers
RefPtr<int> ptr2 = ptr1, ptr3;
ptr3 = ptr1;
// make ptr2 point toward nothing (this does not deallocate int (4) as it
// is pointed to by ptr1 and ptr3)
ptr2.clear ();
// modifying the value pointed to by the dumb pointer contained in ptr1
*ptr1 = 5;
// print the content of ptr3
cerr << *ptr3 << " = 5" << endl;
// check whether ptr1 and ptr3 reference the same dumb pointer
if (ptr1 == ptr2) cerr << "reference the same dumb pointer" << endl;
// check whether ptr1 and ptr2 contain a dumb pointer
if (ptr1 && !ptr2) cerr << "check containers" << endl;
void clear()
Makes the smart pointer point to 0.
Definition refPtr_tpl.h:239
Template Parameters
ValThe type referenced by the gum::RefPtr.

Definition at line 136 of file refPtr.h.

Constructor & Destructor Documentation

◆ RefPtr() [1/4]

template<typename Val>
INLINE gum::RefPtr< Val >::RefPtr ( Val * val = 0)
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:

RefPtr( new myObject )

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.

Parameters
valThe dumb pointer encapsulated into the object (make sure it is allocated on the heap)
Exceptions
std::bad_allocRaised if the complete RefPtr structure cannot be set properly.

Definition at line 56 of file refPtr_tpl.h.

56 : _val_(v), _refcount_(v ? new unsigned int(1U) : 0) {
58 }
Smart pointers.
Definition refPtr.h:136
unsigned int * _refcount_
A reference counter on *val.
Definition refPtr.h:361
Val * _val_
The dumb pointer encapsulated into the "smart" pointer.
Definition refPtr.h:358

References RefPtr, _refcount_, and _val_.

Here is the call graph for this function:

◆ RefPtr() [2/4]

template<typename Val>
INLINE gum::RefPtr< Val >::RefPtr ( const RefPtr< Val > & from)

Copy constructor.

Parameters
fromthe smart pointer we wish to make a copy.

Definition at line 63 of file refPtr_tpl.h.

References RefPtr, _refcount_, and _val_.

Here is the call graph for this function:

◆ RefPtr() [3/4]

template<typename Val>
template<typename DownVal>
gum::RefPtr< Val >::RefPtr ( const RefPtr< DownVal > & from)

Copy constructor for downcastable pointers.

Parameters
fromthe smart pointer we wish to make a copy.
Template Parameters
DownValThe downcastable type.

References RefPtr.

Here is the call graph for this function:

◆ ~RefPtr()

template<typename Val>
INLINE gum::RefPtr< Val >::~RefPtr ( )

Class destructor.

Decrements the ref count and deletes if necessary the dumb pointer.

Definition at line 183 of file refPtr_tpl.h.

183 {
186 }
void _destroy_(unsigned int *, Val *)
A function to remove the content of the smart pointer, if any.
Definition refPtr_tpl.h:84

References RefPtr, _destroy_(), _refcount_, and _val_.

Here is the call graph for this function:

◆ RefPtr() [4/4]

template<typename Val>
template<typename DownVal>
INLINE gum::RefPtr< Val >::RefPtr ( const RefPtr< DownVal > & from)

Definition at line 74 of file refPtr_tpl.h.

References RefPtr, _refcount_, and _val_.

Here is the call graph for this function:

Member Function Documentation

◆ _destroy_()

template<typename Val>
INLINE void gum::RefPtr< Val >::_destroy_ ( unsigned int * count,
Val * v )
private

A function to remove the content of the smart pointer, if any.

Definition at line 84 of file refPtr_tpl.h.

84 {
85 if (count) {
86 if (*count == 1U) {
87 // do not change the order of the deletes (this prevents memory leaks
88 // when
89 // the delete of v fails (note that this should probably never happen))
90 delete count;
91 delete v;
92 } else --*count;
93 }
94 }

Referenced by ~RefPtr(), clear(), operator=(), operator=(), and operator=().

Here is the caller graph for this function:

◆ _refCountPtr_()

template<typename Val>
INLINE unsigned int * gum::RefPtr< Val >::_refCountPtr_ ( ) const
private

A function to return the refcount pointer.

Definition at line 262 of file refPtr_tpl.h.

262 {
263 return _refcount_;
264 }

References _refcount_.

◆ clear()

template<typename Val>
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 239 of file refPtr_tpl.h.

239 {
240 // keep track of the old pointer and reference count
241 unsigned int* old_refcount = _refcount_;
242 Val* old_val = _val_;
243 // set properly the dumb pointer and its refcount
244 _val_ = 0;
245 _refcount_ = 0;
246 // now try to dereference the old dumb pointer
248 }

References _destroy_(), _refcount_, and _val_.

Here is the call graph for this function:

◆ operator bool()

template<typename Val>
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 232 of file refPtr_tpl.h.

232 {
233 return (_val_ != 0);
234 }

References RefPtr, and _val_.

Here is the call graph for this function:

◆ operator!=()

template<typename Val>
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).

Parameters
fromThe gum::RefPtr to test for inequality.
Returns
Returns true if this and from differ.

Definition at line 198 of file refPtr_tpl.h.

198 {
199 return from._refcount_ != _refcount_;
200 }

References RefPtr, and _refcount_.

Here is the call graph for this function:

◆ operator*() [1/2]

template<typename Val>
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.

Returns
Returns a reference over the value referenced by this gum::RefPtr.
Exceptions
NullElementRaised whenever the RefPtr points to 0.

Definition at line 205 of file refPtr_tpl.h.

205 {
206 if (!_val_) { GUM_ERROR(NullElement, "dereferencing a nullptr pointer") }
207
208 return *_val_;
209 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References _val_, and GUM_ERROR.

◆ operator*() [2/2]

template<typename Val>
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.

Returns
Returns a constant reference over the value referenced by this gum::RefPtr.
Exceptions
NullElementRaised whenever the RefPtr points to 0.

Definition at line 214 of file refPtr_tpl.h.

214 {
215 if (!_val_) { GUM_ERROR(NullElement, "dereferencing a nullptr pointer") }
216
217 return *_val_;
218 }

References _val_, and GUM_ERROR.

◆ operator->()

template<typename Val>
INLINE Val * gum::RefPtr< Val >::operator-> ( ) const

Dereferencing operator.

This operator allows developers to write code like refptr->member().

Returns
Returns a pointer over the value referenced by this gum::RefPtr.
Exceptions
NullElementRaised whenever the smart pointer points toward 0.

Definition at line 223 of file refPtr_tpl.h.

223 {
224 if (!_val_) { GUM_ERROR(NullElement, "dereferencing a nullptr pointer") }
225
226 return _val_;
227 }

References _val_, and GUM_ERROR.

◆ operator=() [1/4]

template<typename Val>
template<typename DownVal>
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.

Template Parameters
DownValThe downcastable type.
Parameters
fromthe smart pointer we wish to make a copy.
Returns
Returns this gum::RefPtr.

References RefPtr.

Here is the call graph for this function:

◆ operator=() [2/4]

template<typename Val>
template<typename DownVal>
INLINE RefPtr< Val > & gum::RefPtr< Val >::operator= ( const RefPtr< DownVal > & from)

Definition at line 162 of file refPtr_tpl.h.

162 {
164 // keep track of the current refcount and dumb pointer
165 unsigned int* old_refcount = _refcount_;
166 Val* old_val = _val_;
167
168 // perform the copy
170 _val_ = from._val_;
171
172 if (_refcount_) ++*_refcount_;
173
174 // now try to dereference the old dumb pointer
176
177 return *this;
178 }

References RefPtr, _destroy_(), _refcount_, and _val_.

Here is the call graph for this function:

◆ operator=() [3/4]

template<typename Val>
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.

Parameters
fromThe smart pointer we wish to make a copy.
Returns
Returns this gum::RefPtr.

Definition at line 99 of file refPtr_tpl.h.

99 {
100 // avoid self assignment
101 if (_val_ != from._val_) {
103
104 // keep track of the current refcount and dumb pointer
105 unsigned int* old_refcount = _refcount_;
106 Val* old_val = _val_;
107
108 // perform the copy
110 _val_ = from._val_;
111
112 if (_refcount_) ++*_refcount_;
113
114 // now try to dereference the old dumb pointer
116 }
117
118 return *this;
119 }

References RefPtr, _destroy_(), _refcount_, and _val_.

Here is the call graph for this function:

◆ operator=() [4/4]

template<typename Val>
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.

Parameters
fromthe dumb pointer we wish to encapsulate.
Returns
Returns this gum::RefPtr.

Definition at line 124 of file refPtr_tpl.h.

124 {
125 // avoid self assignment
126 if (_val_ != from) {
128
129 // keep track of the current refcount and dumb pointer
130 unsigned int* old_refcount = _refcount_;
131 Val* old_val = _val_;
132
133 // perform the copy
134 try {
135 if (from) _refcount_ = new unsigned int(1U);
136 else _refcount_ = 0;
137
138 _val_ = from;
139 } catch (std::bad_alloc&) {
140 if (*old_refcount == 1) {
141 _val_ = from;
142 delete old_val;
143 return *this;
144 }
145
146 _refcount_ = 0;
147 _val_ = 0;
148 throw;
149 }
150
151 // now try to dereference the old dumb pointer
153 }
154
155 return *this;
156 }

References RefPtr, _destroy_(), _refcount_, and _val_.

Here is the call graph for this function:

◆ operator==()

template<typename Val>
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).

Parameters
fromThe gum::RefPtr to test for equality.
Returns
Returns true if this and from are equal.

Definition at line 191 of file refPtr_tpl.h.

191 {
192 return from._refcount_ == _refcount_;
193 }

References RefPtr, and _refcount_.

Here is the call graph for this function:

◆ refCount()

template<typename Val>
INLINE unsigned int gum::RefPtr< Val >::refCount ( ) const

Returns the number of smart pointer referencing the contained pointer.

Definition at line 253 of file refPtr_tpl.h.

253 {
254 if (_refcount_ == 0) return 0;
255
256 return *_refcount_;
257 }

References _refcount_.

◆ RefPtr

template<typename Val>
template<typename T>
friend class RefPtr
friend

A friend to allow downcastings.

Definition at line 351 of file refPtr.h.

References RefPtr.

Referenced by RefPtr, RefPtr(), RefPtr(), RefPtr(), ~RefPtr(), operator bool(), operator!=(), operator=(), operator=(), operator=(), operator==(), and swap.

◆ HashFunc

template<typename Val>
template<typename T>
friend class HashFunc
friend

A friend for hashing quickly ref pointers.

Definition at line 355 of file refPtr.h.

References HashFunc.

Referenced by HashFunc.

◆ swap

template<typename Val>
void swap ( RefPtr< Val > & ptr1,
RefPtr< Val > & ptr2 )
friend

The swap function must access to gum::RefPtr private parts.

Definition at line 269 of file refPtr_tpl.h.

269 {
270 // save from's content
272 unsigned int* tmp_refcount = ptr2._refcount_;
273 // modify from's content
276 // modify this's content
279 }

References RefPtr, _refcount_, and _val_.

Member Data Documentation

◆ _refcount_

template<typename Val>
unsigned int* gum::RefPtr< Val >::_refcount_
private

A reference counter on *val.

Definition at line 361 of file refPtr.h.

Referenced by RefPtr(), RefPtr(), RefPtr(), ~RefPtr(), _refCountPtr_(), clear(), operator!=(), operator=(), operator=(), operator=(), operator==(), refCount(), and swap.

◆ _val_

template<typename Val>
Val* gum::RefPtr< Val >::_val_
private

The dumb pointer encapsulated into the "smart" pointer.

Definition at line 358 of file refPtr.h.

Referenced by RefPtr(), RefPtr(), RefPtr(), ~RefPtr(), clear(), operator bool(), operator*(), operator*(), operator->(), operator=(), operator=(), operator=(), and swap.


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