aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
refPtr.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
49
50#ifndef GUM_REFPTR_H
51#define GUM_REFPTR_H
52
53#include <new>
54
55#include <agrum/agrum.h>
56
57namespace gum {
58
59#ifndef DOXYGEN_SHOULD_SKIP_THIS
60
61 template < typename Val >
62 class RefPtr;
63
64 template < typename Val >
65 class HashFunc;
66#endif
67
68 /* DOXYGEN does not like template functions...
69 *
70 * @brief Swap the contents of two RefPtr.
71 * @tparam Val The type referenced by both gum::RefPtr.
72 * @param ptr1 The smart pointer the content of which we swap with that of
73 * ptr2.
74 * @param ptr2 The smart pointer the content of which we replace with that of
75 * ptr1.
76 */
79 template < typename Val >
80 void swap(RefPtr< Val >& ptr1, RefPtr< Val >& ptr2);
81
82 // ===========================================================================
83 // === SMART POINTERS ===
84 // ===========================================================================
135 template < typename Val >
136 class RefPtr {
137 public:
139 friend void swap<>(RefPtr< Val >&, RefPtr< Val >&);
140
141 // ============================================================================
143 // ============================================================================
145
169 explicit RefPtr(Val* val = 0);
170
175 RefPtr(const RefPtr< Val >& from);
176
182 template < typename DownVal >
184
190 ~RefPtr();
191
193 // ============================================================================
195 // ============================================================================
197
203 operator bool() const;
204
214 void clear();
215
220 unsigned int refCount() const;
221
223 // ============================================================================
225 // ============================================================================
227
242
257
258 RefPtr< Val >& operator=(Val* from);
259
274 template < typename DownVal >
276
293 bool operator==(const RefPtr< Val >& from) const;
294
306 bool operator!=(const RefPtr< Val >& from) const;
307
315 Val* operator->() const;
316
328 Val& operator*();
329
341 const Val& operator*() const;
342
344 // ============================================================================
346 // ============================================================================
347
348 private:
350 template < typename T >
351 friend class RefPtr;
352
354 template < typename T >
355 friend class HashFunc;
356
358 Val* _val_;
359
361 unsigned int* _refcount_;
362
364 void _destroy_(unsigned int*, Val*);
365
367 unsigned int* _refCountPtr_() const;
368 };
369
370} /* namespace gum */
371
372
373#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
374# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
375# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
376extern template class gum::RefPtr< int >;
377# endif
378# endif
379#endif
380#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
381# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
382# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
383extern template class gum::RefPtr< long >;
384# endif
385# endif
386#endif
387
388#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
389# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
390# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
391extern template class gum::RefPtr< double >;
392# endif
393# endif
394#endif
395
396
397// always include the tpl_.h as it contains only templates
399
400#endif /* GUM_REFPTR_H */
This class should be useless as only its specializations should be used.
Definition hashFunc.h:487
Smart pointers.
Definition refPtr.h:136
friend class RefPtr
A friend to allow downcastings.
Definition refPtr.h:351
~RefPtr()
Class destructor.
Definition refPtr_tpl.h:183
friend class HashFunc
A friend for hashing quickly ref pointers.
Definition refPtr.h:355
unsigned int * _refcount_
A reference counter on *val.
Definition refPtr.h:361
bool operator==(const RefPtr< Val > &from) const
Checks whether two RefPtr<Val> are smart pointers for the same element.
Definition refPtr_tpl.h:191
friend void swap(RefPtr< Val > &, RefPtr< Val > &)
The swap function must access to gum::RefPtr private parts.
Definition refPtr_tpl.h:269
unsigned int refCount() const
Returns the number of smart pointer referencing the contained pointer.
Definition refPtr_tpl.h:253
void _destroy_(unsigned int *, Val *)
A function to remove the content of the smart pointer, if any.
Definition refPtr_tpl.h:84
RefPtr(const RefPtr< DownVal > &from)
Copy constructor for downcastable pointers.
Val * operator->() const
Dereferencing operator.
Definition refPtr_tpl.h:223
unsigned int * _refCountPtr_() const
A function to return the refcount pointer.
Definition refPtr_tpl.h:262
Val * _val_
The dumb pointer encapsulated into the "smart" pointer.
Definition refPtr.h:358
RefPtr< Val > & operator=(const RefPtr< Val > &from)
Copy operator.
Definition refPtr_tpl.h:99
RefPtr< Val > & operator=(const RefPtr< DownVal > &from)
Copy operator for downcastable pointers.
void clear()
Makes the smart pointer point to 0.
Definition refPtr_tpl.h:239
Val & operator*()
Dereferencing operator.
Definition refPtr_tpl.h:205
bool operator!=(const RefPtr< Val > &from) const
Checks whether two RefPtr<Val> are smart pointers for different elements.
Definition refPtr_tpl.h:198
void swap(RefPtr< Val > &ptr1, RefPtr< Val > &ptr2)
Swap the contents of two RefPtr.
Definition refPtr_tpl.h:269
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Implementation of aGrUM's "smart" pointers.