[injector] fix some init issues

This commit is contained in:
Timothy Stack 2021-01-23 13:36:22 -08:00
parent 310c67c53e
commit 775d2443a6
2 changed files with 11 additions and 9 deletions

View File

@ -97,7 +97,7 @@ struct bind_multiple : multiple_storage<T> {
template<typename I>
bind_multiple& add() noexcept {
multiple_storage<T>::ms_factories[typeid(I).name()] =
multiple_storage<T>::get_factories()[typeid(I).name()] =
details::create_factory<I>();
return *this;
@ -109,10 +109,10 @@ struct bind_multiple : multiple_storage<T> {
auto single = factory();
if (sizeof...(Annotations) > 0) {
bind<T, Annotations...>::to_instance(single.get());
bind<T, Annotations...>::to_instance((T *) single.get());
}
bind<I, Annotations...>::to_instance(single.get());
multiple_storage<T>::ms_factories[typeid(I).name()] = [single]() {
multiple_storage<T>::get_factories()[typeid(I).name()] = [single]() {
return single;
};

View File

@ -84,18 +84,20 @@ struct multiple_storage {
static std::vector<std::shared_ptr<T>> create() {
std::vector<std::shared_ptr<T>> retval;
for (const auto& pair : ms_factories) {
for (const auto& pair : get_factories()) {
retval.template emplace_back(pair.second());
}
return retval;
}
protected:
static std::map<std::string, std::function<std::shared_ptr<T>()>> ms_factories;
};
using factory_map_t = std::map<std::string, std::function<std::shared_ptr<T>()>>;
template<typename T>
std::map<std::string, std::function<std::shared_ptr<T>()>>
multiple_storage<T>::ms_factories;
static factory_map_t& get_factories() {
static factory_map_t retval;
return retval;
}
};
template<typename T, typename...Annotations,
std::enable_if_t<std::is_reference<T>::value, bool> = true>