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>
33using configuration_map =
35 std::function<deferred_factory_parameter<gko::LinOpFactory>(
36 const pnode&,
const registry&, type_descriptor)>>;
42class registry_accessor;
50template <
typename T,
typename =
void>
54struct 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_;
127template <
typename Type>
128inline allowed_ptr::allowed_ptr(std::shared_ptr<Type> obj)
131 std::make_shared<concrete_container<typename base_type<Type>::type>>(
136template <
typename Type>
137inline bool allowed_ptr::contains()
const
139 return dynamic_cast<const concrete_container<Type>*
>(data_.get());
143template <
typename Type>
144inline 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_;
238 auto it = stored_map_.emplace(key, data);
244inline std::shared_ptr<T> registry::get_data(std::string key)
const
247 .template get<
typename detail::base_type<T>::type>());
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