aGrUM  0.16.0
refPtr.h
Go to the documentation of this file.
1 
31 #ifndef GUM_REFPTR_H
32 #define GUM_REFPTR_H
33 
34 #include <new>
35 
36 #include <agrum/agrum.h>
37 
38 namespace gum {
39 
40 #ifndef DOXYGEN_SHOULD_SKIP_THIS
41 
42  template < typename Val >
43  class RefPtr;
44 
45  template < typename Val >
46  class HashFunc;
47 #endif
48 
49  /* DOXYGEN does not like template functions...
50  *
51  * @brief Swap the contents of two RefPtr.
52  * @tparam Val The type referenced by both gum::RefPtr.
53  * @param ptr1 The smart pointer the content of which we swap with that of
54  * ptr2.
55  * @param ptr2 The smart pointer the content of which we replace with that of
56  * ptr1.
57  */
60  template < typename Val >
61  void swap(RefPtr< Val >& ptr1, RefPtr< Val >& ptr2);
62 
63  // ===========================================================================
64  // === SMART POINTERS ===
65  // ===========================================================================
116  template < typename Val >
117  class RefPtr {
118  public:
120  friend void swap<>(RefPtr< Val >&, RefPtr< Val >&);
121 
122  // ============================================================================
124  // ============================================================================
126 
150  explicit RefPtr(Val* val = 0);
151 
156  RefPtr(const RefPtr< Val >& from);
157 
163  template < typename DownVal >
164  RefPtr(const RefPtr< DownVal >& from);
165 
171  ~RefPtr();
172 
174  // ============================================================================
176  // ============================================================================
178 
184  operator bool() const;
185 
195  void clear();
196 
201  unsigned int refCount() const;
202 
204  // ============================================================================
206  // ============================================================================
208 
222  RefPtr< Val >& operator=(const RefPtr< Val >& from);
223 
239  RefPtr< Val >& operator=(Val* from);
240 
255  template < typename DownVal >
256  RefPtr< Val >& operator=(const RefPtr< DownVal >& from);
257 
274  bool operator==(const RefPtr< Val >& from) const;
275 
287  bool operator!=(const RefPtr< Val >& from) const;
288 
296  Val* operator->() const;
297 
309  Val& operator*();
310 
322  const Val& operator*() const;
323 
325  // ============================================================================
327  // ============================================================================
328  private:
330  template < typename T >
331  friend class RefPtr;
332 
334  template < typename T >
335  friend class HashFunc;
336 
338  Val* __val;
339 
341  unsigned int* __refcount;
342 
344  void __destroy(unsigned int*, Val*);
345 
347  unsigned int* __refCountPtr() const;
348  };
349 
350 } /* namespace gum */
351 
352 
353 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
354 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
355 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
356 extern template class gum::RefPtr< int >;
357 # endif
358 # endif
359 #endif
360 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
361 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
362 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
363 extern template class gum::RefPtr< long >;
364 # endif
365 # endif
366 #endif
367 
368 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
369 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
370 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
371 extern template class gum::RefPtr< double >;
372 # endif
373 # endif
374 #endif
375 
376 
377 // always include the _tpl.h as it contains only templates
378 #include <agrum/core/refPtr_tpl.h>
379 
380 #endif /* GUM_REFPTR_H */
RefPtr< Val > & operator=(const RefPtr< Val > &from)
Copy operator.
Definition: refPtr_tpl.h:84
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:331
~RefPtr()
Class destructor.
Definition: refPtr_tpl.h:173
unsigned int * __refCountPtr() const
A function to return the refcount pointer.
Definition: refPtr_tpl.h:253
Val * operator->() const
Dereferencing operator.
Definition: refPtr_tpl.h:214
void __destroy(unsigned int *, Val *)
A function to remove the content of the smart pointer, if any.
Definition: refPtr_tpl.h:68
Class template representing hashing function of LpCol.
Definition: hashFunc.h:471
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Smart pointersaGrUM&#39;s smart pointers keep track of the number of times the value they point to is ref...
Definition: refPtr.h:117
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:338
bool operator!=(const RefPtr< Val > &from) const
Checks whether two RefPtr<Val> are smart pointers for different elements.
Definition: refPtr_tpl.h:189
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
bool operator==(const RefPtr< Val > &from) const
Checks whether two RefPtr<Val> are smart pointers for the same element.
Definition: refPtr_tpl.h:182
void clear()
Makes the smart pointer point to 0.
Definition: refPtr_tpl.h:230
Val & operator*()
Dereferencing operator.
Definition: refPtr_tpl.h:196
void swap(RefPtr< Val > &ptr1, RefPtr< Val > &ptr2)
Swap the contents of two RefPtr.
Definition: refPtr_tpl.h:260
unsigned int refCount() const
Returns the number of smart pointer referencing the contained pointer.
Definition: refPtr_tpl.h:244
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:341