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

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...
 

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 )

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;
Template Parameters
ValThe type referenced by the gum::RefPtr.

Definition at line 116 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 35 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

35  :
36  val__(v), refcount__(v ? new unsigned int(1U) : 0) {
37  // for debugging purposes
38  GUM_CONSTRUCTOR(RefPtr);
39  }
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:330
unsigned int * refcount__
A reference counter on *val.
Definition: refPtr.h:340
Val * val__
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:337
+ 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 44 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

44  :
45  val__(from.val__), refcount__(from.refcount__) {
46  // for debugging purposes
47  GUM_CONS_CPY(RefPtr);
48 
49  if (refcount__) ++*refcount__;
50  }
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:330
unsigned int * refcount__
A reference counter on *val.
Definition: refPtr.h:340
Val * val__
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:337
+ 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.

◆ ~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 172 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

172  {
173  // for debugging purposes
174  GUM_DESTRUCTOR(RefPtr);
176  }
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:330
unsigned int * refcount__
A reference counter on *val.
Definition: refPtr.h:340
void destroy__(unsigned int *, Val *)
A function to remove the content of the smart pointer, if any.
Definition: refPtr_tpl.h:67
Val * val__
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:337
+ 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 56 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

56  :
57  val__(from.val__), refcount__(from.refcount__) {
58  // for debugging purposes
59  GUM_CONS_CPY(RefPtr);
60 
61  if (refcount__) ++*refcount__;
62  }
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:330
unsigned int * refcount__
A reference counter on *val.
Definition: refPtr.h:340
Val * val__
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:337
+ Here is the call graph for this function:

Member Function Documentation

◆ 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 229 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

229  {
230  // keep track of the old pointer and reference count
231  unsigned int* old_refcount = refcount__;
232  Val* old_val = val__;
233  // set properly the dumb pointer and its refcount
234  val__ = 0;
235  refcount__ = 0;
236  // now try to dereference the old dumb pointer
237  destroy__(old_refcount, old_val);
238  }
unsigned int * refcount__
A reference counter on *val.
Definition: refPtr.h:340
void destroy__(unsigned int *, Val *)
A function to remove the content of the smart pointer, if any.
Definition: refPtr_tpl.h:67
Val * val__
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:337
+ Here is the call graph for this function:

◆ 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 67 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

67  {
68  if (count) {
69  if (*count == 1U) {
70  // do not change the order of the deletes (this prevents memory leaks
71  // when
72  // the delete of v fails (note that this should probably never happen))
73  delete count;
74  delete v;
75  } else
76  --*count;
77  }
78  }
+ 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 222 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

222  {
223  return (val__ != 0);
224  }
Val * val__
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:337
+ 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 188 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

188  {
189  return from.refcount__ != refcount__;
190  }
unsigned int * refcount__
A reference counter on *val.
Definition: refPtr.h:340
+ 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 195 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

195  {
196  if (!val__) { GUM_ERROR(NullElement, "dereferencing a nullptr pointer"); }
197 
198  return *val__;
199  }
Val * val__
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:337
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ 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 204 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

204  {
205  if (!val__) { GUM_ERROR(NullElement, "dereferencing a nullptr pointer"); }
206 
207  return *val__;
208  }
Val * val__
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:337
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ 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 213 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

213  {
214  if (!val__) { GUM_ERROR(NullElement, "dereferencing a nullptr pointer"); }
215 
216  return val__;
217  }
Val * val__
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:337
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ operator=() [1/4]

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

Definition at line 150 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

150  {
151  // for debugging purposes
152  GUM_OP_CPY(RefPtr);
153  // keep track of the current refcount and dumb pointer
154  unsigned int* old_refcount = refcount__;
155  Val* old_val = val__;
156 
157  // perform the copy
158  refcount__ = from.refcount__;
159  val__ = from.val__;
160 
161  if (refcount__) ++*refcount__;
162 
163  // now try to dereference the old dumb pointer
164  destroy__(old_refcount, old_val);
165 
166  return *this;
167  }
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:330
unsigned int * refcount__
A reference counter on *val.
Definition: refPtr.h:340
void destroy__(unsigned int *, Val *)
A function to remove the content of the smart pointer, if any.
Definition: refPtr_tpl.h:67
Val * val__
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:337
+ Here is the call graph for this function:

