19 #include <string_view>
20 #include <type_traits>
22 #include <unordered_map>
26 namespace ActsExamples {
38 std::unordered_map<std::string, std::string> objectAliases = {});
65 std::vector<std::string_view>
similarNames(
const std::string_view& name,
67 std::size_t maxNumber)
const;
72 virtual const std::type_info&
type()
const = 0;
81 const std::type_info&
type()
const override {
return typeid(
T); }
85 std::unordered_map<std::string, std::shared_ptr<IHolder>>
m_store;
91 const char* req,
const char* act);
103 std::unique_ptr<const Acts::Logger>
logger,
104 std::unordered_map<std::string, std::string> objectAliases)
107 template <
typename T>
110 throw std::invalid_argument(
"Object can not have an empty name");
112 if (0 < m_store.count(name)) {
113 throw std::invalid_argument(
"Object '" + name +
"' already exists");
115 auto holder = std::make_shared<HolderT<T>>(std::forward<T>(object));
116 m_store.emplace(name, holder);
118 if (
auto it = m_objectAliases.find(name);
it != m_objectAliases.end()) {
119 m_store[
it->second] = holder;
124 template <
typename T>
126 ACTS_VERBOSE(
"Attempt to get object '" << name <<
"' of type "
127 <<
typeid(
T).
name());
128 auto it = m_store.find(name);
129 if (
it == m_store.end()) {
130 const auto names = similarNames(name, 10, 3);
132 std::stringstream ss;
133 if (not
names.empty()) {
134 ss <<
", similar ones are: [ ";
136 ss <<
"'" <<
names[
i] <<
"' ";
141 throw std::out_of_range(
"Object '" + name +
"' does not exists" + ss.str());
144 const IHolder* holder =
it->second.get();
146 const auto* castedHolder =
dynamic_cast<const HolderT<T>*
>(holder);
147 if (castedHolder ==
nullptr) {
148 throw std::out_of_range(
149 typeMismatchMessage(name,
typeid(
T).
name(), holder->
type().name()));
153 return castedHolder->value;
157 return m_store.find(name) != m_store.end();