diff options
author | HampusM <hampus@hampusmat.com> | 2022-04-30 16:28:13 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:58 +0200 |
commit | 40d02748924aa7c48b04cf948204d8dacdfbbc74 (patch) | |
tree | 4d07a3703207295799b02cef52618a8f0c820542 /src/DI/container.tpp | |
parent | db6edcd473a684420e9a7611b24462df21165c1b (diff) |
refactor: replace DI files with the yacppdic library
Diffstat (limited to 'src/DI/container.tpp')
-rw-r--r-- | src/DI/container.tpp | 82 |
1 files changed, 0 insertions, 82 deletions
diff --git a/src/DI/container.tpp b/src/DI/container.tpp deleted file mode 100644 index d81a0cc..0000000 --- a/src/DI/container.tpp +++ /dev/null @@ -1,82 +0,0 @@ -#pragma once - -#include "container.hpp" - -#include "function_wrapper.hpp" -#include "object_wrapper.hpp" - -#include <iostream> - -template <typename Interface> -BindingBuilder<Interface>::BindingBuilder(Container *container) noexcept - : _container(container) -{ -} - -template <typename Interface> -template <typename Impl> -requires Abstract<Interface> && std::derived_from<Impl, Interface> -void BindingBuilder<Interface>::to() noexcept -{ - using Wrapper = ObjectWrapper<Interface, Impl>; - - auto wrapper = Container::WrapperPtr<Wrapper>(new Wrapper(*_container)); - - _container->add( - ObjectType<Interface>(), - std::dynamic_pointer_cast<IGenericWrapper>(wrapper) - ); -} - -template <typename Interface> -template <typename FactoryFunc> -requires IsFactory<Interface> && std::constructible_from<Interface, FactoryFunc> -void BindingBuilder<Interface>::to_factory(FactoryFunc factory) noexcept -{ - using Wrapper = FunctionWrapper<Interface>; - - auto wrapper = Container::WrapperPtr<Wrapper>(new Wrapper(factory)); - - _container->add( - ObjectType<Interface>(), - std::dynamic_pointer_cast<IGenericWrapper>(wrapper) - ); -} - -template <typename Interface> -auto Container::bind() noexcept -> BindingBuilder<Interface> -{ - return BindingBuilder<Interface>(this); -} - -template <typename Interface> -requires Abstract<Interface> -auto Container::get() const noexcept -> std::unique_ptr<Interface> -{ - ObjectType<Interface> interface_type; - - if (_bindings.count(interface_type) == 0) - { - std::cerr - << "Error: Tried to get a item from the container using unbound interface '" - << interface_type.name() << "'" << std::endl; - exit(EXIT_FAILURE); - } - - auto wrapper = std::dynamic_pointer_cast<IWrapper<std::unique_ptr<Interface>>>( - _bindings.at(interface_type) - ); - - return wrapper->get(); -} - -template <typename AFactory> -requires IsFactory<AFactory> -auto Container::get() const noexcept -> AFactory -{ - auto wrapper = - std::dynamic_pointer_cast<IWrapper<AFactory>>(_bindings.at(ObjectType<AFactory>()) - ); - - return wrapper->get(); -} |