aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
refPtr_tpl.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#pragma once
41
42
50
51namespace gum {
52
53 // default constructor
54
55 template < typename Val >
56 INLINE RefPtr< Val >::RefPtr(Val* v) : _val_(v), _refcount_(v ? new unsigned int(1U) : 0) {
57 GUM_CONSTRUCTOR(RefPtr);
58 }
59
60 // copy constructor
61
62 template < typename Val >
64 _val_(from._val_), _refcount_(from._refcount_) {
65 GUM_CONS_CPY(RefPtr);
66
67 if (_refcount_) ++*_refcount_;
68 }
69
70 // copy constructor for castable pointers
71
72 template < typename Val >
73 template < typename DownVal >
75 _val_(from._val_), _refcount_(from._refcount_) {
76 GUM_CONS_CPY(RefPtr);
77
78 if (_refcount_) ++*_refcount_;
79 }
80
81 // removes the current content of the smart pointer
82
83 template < typename Val >
84 INLINE void RefPtr< Val >::_destroy_(unsigned int* count, Val* v) {
85 if (count) {
86 if (*count == 1U) {
87 // do not change the order of the deletes (this prevents memory leaks
88 // when
89 // the delete of v fails (note that this should probably never happen))
90 delete count;
91 delete v;
92 } else --*count;
93 }
94 }
95
96 // copy operator
97
98 template < typename Val >
100 // avoid self assignment
101 if (_val_ != from._val_) {
102 GUM_OP_CPY(RefPtr)
103
104 // keep track of the current refcount and dumb pointer
105 unsigned int* old_refcount = _refcount_;
106 Val* old_val = _val_;
107
108 // perform the copy
109 _refcount_ = from._refcount_;
110 _val_ = from._val_;
111
112 if (_refcount_) ++*_refcount_;
113
114 // now try to dereference the old dumb pointer
115 _destroy_(old_refcount, old_val);
116 }
117
118 return *this;
119 }
120
121 // copy operator
122
123 template < typename Val >
125 // avoid self assignment
126 if (_val_ != from) {
127 GUM_OP_CPY(RefPtr);
128
129 // keep track of the current refcount and dumb pointer
130 unsigned int* old_refcount = _refcount_;
131 Val* old_val = _val_;
132
133 // perform the copy
134 try {
135 if (from) _refcount_ = new unsigned int(1U);
136 else _refcount_ = 0;
137
138 _val_ = from;
139 } catch (std::bad_alloc&) {
140 if (*old_refcount == 1) {
141 _val_ = from;
142 delete old_val;
143 return *this;
144 }
145
146 _refcount_ = 0;
147 _val_ = 0;
148 throw;
149 }
150
151 // now try to dereference the old dumb pointer
152 _destroy_(old_refcount, old_val);
153 }
154
155 return *this;
156 }
157
158 // copy operator for downcastable pointers
159
160 template < typename Val >
161 template < typename DownVal >
163 GUM_OP_CPY(RefPtr)
164 // keep track of the current refcount and dumb pointer
165 unsigned int* old_refcount = _refcount_;
166 Val* old_val = _val_;
167
168 // perform the copy
169 _refcount_ = from._refcount_;
170 _val_ = from._val_;
171
172 if (_refcount_) ++*_refcount_;
173
174 // now try to dereference the old dumb pointer
175 _destroy_(old_refcount, old_val);
176
177 return *this;
178 }
179
180 // destructor: it decrements the Val's reference count
181
182 template < typename Val >
184 GUM_DESTRUCTOR(RefPtr);
186 }
187
188 // checks whether two RefPtr<Val> are smart pointers for the same element
189
190 template < typename Val >
191 INLINE bool RefPtr< Val >::operator==(const RefPtr< Val >& from) const {
192 return from._refcount_ == _refcount_;
193 }
194
195 // checks whether two RefPtr<Val> are smart pointers for differen elements
196
197 template < typename Val >
198 INLINE bool RefPtr< Val >::operator!=(const RefPtr< Val >& from) const {
199 return from._refcount_ != _refcount_;
200 }
201
202 // dereferencing operator
203
204 template < typename Val >
206 if (!_val_) { GUM_ERROR(NullElement, "dereferencing a nullptr pointer") }
207
208 return *_val_;
209 }
210
211 // dereferencing operator
212
213 template < typename Val >
214 INLINE const Val& RefPtr< Val >::operator*() const {
215 if (!_val_) { GUM_ERROR(NullElement, "dereferencing a nullptr pointer") }
216
217 return *_val_;
218 }
219
220 // dereferencing operator
221
222 template < typename Val >
223 INLINE Val* RefPtr< Val >::operator->() const {
224 if (!_val_) { GUM_ERROR(NullElement, "dereferencing a nullptr pointer") }
225
226 return _val_;
227 }
228
229 // checks whether a RefPtr points toward something
230
231 template < typename Val >
233 return (_val_ != 0);
234 }
235
236 // dereference what was referenced by the smart pointer
237
238 template < typename Val >
239 INLINE void RefPtr< Val >::clear() {
240 // keep track of the old pointer and reference count
241 unsigned int* old_refcount = _refcount_;
242 Val* old_val = _val_;
243 // set properly the dumb pointer and its refcount
244 _val_ = 0;
245 _refcount_ = 0;
246 // now try to dereference the old dumb pointer
247 _destroy_(old_refcount, old_val);
248 }
249
250 // returns the number of references on the contained pointer
251
252 template < typename Val >
253 INLINE unsigned int RefPtr< Val >::refCount() const {
254 if (_refcount_ == 0) return 0;
255
256 return *_refcount_;
257 }
258
259 // returns the refcount pointer
260
261 template < typename Val >
262 INLINE unsigned int* RefPtr< Val >::_refCountPtr_() const {
263 return _refcount_;
264 }
265
266 // replace the contents of two RefPtr
267
268 template < typename Val >
269 void swap(RefPtr< Val >& ptr1, RefPtr< Val >& ptr2) {
270 // save from's content
271 Val* tmp_val = ptr2._val_;
272 unsigned int* tmp_refcount = ptr2._refcount_;
273 // modify from's content
274 ptr2._refcount_ = ptr1._refcount_;
275 ptr2._val_ = ptr1._val_;
276 // modify this's content
277 ptr1._val_ = tmp_val;
278 ptr1._refcount_ = tmp_refcount;
279 }
280
281} /* namespace gum */
Exception : a pointer or a reference on a nullptr (0) object.
friend class RefPtr
A friend to allow downcastings.
Definition refPtr.h:351
~RefPtr()
Class destructor.
Definition refPtr_tpl.h:183
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
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
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
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
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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
Class providing aGrUM's "smart" pointers.