aGrUM  0.14.2
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 114 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 33 of file refPtr_tpl.h.

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

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

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

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

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

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

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

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

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

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

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

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

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

250  {
251  return __refcount;
252  }
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:338

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

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

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

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

220  {
221  return (__val != 0);
222  }
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:335

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

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

186  {
187  return from.__refcount != __refcount;
188  }
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:338

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

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

193  {
194  if (!__val) { GUM_ERROR(NullElement, "dereferencing a nullptr pointer"); }
195 
196  return *__val;
197  }
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:335
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

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

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

202  {
203  if (!__val) { GUM_ERROR(NullElement, "dereferencing a nullptr pointer"); }
204 
205  return *__val;
206  }
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:335
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

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

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

211  {
212  if (!__val) { GUM_ERROR(NullElement, "dereferencing a nullptr pointer"); }
213 
214  return __val;
215  }
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:335
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

◆ operator=() [1/4]

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

Definition at line 148 of file refPtr_tpl.h.

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

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

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

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

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

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

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

179  {
180  return from.__refcount == __refcount;
181  }
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:338

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

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

241  {
242  if (__refcount == 0) return 0;
243 
244  return *__refcount;
245  }
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:338

Friends And Related Function Documentation

◆ RefPtr

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

A friend to allow downcastings.

Definition at line 328 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 332 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 257 of file refPtr_tpl.h.

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

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: