From b36d072ad7a7b9c6e30fcb25d6bbb001a8393468 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 10 Apr 2022 17:20:49 +0200 Subject: refactor: add factory class & make DI container return unique ptrs --- src/DI/container.hpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'src/DI/container.hpp') diff --git a/src/DI/container.hpp b/src/DI/container.hpp index 0c0155c..d5f52b8 100644 --- a/src/DI/container.hpp +++ b/src/DI/container.hpp @@ -3,9 +3,11 @@ #include "DI/interfaces/wrapper.hpp" #include "DI/concepts.hpp" +#include "DI/factory.hpp" #include "DI/object_type.hpp" #include +#include #include #include @@ -18,10 +20,12 @@ public: explicit BindingBuilder(Container *container) noexcept; template - requires std::derived_from + requires Abstract && std::derived_from void to() noexcept; - void to_factory(Interface func) noexcept; + template + requires IsFactory && std::constructible_from + void to_factory(FactoryFunc factory) noexcept; private: Container *_container; @@ -33,23 +37,24 @@ public: Container() noexcept = default; template - using Ptr = std::shared_ptr; + using WrapperPtr = std::shared_ptr; template auto bind() noexcept -> BindingBuilder; template requires Abstract - auto get() const noexcept -> Ptr; + auto get() const noexcept -> std::unique_ptr; - template - requires Function - auto get() const noexcept -> Interface; + template + requires IsFactory + auto get() const noexcept -> AFactory; - void add(BaseObjectType type, const Ptr &wrapper) noexcept; + void add(BaseObjectType type, const WrapperPtr &wrapper) noexcept; private: - std::unordered_map, ObjectTypeHasher> _bindings; + std::unordered_map, ObjectTypeHasher> + _bindings; }; #include "container.tpp" -- cgit v1.2.3-18-g5258