aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
partialInstantiationRegister4MultiDim_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
49
50#ifndef DOXYGEN_SHOULD_SKIP_THIS
51
52# include <agrum/agrum.h>
53
55
56namespace gum {
57
58 // adds a new entry into the register
59 template < typename GUM_SCALAR >
61 const std::string& instantiation_func_name,
62 const std::string& type_multidim,
64 newFunction) {
65 // insert the new entry
66 PartialInstantiationSet* theset = nullptr;
67
68 if (!_set_.exists(instantiation_func_name)) {
69 theset = _set_.insert(instantiation_func_name, new PartialInstantiationSet).second;
70# ifdef GUM_DEBUG_MODE
71 // for debugging purposes, we should inform the aGrUM's debugger that
72 // the hashtable contained within the
73 // PartialInstantiationRegister4MultiDim
74 // will be removed at the end of the program's execution.
75 __debug__::_inc_deletion_("HashTable", __FILE__, __LINE__, "destructor of", (void*)theset);
76# endif /* GUM_DEBUG_MODE */
77 } else {
78 theset = _set_[instantiation_func_name];
79 }
80
81 theset->insert(type_multidim, newFunction);
82 }
83
84 // removes a given entry from the register
85 template < typename GUM_SCALAR >
87 const std::string& instantiation_func_name,
88 const std::string& type_multidim) {
89 if (!_set_.exists(instantiation_func_name)) return;
90
91 PartialInstantiationSet* theset = _set_[instantiation_func_name];
92
93 theset->erase(type_multidim);
94 }
95
96 // indicates whether a given entry exists in the register
97 template < typename GUM_SCALAR >
99 const std::string& instantiation_func_name,
100 const std::string& type_multidim) const {
101 if (!_set_.exists(instantiation_func_name)) return false;
102
103 const PartialInstantiationSet& theset = *(_set_[instantiation_func_name]);
104 return theset.exists(type_multidim);
105 }
106
109 template < typename GUM_SCALAR >
112 const std::string& instantiation_func_name,
113 const std::string& type_multidim) const {
114 const PartialInstantiationSet& theset = *(_set_[instantiation_func_name]);
115 return theset[type_multidim];
116 }
117
118 // a named constructor that constructs one and only one Register per data
119 // type
120 template < typename GUM_SCALAR >
123 // Here, this initialization is thread-safe due to Meyer’s Singleton property
125
126# ifdef GUM_DEBUG_MODE
127 static std::atomic_flag first = ATOMIC_FLAG_INIT;
128 if (!first.test_and_set()) {
129 // for debugging purposes, we should inform the aGrUM's debugger that
130 // the hashtable contained within the
131 // PartialInstantiationRegister4MultiDim
132 // will be removed at the end of the program's execution.
133 __debug__::_inc_deletion_("HashTable",
134 __FILE__,
135 __LINE__,
136 "destructor of",
137 (void*)&container._set_);
138 }
139# endif /* GUM_DEBUG_MODE */
140
141 return container;
142 }
143
144 // Default constructor: creates an empty register
145 template < typename GUM_SCALAR >
147
148 // destructor
149 template < typename GUM_SCALAR >
151 // remove all the sets
153 = _set_.beginSafe();
154 iter != _set_.endSafe();
155 ++iter)
156 delete iter.val();
157 }
158
159 // a function to more easily register new instantiation functions in
160 // MultiDims
161 template < typename GUM_SCALAR >
163 const std::string& instantiation_func_name,
164 const std::string& type_multidim,
166 function) {
168 type_multidim,
169 function);
170 }
171
172} /* namespace gum */
173
174#endif /* DOXYGEN_SHOULD_SKIP_THIS */
HashTableIteratorSafe< Key, Val > iterator_safe
Types for STL compliance.
Definition hashTable.h:652
A container for registering partial instantiation functions on multiDimImplementations,...
bool exists(const std::string &instantiation_func_name, const std::string &type_multidim) const
Indicates whether a given entry exists in the register.
static PartialInstantiationRegister4MultiDim & Register()
A named constructor that constructs one and only one Register per data type.
~PartialInstantiationRegister4MultiDim()
Class destructor.
PartialInstantiationRegister4MultiDim()
Default constructor: creates an empty register.
void erase(const std::string &instantiation_func_name, const std::string &type_multidim)
Removes a given entry from the register.
void insert(const std::string &instantiation_func_name, const std::string &type_multidim, PartialInstantiationPtr newFunction)
Adds a new entry into the register.
MultiDimImplementation< GUM_SCALAR > *(*)(const MultiDimImplementation< GUM_SCALAR > *, const HashTable< const DiscreteVariable *, Idx > &) PartialInstantiationPtr
The type of functions used by the register.
PartialInstantiationPtr get(const std::string &instantiation_func_name, const std::string &type_multidim) const
Returns the specialized partial instantiation operator assigned to a given type of MultiDimImplementa...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
void registerPartialInstantiation(const std::string &instantiation_func_name, const std::string &type_multidim, typename PartialInstantiationRegister4MultiDim< GUM_SCALAR >::PartialInstantiationPtr function)
A function to more easily register new instantiation functions in MultiDims.
Headers for PartialInstantiationRegister4MultiDim.