aGrUM  0.14.2
refPtr.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
28 #ifndef GUM_REFPTR_H
29 #define GUM_REFPTR_H
30 
31 #include <new>
32 
33 #include <agrum/agrum.h>
34 
35 namespace gum {
36 
37 #ifndef DOXYGEN_SHOULD_SKIP_THIS
38 
39  template < typename Val >
40  class RefPtr;
41 
42  template < typename Val >
43  class HashFunc;
44 #endif
45 
46  /* DOXYGEN does not like template functions...
47  *
48  * @brief Swap the contents of two RefPtr.
49  * @tparam Val The type referenced by both gum::RefPtr.
50  * @param ptr1 The smart pointer the content of which we swap with that of
51  * ptr2.
52  * @param ptr2 The smart pointer the content of which we replace with that of
53  * ptr1.
54  */
57  template < typename Val >
58  void swap(RefPtr< Val >& ptr1, RefPtr< Val >& ptr2);
59 
60  // ===========================================================================
61  // === SMART POINTERS ===
62  // ===========================================================================
113  template < typename Val >
114  class RefPtr {
115  public:
117  friend void swap<>(RefPtr< Val >&, RefPtr< Val >&);
118 
119  // ============================================================================
121  // ============================================================================
123 
147  explicit RefPtr(Val* val = 0);
148 
153  RefPtr(const RefPtr< Val >& from);
154 
160  template < typename DownVal >
161  RefPtr(const RefPtr< DownVal >& from);
162 
168  ~RefPtr();
169 
171  // ============================================================================
173  // ============================================================================
175 
181  operator bool() const;
182 
192  void clear();
193 
198  unsigned int refCount() const;
199 
201  // ============================================================================
203  // ============================================================================
205 
219  RefPtr< Val >& operator=(const RefPtr< Val >& from);
220 
236  RefPtr< Val >& operator=(Val* from);
237 
252  template < typename DownVal >
253  RefPtr< Val >& operator=(const RefPtr< DownVal >& from);
254 
271  bool operator==(const RefPtr< Val >& from) const;
272 
284  bool operator!=(const RefPtr< Val >& from) const;
285 
293  Val* operator->() const;
294 
306  Val& operator*();
307 
319  const Val& operator*() const;
320 
322  // ============================================================================
324  // ============================================================================
325  private:
327  template < typename T >
328  friend class RefPtr;
329 
331  template < typename T >
332  friend class HashFunc;
333 
335  Val* __val;
336 
338  unsigned int* __refcount;
339 
341  void __destroy(unsigned int*, Val*);
342 
344  unsigned int* __refCountPtr() const;
345  };
346 
347 } /* namespace gum */
348 
349 
350 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
351 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
352 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
353 extern template class gum::RefPtr< int >;
354 # endif
355 # endif
356 #endif
357 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
358 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
359 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
360 extern template class gum::RefPtr< long >;
361 # endif
362 # endif
363 #endif
364 
365 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
366 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
367 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
368 extern template class gum::RefPtr< double >;
369 # endif
370 # endif
371 #endif
372 
373 
374 // always include the _tpl.h as it contains only templates
375 #include <agrum/core/refPtr_tpl.h>
376 
377 #endif /* GUM_REFPTR_H */
RefPtr< Val > & operator=(const RefPtr< Val > &from)
Copy operator.
Definition: refPtr_tpl.h:81
friend class RefPtr
A friend to allow downcastings.
Definition: refPtr.h:328
~RefPtr()
Class destructor.
Definition: refPtr_tpl.h:170
unsigned int * __refCountPtr() const
A function to return the refcount pointer.
Definition: refPtr_tpl.h:250
Val * operator->() const
Dereferencing operator.
Definition: refPtr_tpl.h:211
void __destroy(unsigned int *, Val *)
A function to remove the content of the smart pointer, if any.
Definition: refPtr_tpl.h:65
Class template representing hashing function of LpCol.
Definition: hashFunc.h:469
gum is the global namespace for all aGrUM entities
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:114
Val * __val
The dumb pointer encapsulated into the "smart" pointer.
Definition: refPtr.h:335
bool operator!=(const RefPtr< Val > &from) const
Checks whether two RefPtr<Val> are smart pointers for different elements.
Definition: refPtr_tpl.h:186
Implementation of aGrUM&#39;s "smart" pointers.
bool operator==(const RefPtr< Val > &from) const
Checks whether two RefPtr<Val> are smart pointers for the same element.
Definition: refPtr_tpl.h:179
void clear()
Makes the smart pointer point to 0.
Definition: refPtr_tpl.h:227
Val & operator*()
Dereferencing operator.
Definition: refPtr_tpl.h:193
void swap(RefPtr< Val > &ptr1, RefPtr< Val > &ptr2)
Swap the contents of two RefPtr.
Definition: refPtr_tpl.h:257
unsigned int refCount() const
Returns the number of smart pointer referencing the contained pointer.
Definition: refPtr_tpl.h:241
unsigned int * __refcount
A reference counter on *val.
Definition: refPtr.h:338