5 #ifndef GKO_PUBLIC_CORE_CONFIG_REGISTRY_HPP_
6 #define GKO_PUBLIC_CORE_CONFIG_REGISTRY_HPP_
13 #include <unordered_map>
16 #include <ginkgo/core/base/exception_helpers.hpp>
17 #include <ginkgo/core/base/lin_op.hpp>
18 #include <ginkgo/core/base/types.hpp>
19 #include <ginkgo/core/base/utils_helper.hpp>
20 #include <ginkgo/core/config/property_tree.hpp>
21 #include <ginkgo/core/stop/criterion.hpp>
30 class type_descriptor;
32 using configuration_map =
34 std::function<deferred_factory_parameter<gko::LinOpFactory>(
35 const pnode&,
const registry&, type_descriptor)>>;
41 class registry_accessor;
49 template <
typename T,
typename =
void>
53 struct base_type<T, std::enable_if_t<std::is_convertible<T*, LinOp*>::value>> {
59 T, std::enable_if_t<std::is_convertible<T*, LinOpFactory*>::value>> {
60 using type = LinOpFactory;
66 std::enable_if_t<std::is_convertible<T*, stop::CriterionFactory*>::value>> {
84 template <
typename Type>
85 allowed_ptr(std::shared_ptr<Type> obj);
92 template <
typename Type>
93 bool contains()
const;
102 template <
typename Type>
103 std::shared_ptr<Type> get()
const;
106 struct generic_container {
107 virtual ~generic_container() =
default;
110 template <
typename Type>
111 struct concrete_container : generic_container {
112 concrete_container(std::shared_ptr<Type> obj) : ptr{obj}
115 std::is_same<Type,
typename base_type<Type>::type>::value,
116 "The given type must be a base_type");
119 std::shared_ptr<Type> ptr;
122 std::shared_ptr<generic_container> data_;
126 template <
typename Type>
127 inline allowed_ptr::allowed_ptr(std::shared_ptr<Type> obj)
130 std::make_shared<concrete_container<typename base_type<Type>::type>>(
135 template <
typename Type>
136 inline bool allowed_ptr::contains()
const
138 return dynamic_cast<const concrete_container<Type>*
>(data_.get());
142 template <
typename Type>
143 inline std::shared_ptr<Type> allowed_ptr::get()
const
145 GKO_THROW_IF_INVALID(this->
template contains<Type>(),
146 "does not hold the requested type.");
147 return dynamic_cast<concrete_container<Type>*
>(data_.get())->ptr;
169 friend class detail::registry_accessor;
181 registry(
const configuration_map& additional_map = {});
196 const std::unordered_map<std::string, detail::allowed_ptr>& stored_map,
197 const configuration_map& additional_map = {});
207 template <
typename T>
208 bool emplace(std::string key, std::shared_ptr<T> data);
220 template <
typename T>
221 std::shared_ptr<T> get_data(std::string key)
const;
226 const configuration_map& get_build_map()
const {
return build_map_; }
229 std::unordered_map<std::string, detail::allowed_ptr> stored_map_;
230 configuration_map build_map_;
234 template <
typename T>
237 auto it = stored_map_.emplace(key, data);
242 template <
typename T>
243 inline std::shared_ptr<T> registry::get_data(std::string key)
const
245 return gko::as<T>(stored_map_.at(key)
246 .template get<typename detail::base_type<T>::type>());
252 #endif // GKO_PUBLIC_CORE_CONFIG_REGISTRY_HPP_