Ginkgo Generated from branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
registry.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_CONFIG_REGISTRY_HPP_
6#define GKO_PUBLIC_CORE_CONFIG_REGISTRY_HPP_
7
8
9#include <complex>
10#include <functional>
11#include <map>
12#include <string>
13#include <unordered_map>
14#include <utility>
15
16
17#include <ginkgo/core/base/exception_helpers.hpp>
18#include <ginkgo/core/base/lin_op.hpp>
19#include <ginkgo/core/base/types.hpp>
20#include <ginkgo/core/base/utils_helper.hpp>
21#include <ginkgo/core/config/property_tree.hpp>
22#include <ginkgo/core/stop/criterion.hpp>
23
24
25namespace gko {
26namespace config {
27
28
29class registry;
30
31class type_descriptor;
32
33using configuration_map =
34 std::map<std::string,
35 std::function<deferred_factory_parameter<gko::LinOpFactory>(
36 const pnode&, const registry&, type_descriptor)>>;
37
38
39namespace detail {
40
41
42class registry_accessor;
43
44
50template <typename T, typename = void>
51struct base_type {};
52
53template <typename T>
54struct base_type<T, std::enable_if_t<std::is_convertible<T*, LinOp*>::value>> {
55 using type = LinOp;
56};
57
58template <typename T>
59struct base_type<
60 T, std::enable_if_t<std::is_convertible<T*, LinOpFactory*>::value>> {
61 using type = LinOpFactory;
62};
63
64template <typename T>
65struct base_type<
66 T,
67 std::enable_if_t<std::is_convertible<T*, stop::CriterionFactory*>::value>> {
68 using type = stop::CriterionFactory;
69};
70
71
76class allowed_ptr {
77public:
85 template <typename Type>
86 allowed_ptr(std::shared_ptr<Type> obj);
87
93 template <typename Type>
94 bool contains() const;
95
103 template <typename Type>
104 std::shared_ptr<Type> get() const;
105
106private:
107 struct generic_container {
108 virtual ~generic_container() = default;
109 };
110
111 template <typename Type>
112 struct concrete_container : generic_container {
113 concrete_container(std::shared_ptr<Type> obj) : ptr{obj}
114 {
115 static_assert(
116 std::is_same<Type, typename base_type<Type>::type>::value,
117 "The given type must be a base_type");
118 }
119
120 std::shared_ptr<Type> ptr;
121 };
122
123 std::shared_ptr<generic_container> data_;
124};
125
126
127template <typename Type>
128inline allowed_ptr::allowed_ptr(std::shared_ptr<Type> obj)
129{
130 data_ =
131 std::make_shared<concrete_container<typename base_type<Type>::type>>(
132 obj);
133}
134
135
136template <typename Type>
137inline bool allowed_ptr::contains() const
138{
139 return dynamic_cast<const concrete_container<Type>*>(data_.get());
140}
141
142
143template <typename Type>
144inline std::shared_ptr<Type> allowed_ptr::get() const
145{
146 GKO_THROW_IF_INVALID(this->template contains<Type>(),
147 "does not hold the requested type.");
148 return dynamic_cast<concrete_container<Type>*>(data_.get())->ptr;
149}
150
151
152} // namespace detail
153
154
168class registry final {
169public:
170 friend class detail::registry_accessor;
171
172
182 registry(const configuration_map& additional_map = {});
183
197 const std::unordered_map<std::string, detail::allowed_ptr>& stored_map,
198 const configuration_map& additional_map = {});
199
208 template <typename T>
209 bool emplace(std::string key, std::shared_ptr<T> data);
210
211protected:
221 template <typename T>
222 std::shared_ptr<T> get_data(std::string key) const;
223
227 const configuration_map& get_build_map() const { return build_map_; }
228
229private:
230 std::unordered_map<std::string, detail::allowed_ptr> stored_map_;
231 configuration_map build_map_;
232};
233
234
235template <typename T>
236inline bool registry::emplace(std::string key, std::shared_ptr<T> data)
237{
238 auto it = stored_map_.emplace(key, data);
239 return it.second;
240}
241
242
243template <typename T>
244inline std::shared_ptr<T> registry::get_data(std::string key) const
245{
246 return gko::as<T>(stored_map_.at(key)
247 .template get<typename detail::base_type<T>::type>());
248}
249
250} // namespace config
251} // namespace gko
252
253#endif // GKO_PUBLIC_CORE_CONFIG_REGISTRY_HPP_
This class stores additional context for creating Ginkgo objects from configuration files.
Definition registry.hpp:168
registry(const std::unordered_map< std::string, detail::allowed_ptr > &stored_map, const configuration_map &additional_map={})
registry constructor
registry(const configuration_map &additional_map={})
registry constructor
bool emplace(std::string key, std::shared_ptr< T > data)
Store the data with the key.
Definition registry.hpp:236
AbstractFactory< Criterion, CriterionArgs > CriterionFactory
Declares an Abstract Factory specialized for Criterions.
Definition criterion.hpp:226
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::decay_t< T > * as(U *obj)
Performs polymorphic type conversion.
Definition utils_helper.hpp:309