aGrUM  0.16.0
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/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 117 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 36 of file refPtr_tpl.h.

36  :
37  __val(v), __refcount(v ? new unsigned int(1U) : 0) {
38  // for debugging purposes
39  GUM_CONSTRUCTOR(RefPtr);
40  }
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:331
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:338
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:341

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

References gum::RefPtr< Val >::__refcount.

45  :
46  __val(from.__val), __refcount(from.__refcount) {
47  // for debugging purposes
48  GUM_CONS_CPY(RefPtr);
49 
50  if (__refcount) ++*__refcount;
51  }
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:331
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:338
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:341

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

References gum::RefPtr< Val >::__destroy(), gum::RefPtr< Val >::__refcount, and gum::RefPtr< Val >::__val.

173  {
174  // for debugging purposes
175  GUM_DESTRUCTOR(RefPtr);
177  }
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:331
void __destroy(unsigned int *, Val *)
A function to remove the content of the smart pointer, if any.
Definition: refPtr_tpl.h:68
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:338
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:341
+ 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 57 of file refPtr_tpl.h.

References gum::RefPtr< Val >::__refcount.

57  :
58  __val(from.__val), __refcount(from.__refcount) {
59  // for debugging purposes
60  GUM_CONS_CPY(RefPtr);
61 
62  if (__refcount) ++*__refcount;
63  }
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:331
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:338
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:341

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

Referenced by gum::RefPtr< Val >::clear(), gum::RefPtr< Val >::operator=(), and gum::RefPtr< Val >::~RefPtr().

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

References gum::RefPtr< Val >::__refcount.

253  {
254  return __refcount;
255  }
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:341

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

References gum::RefPtr< Val >::__destroy(), gum::RefPtr< Val >::__refcount, and gum::RefPtr< Val >::__val.

230  {
231  // keep track of the old pointer and reference count
232  unsigned int* old_refcount = __refcount;
233  Val* old_val = __val;
234  // set properly the dumb pointer and its refcount
235  __val = 0;
236  __refcount = 0;
237  // now try to dereference the old dumb pointer
238  __destroy(old_refcount, old_val);
239  }
void __destroy(unsigned int *, Val *)
A function to remove the content of the smart pointer, if any.
Definition: refPtr_tpl.h:68
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:338
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:341
+ 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 223 of file refPtr_tpl.h.

References gum::RefPtr< Val >::__val.

223  {
224  return (__val != 0);
225  }
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:338

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

References gum::RefPtr< Val >::__refcount.

189  {
190  return from.__refcount != __refcount;
191  }
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:341

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

References gum::RefPtr< Val >::__val, and GUM_ERROR.

196  {
197  if (!__val) { GUM_ERROR(NullElement, "dereferencing a nullptr pointer"); }
198 
199  return *__val;
200  }
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:338
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

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

References gum::RefPtr< Val >::__val, and GUM_ERROR.

205  {
206  if (!__val) { GUM_ERROR(NullElement, "dereferencing a nullptr pointer"); }
207 
208  return *__val;
209  }
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:338
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

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

References gum::RefPtr< Val >::__val, and GUM_ERROR.

214  {
215  if (!__val) { GUM_ERROR(NullElement, "dereferencing a nullptr pointer"); }
216 
217  return __val;
218  }
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:338
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

◆ operator=() [1/4]

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

Definition at line 151 of file refPtr_tpl.h.

References gum::RefPtr< Val >::__destroy(), gum::RefPtr< Val >::__refcount, and gum::RefPtr< Val >::__val.

151  {
152  // for debugging purposes
153  GUM_OP_CPY(RefPtr);
154  // keep track of the current refcount and dumb pointer
155  unsigned int* old_refcount = __refcount;
156  Val* old_val = __val;
157 
158  // perform the copy
159  __refcount = from.__refcount;
160  __val = from.__val;
161 
162  if (__refcount) ++*__refcount;
163 
164  // now try to dereference the old dumb pointer
165  __destroy(old_refcount, old_val);
166 
167  return *this;
168  }
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:331
void __destroy(unsigned int *, Val *)
A function to remove the content of the smart pointer, if any.
Definition: refPtr_tpl.h:68
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:338
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:341
+ 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 84 of file refPtr_tpl.h.

References gum::RefPtr< Val >::__destroy(), gum::RefPtr< Val >::__refcount, and gum::RefPtr< Val >::__val.

84  {
85  // avoid self assignment
86  if (__val != from.__val) {
87  // for debugging purposes
88  GUM_OP_CPY(RefPtr);
89 
90  // keep track of the current refcount and dumb pointer
91  unsigned int* old_refcount = __refcount;
92  Val* old_val = __val;
93 
94  // perform the copy
95  __refcount = from.__refcount;
96  __val = from.__val;
97 
98  if (__refcount) ++*__refcount;
99 
100  // now try to dereference the old dumb pointer
101  __destroy(old_refcount, old_val);
102  }
103 
104  return *this;
105  }
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:331
void __destroy(unsigned int *, Val *)
A function to remove the content of the smart pointer, if any.
Definition: refPtr_tpl.h:68
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:338
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:341
+ 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 110 of file refPtr_tpl.h.

References gum::RefPtr< Val >::__destroy(), gum::RefPtr< Val >::__refcount, and gum::RefPtr< Val >::__val.

110  {
111  // avoid self assignment
112  if (__val != from) {
113  // for debugging purposes
114  GUM_OP_CPY(RefPtr);
115 
116  // keep track of the current refcount and dumb pointer
117  unsigned int* old_refcount = __refcount;
118  Val* old_val = __val;
119 
120  // perform the copy
121  try {
122  if (from)
123  __refcount = new unsigned int(1U);
124  else
125  __refcount = 0;
126 
127  __val = from;
128  } catch (std::bad_alloc&) {
129  if (*old_refcount == 1) {
130  __val = from;
131  delete old_val;
132  return *this;
133  }
134 
135  __refcount = 0;
136  __val = 0;
137  throw;
138  }
139 
140  // now try to dereference the old dumb pointer
141  __destroy(old_refcount, old_val);
142  }
143 
144  return *this;
145  }
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:331
void __destroy(unsigned int *, Val *)
A function to remove the content of the smart pointer, if any.
Definition: refPtr_tpl.h:68
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:338
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:341
+ 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 182 of file refPtr_tpl.h.

References gum::RefPtr< Val >::__refcount.

182  {
183  return from.__refcount == __refcount;
184  }
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:341

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

References gum::RefPtr< Val >::__refcount.

244  {
245  if (__refcount == 0) return 0;
246 
247  return *__refcount;
248  }
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:341

Friends And Related Function Documentation

◆ RefPtr

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

A friend to allow downcastings.

Definition at line 331 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 335 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 260 of file refPtr_tpl.h.

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

Member Data Documentation

◆ __refcount

◆ __val

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

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