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