aboutsummaryrefslogtreecommitdiff
path: root/src/DI/container.tpp
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.tpp
parenta8fbce594f87554b48da661c7939559be801da13 (diff)
refactor: make container use concepts
Diffstat (limited to 'src/DI/container.tpp')
-rw-r--r--src/DI/container.tpp46
1 files changed, 27 insertions, 19 deletions
diff --git a/src/DI/container.tpp b/src/DI/container.tpp
index 2573223..54b09c6 100644
--- a/src/DI/container.tpp
+++ b/src/DI/container.tpp
@@ -7,42 +7,49 @@
#include <iostream>
-template <class Interface>
+template <typename Interface>
BindingBuilder<Interface>::BindingBuilder(Container *container) noexcept
: _container(container)
{
}
-template <class Interface>
-template <class ObjectImpl, class>
+template <typename Interface>
+template <typename Impl>
+requires std::derived_from<Impl, Interface>
void BindingBuilder<Interface>::to() noexcept
{
- _container->bindings.emplace(
- ObjectType<Interface>(),
- std::dynamic_pointer_cast<IGenericWrapper>(
- std::make_shared<ObjectWrapper<Interface, ObjectImpl>>(*_container)));
+ using Wrapper = ObjectWrapper<Interface, Impl>;
+
+ auto wrapper = Container::Ptr<Wrapper>(new Wrapper(*_container));
+
+ _container->add(ObjectType<Interface>(),
+ std::dynamic_pointer_cast<IGenericWrapper>(wrapper));
}
-template <class Interface>
+template <typename Interface>
void BindingBuilder<Interface>::to_factory(Interface func) noexcept
{
- _container->bindings.emplace(ObjectType<Interface>(),
- std::dynamic_pointer_cast<IGenericWrapper>(
- std::make_shared<FunctionWrapper<Interface>>(func)));
+ using Wrapper = FunctionWrapper<Interface>;
+
+ auto wrapper = Container::Ptr<Wrapper>(new Wrapper(func));
+
+ _container->add(ObjectType<Interface>(),
+ std::dynamic_pointer_cast<IGenericWrapper>(wrapper));
}
-template <class Interface>
+template <typename Interface>
auto Container::bind() noexcept -> BindingBuilder<Interface>
{
return BindingBuilder<Interface>(this);
}
-template <class Interface, class>
-auto Container::get() const noexcept -> std::shared_ptr<Interface>
+template <typename Interface>
+requires Abstract<Interface>
+auto Container::get() const noexcept -> Ptr<Interface>
{
ObjectType<Interface> interface_type;
- if (bindings.count(interface_type) == 0)
+ if (_bindings.count(interface_type) == 0)
{
std::cerr
<< "Error: Tried to get a item from the container using unbound interface '"
@@ -50,17 +57,18 @@ auto Container::get() const noexcept -> std::shared_ptr<Interface>
exit(EXIT_FAILURE);
}
- auto wrapper = std::dynamic_pointer_cast<IWrapper<std::shared_ptr<Interface>>>(
- bindings.at(interface_type));
+ auto wrapper =
+ std::dynamic_pointer_cast<IWrapper<Ptr<Interface>>>(_bindings.at(interface_type));
return wrapper->get();
}
-template <typename Interface, typename>
+template <typename Interface>
+requires Function<Interface>
auto Container::get() const noexcept -> Interface
{
auto wrapper = std::dynamic_pointer_cast<IWrapper<Interface>>(
- bindings.at(ObjectType<Interface>()));
+ _bindings.at(ObjectType<Interface>()));
return wrapper->get();
}