#pragma once #include "container.hpp" #include "function_wrapper.hpp" #include "object_wrapper.hpp" #include template BindingBuilder::BindingBuilder(Container *container) noexcept : _container(container) { } template template void BindingBuilder::to() noexcept { _container->bindings.emplace( ObjectType(), std::dynamic_pointer_cast( std::make_shared>(*_container))); } template void BindingBuilder::to_factory(Interface func) noexcept { _container->bindings.emplace(ObjectType(), std::dynamic_pointer_cast( std::make_shared>(func))); } template auto Container::bind() noexcept -> BindingBuilder { return BindingBuilder(this); } template auto Container::get() const noexcept -> std::shared_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 auto Container::get() const noexcept -> Interface { auto wrapper = std::dynamic_pointer_cast>( bindings.at(ObjectType())); return wrapper->get(); }