aboutsummaryrefslogtreecommitdiff
path: root/src/DI/container.hpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-04-10 17:20:49 +0200
committerHampusM <hampus@hampusmat.com>2022-06-13 17:56:58 +0200
commitb36d072ad7a7b9c6e30fcb25d6bbb001a8393468 (patch)
tree7749fc877652bb2cf7bbd2b7c1fed5e3abe397fc /src/DI/container.hpp
parentb828d4799a84beb1afbd889e5c4cb939a8b3df78 (diff)
refactor: add factory class & make DI container return unique ptrs
Diffstat (limited to 'src/DI/container.hpp')
-rw-r--r--src/DI/container.hpp23
1 files changed, 14 insertions, 9 deletions
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 <concepts>
+#include <functional>
#include <memory>
#include <unordered_map>
@@ -18,10 +20,12 @@ public:
explicit BindingBuilder(Container *container) noexcept;
template <typename Impl>
- requires std::derived_from<Impl, Interface>
+ requires Abstract<Interface> && std::derived_from<Impl, Interface>
void to() noexcept;
- void to_factory(Interface func) noexcept;
+ template <typename FactoryFunc>
+ requires IsFactory<Interface> && std::constructible_from<Interface, FactoryFunc>
+ void to_factory(FactoryFunc factory) noexcept;
private:
Container *_container;
@@ -33,23 +37,24 @@ public:
Container() noexcept = default;
template <typename Type>
- using Ptr = std::shared_ptr<Type>;
+ using WrapperPtr = std::shared_ptr<Type>;
template <class Interface>
auto bind() noexcept -> BindingBuilder<Interface>;
template <class Interface>
requires Abstract<Interface>
- auto get() const noexcept -> Ptr<Interface>;
+ auto get() const noexcept -> std::unique_ptr<Interface>;
- template <typename Interface>
- requires Function<Interface>
- auto get() const noexcept -> Interface;
+ template <typename AFactory>
+ requires IsFactory<AFactory>
+ auto get() const noexcept -> AFactory;
- void add(BaseObjectType type, const Ptr<IGenericWrapper> &wrapper) noexcept;
+ void add(BaseObjectType type, const WrapperPtr<IGenericWrapper> &wrapper) noexcept;
private:
- std::unordered_map<BaseObjectType, Ptr<IGenericWrapper>, ObjectTypeHasher> _bindings;
+ std::unordered_map<BaseObjectType, WrapperPtr<IGenericWrapper>, ObjectTypeHasher>
+ _bindings;
};
#include "container.tpp"