diff options
author | HampusM <hampus@hampusmat.com> | 2022-04-10 17:20:49 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:58 +0200 |
commit | b36d072ad7a7b9c6e30fcb25d6bbb001a8393468 (patch) | |
tree | 7749fc877652bb2cf7bbd2b7c1fed5e3abe397fc /src/DI/container.tpp | |
parent | b828d4799a84beb1afbd889e5c4cb939a8b3df78 (diff) |
refactor: add factory class & make DI container return unique ptrs
Diffstat (limited to 'src/DI/container.tpp')
-rw-r--r-- | src/DI/container.tpp | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/src/DI/container.tpp b/src/DI/container.tpp index 54b09c6..13397bc 100644 --- a/src/DI/container.tpp +++ b/src/DI/container.tpp @@ -15,23 +15,25 @@ BindingBuilder<Interface>::BindingBuilder(Container *container) noexcept template <typename Interface> template <typename Impl> -requires std::derived_from<Impl, Interface> +requires Abstract<Interface> && std::derived_from<Impl, Interface> void BindingBuilder<Interface>::to() noexcept { using Wrapper = ObjectWrapper<Interface, Impl>; - auto wrapper = Container::Ptr<Wrapper>(new Wrapper(*_container)); + auto wrapper = Container::WrapperPtr<Wrapper>(new Wrapper(*_container)); _container->add(ObjectType<Interface>(), std::dynamic_pointer_cast<IGenericWrapper>(wrapper)); } template <typename Interface> -void BindingBuilder<Interface>::to_factory(Interface func) noexcept +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::Ptr<Wrapper>(new Wrapper(func)); + auto wrapper = Container::WrapperPtr<Wrapper>(new Wrapper(factory)); _container->add(ObjectType<Interface>(), std::dynamic_pointer_cast<IGenericWrapper>(wrapper)); @@ -45,7 +47,7 @@ auto Container::bind() noexcept -> BindingBuilder<Interface> template <typename Interface> requires Abstract<Interface> -auto Container::get() const noexcept -> Ptr<Interface> +auto Container::get() const noexcept -> std::unique_ptr<Interface> { ObjectType<Interface> interface_type; @@ -57,18 +59,18 @@ auto Container::get() const noexcept -> Ptr<Interface> exit(EXIT_FAILURE); } - auto wrapper = - std::dynamic_pointer_cast<IWrapper<Ptr<Interface>>>(_bindings.at(interface_type)); + auto wrapper = std::dynamic_pointer_cast<IWrapper<std::unique_ptr<Interface>>>( + _bindings.at(interface_type)); return wrapper->get(); } -template <typename Interface> -requires Function<Interface> -auto Container::get() const noexcept -> Interface +template <typename AFactory> +requires IsFactory<AFactory> +auto Container::get() const noexcept -> AFactory { - auto wrapper = std::dynamic_pointer_cast<IWrapper<Interface>>( - _bindings.at(ObjectType<Interface>())); + auto wrapper = std::dynamic_pointer_cast<IWrapper<AFactory>>( + _bindings.at(ObjectType<AFactory>())); return wrapper->get(); } |