5 #ifndef GKO_PUBLIC_CORE_CONFIG_REGISTRY_HPP_
6 #define GKO_PUBLIC_CORE_CONFIG_REGISTRY_HPP_
13 #include <unordered_map>
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>
31 class type_descriptor;
33 using configuration_map =
35 std::function<deferred_factory_parameter<gko::LinOpFactory>(
36 const pnode&,
const registry&, type_descriptor)>>;
42 class registry_accessor;
50 template <
typename T,
typename =
void>
54 struct base_type<T, std::enable_if_t<std::is_convertible<T*, LinOp*>::value>> {
60 T, std::enable_if_t<std::is_convertible<T*, LinOpFactory*>::value>> {
61 using type = LinOpFactory;
67 std::enable_if_t<std::is_convertible<T*, stop::CriterionFactory*>::value>> {
85 template <
typename Type>
86 allowed_ptr(std::shared_ptr<Type> obj);
93 template <
typename Type>
94 bool contains()
const;
103 template <
typename Type>
104 std::shared_ptr<Type> get()
const;
107 struct generic_container {
108 virtual ~generic_container() =
default;
111 template <
typename Type>
112 struct concrete_container : generic_container {
113 concrete_container(std::shared_ptr<Type> obj) : ptr{obj}
116 std::is_same<Type,
typename base_type<Type>::type>::value,
117 "The given type must be a base_type");
120 std::shared_ptr<Type> ptr;
123 std::shared_ptr<generic_container> data_;
127 template <
typename Type>
128 inline allowed_ptr::allowed_ptr(std::shared_ptr<Type> obj)
131 std::make_shared<concrete_container<typename base_type<Type>::type>>(
136 template <
typename Type>
137 inline bool allowed_ptr::contains()
const
139 return dynamic_cast<const concrete_container<Type>*
>(data_.get());
143 template <
typename Type>
144 inline std::shared_ptr<Type> allowed_ptr::get()
const
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;
170 friend class detail::registry_accessor;
182 registry(
const configuration_map& additional_map = {});
197 const std::unordered_map<std::string, detail::allowed_ptr>& stored_map,
198 const configuration_map& additional_map = {});
208 template <
typename T>
209 bool emplace(std::string key, std::shared_ptr<T> data);
221 template <
typename T>
222 std::shared_ptr<T> get_data(std::string key)
const;
227 const configuration_map& get_build_map()
const {
return build_map_; }
230 std::unordered_map<std::string, detail::allowed_ptr> stored_map_;
231 configuration_map build_map_;
235 template <
typename T>
238 auto it = stored_map_.emplace(key, data);
243 template <
typename T>
244 inline std::shared_ptr<T> registry::get_data(std::string key)
const
246 return gko::as<T>(stored_map_.at(key)
247 .template get<typename detail::base_type<T>::type>());
253 #endif // GKO_PUBLIC_CORE_CONFIG_REGISTRY_HPP_