aboutsummaryrefslogtreecommitdiff
path: root/src/DI/container.hpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-03-29 19:29:08 +0200
committerHampusM <hampus@hampusmat.com>2022-06-13 17:56:58 +0200
commit40c828455d094854d9e531fc7dd0d1a0244957b7 (patch)
tree5e9e7e5156f0907d58791b6e7e7f3adff44bb4b3 /src/DI/container.hpp
parenta8fbce594f87554b48da661c7939559be801da13 (diff)
refactor: make container use concepts
Diffstat (limited to 'src/DI/container.hpp')
-rw-r--r--src/DI/container.hpp26
1 files changed, 17 insertions, 9 deletions
diff --git a/src/DI/container.hpp b/src/DI/container.hpp
index 83f24ae..0c0155c 100644
--- a/src/DI/container.hpp
+++ b/src/DI/container.hpp
@@ -1,11 +1,12 @@
#pragma once
#include "DI/interfaces/wrapper.hpp"
+
+#include "DI/concepts.hpp"
#include "DI/object_type.hpp"
-#include "DI/type_traits.hpp"
+#include <concepts>
#include <memory>
-#include <type_traits>
#include <unordered_map>
class Container;
@@ -16,8 +17,8 @@ class BindingBuilder
public:
explicit BindingBuilder(Container *container) noexcept;
- template <class ObjectImpl,
- class = std::enable_if_t<std::is_base_of_v<Interface, ObjectImpl>>>
+ template <typename Impl>
+ requires std::derived_from<Impl, Interface>
void to() noexcept;
void to_factory(Interface func) noexcept;
@@ -31,17 +32,24 @@ class Container
public:
Container() noexcept = default;
+ template <typename Type>
+ using Ptr = std::shared_ptr<Type>;
+
template <class Interface>
auto bind() noexcept -> BindingBuilder<Interface>;
- template <class Interface, class = std::enable_if_t<std::is_abstract_v<Interface>>>
- auto get() const noexcept -> std::shared_ptr<Interface>;
+ template <class Interface>
+ requires Abstract<Interface>
+ auto get() const noexcept -> Ptr<Interface>;
- template <typename Interface, typename = std::enable_if_t<is_func_v<Interface>>>
+ template <typename Interface>
+ requires Function<Interface>
auto get() const noexcept -> Interface;
- std::unordered_map<BaseObjectType, std::shared_ptr<IGenericWrapper>, ObjectTypeHasher>
- bindings;
+ void add(BaseObjectType type, const Ptr<IGenericWrapper> &wrapper) noexcept;
+
+private:
+ std::unordered_map<BaseObjectType, Ptr<IGenericWrapper>, ObjectTypeHasher> _bindings;
};
#include "container.tpp"