#pragma once #include "container.hpp" #include "function_wrapper.hpp" #include "object_wrapper.hpp" #include template BindingBuilder::BindingBuilder(Container *container) noexcept : _container(container) { } template template requires std::derived_from void BindingBuilder::to() noexcept { using Wrapper = ObjectWrapper; auto wrapper = Container::Ptr(new Wrapper(*_container)); _container->add(ObjectType(), std::dynamic_pointer_cast(wrapper)); } template void BindingBuilder::to_factory(Interface func) noexcept { using Wrapper = FunctionWrapper; auto wrapper = Container::Ptr(new Wrapper(func)); _container->add(ObjectType(), std::dynamic_pointer_cast(wrapper)); } template auto Container::bind() noexcept -> BindingBuilder { return BindingBuilder(this); } template requires Abstract auto Container::get() const noexcept -> Ptr { ObjectType interface_type; if (_bindings.count(interface_type) == 0) { std::cerr << "Error: Tried to get a item from the container using unbound interface '" << interface_type.name() << "'" << std::endl; exit(EXIT_FAILURE); } auto wrapper = std::dynamic_pointer_cast>>(_bindings.at(interface_type)); return wrapper->get(); } template requires Function auto Container::get() const noexcept -> Interface { auto wrapper = std::dynamic_pointer_cast>( _bindings.at(ObjectType())); return wrapper->get(); }