From 5864e5abc43b201c3801fa39a2fcaf9e3a9e8914 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 27 Feb 2022 12:54:10 +0100 Subject: refactor: use dependency injection --- src/DI/container.tpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/DI/container.tpp (limited to 'src/DI/container.tpp') diff --git a/src/DI/container.tpp b/src/DI/container.tpp new file mode 100644 index 0000000..21bf81a --- /dev/null +++ b/src/DI/container.tpp @@ -0,0 +1,53 @@ +#pragma once + +#include "container.hpp" + +#include "function_wrapper.hpp" +#include "object_wrapper.hpp" + +template +BindingBuilder::BindingBuilder(Container *container) : _container(container) +{ +} + +template +template +void BindingBuilder::to() +{ + _container->bindings.emplace( + ObjectType(), + std::dynamic_pointer_cast( + std::make_shared>(*_container))); +} + +template +void BindingBuilder::to_factory(Interface func) +{ + _container->bindings.emplace(ObjectType(), + std::dynamic_pointer_cast( + std::make_shared>(func))); +} + +template +BindingBuilder Container::bind() +{ + return BindingBuilder(this); +} + +template +std::shared_ptr Container::get() const +{ + auto wrapper = std::dynamic_pointer_cast>>( + bindings.at(ObjectType())); + + return wrapper->get(); +} + +template +Interface Container::get() const +{ + auto wrapper = std::dynamic_pointer_cast>( + bindings.at(ObjectType())); + + return wrapper->get(); +} -- cgit v1.2.3-18-g5258