◆ operator=() [2/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 83 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

83  {
84  // avoid self assignment
85  if (val__ != from.val__) {
86  // for debugging purposes
87  GUM_OP_CPY(RefPtr);
88 
89  // keep track of the current refcount and dumb pointer
90  unsigned int* old_refcount = refcount__;
91  Val* old_val = val__;
92 
93  // perform the copy
94  refcount__ = from.refcount__;
95  val__ = from.val__;
96 
97  if (refcount__) ++*refcount__;
98 
99  // now try to dereference the old dumb pointer
100  destroy__(old_refcount, old_val);
101  }
102 
103  return *this;
104  }
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:330
unsigned int * refcount__
A reference counter on *val.
Definition: refPtr.h:340
void destroy__(unsigned int *, Val *)
A function to remove the content of the smart pointer, if any.
Definition: refPtr_tpl.h:67
Val * val__
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:337
+ Here is the call graph for this function:

◆ operator=() [3/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 109 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

109  {
110  // avoid self assignment
111  if (val__ != from) {
112  // for debugging purposes
113  GUM_OP_CPY(RefPtr);
114 
115  // keep track of the current refcount and dumb pointer
116  unsigned int* old_refcount = refcount__;
117  Val* old_val = val__;
118 
119  // perform the copy
120  try {
121  if (from)
122  refcount__ = new unsigned int(1U);
123  else
124  refcount__ = 0;
125 
126  val__ = from;
127  } catch (std::bad_alloc&) {
128  if (*old_refcount == 1) {
129  val__ = from;
130  delete old_val;
131  return *this;
132  }
133 
134  refcount__ = 0;
135  val__ = 0;
136  throw;
137  }
138 
139  // now try to dereference the old dumb pointer
140  destroy__(old_refcount, old_val);
141  }
142 
143  return *this;
144  }
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:330
unsigned int * refcount__
A reference counter on *val.
Definition: refPtr.h:340
void destroy__(unsigned int *, Val *)
A function to remove the content of the smart pointer, if any.
Definition: refPtr_tpl.h:67
Val * val__
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:337
+ Here is the call graph for this function:

◆ operator=() [4/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.

◆ 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 181 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

181  {
182  return from.refcount__ == refcount__;
183  }
unsigned int * refcount__
A reference counter on *val.
Definition: refPtr.h:340
+ 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 243 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

243  {
244  if (refcount__ == 0) return 0;
245 
246  return *refcount__;
247  }
unsigned int * refcount__
A reference counter on *val.
Definition: refPtr.h:340
+ Here is the call 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 252 of file refPtr_tpl.h.

References gum::Set< Key, Alloc >::emplace().

252  {
253  return refcount__;
254  }
unsigned int * refcount__
A reference counter on *val.
Definition: refPtr.h:340
+ Here is the call graph for this function:

Friends And Related Function Documentation

◆ RefPtr

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

A friend to allow downcastings.

Definition at line 330 of file refPtr.h.

◆ HashFunc

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

A friend for hashing quickly ref pointers.

Definition at line 334 of file refPtr.h.

◆ 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 259 of file refPtr_tpl.h.

259  {
260  // save from's content
261  Val* tmp_val = ptr2.val__;
262  unsigned int* tmp_refcount = ptr2.refcount__;
263  // modify from's content
264  ptr2.refcount__ = ptr1.refcount__;
265  ptr2.val__ = ptr1.val__;
266  // modify this's content
267  ptr1.val__ = tmp_val;
268  ptr1.refcount__ = tmp_refcount;
269  }

Member Data Documentation

◆ refcount__

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

A reference counter on *val.

Definition at line 340 of file refPtr.h.

◆ val__

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

The dumb pointer encapsulated into the "smart" pointer.

Definition at line 337 of file refPtr.h.


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