From b36d072ad7a7b9c6e30fcb25d6bbb001a8393468 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 10 Apr 2022 17:20:49 +0200 Subject: refactor: add factory class & make DI container return unique ptrs --- src/bootstrap.cpp | 50 +++++++++++++++++--------------------------------- 1 file changed, 17 insertions(+), 33 deletions(-) (limited to 'src/bootstrap.cpp') diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index 276b73a..22fe472 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -31,22 +31,12 @@ #include "randomization/generator.hpp" #include "randomization/seed_generator.hpp" -#include "DI/concepts.hpp" -#include "util/function.hpp" +#include "DI/factory.hpp" -#include #include #include #include -template -requires Abstract && std::derived_from && - std::constructible_from -auto construct_as_interface(Params &&...parameters) -> std::shared_ptr -{ - return std::dynamic_pointer_cast(std::make_shared(parameters...)); -} - auto bootstrap() noexcept -> Container { auto container = Container(); @@ -58,69 +48,63 @@ auto bootstrap() noexcept -> Container container.bind().to(); container.bind().to(); - container.bind().to_factory(normalize_lambda( + container.bind().to_factory( [&container](const std::shared_ptr &window, const std::shared_ptr &scene, const std::shared_ptr &cursor_controller) { - auto statusline = + std::shared_ptr statusline = container.get()(cursor_controller, window); - auto generation_tracker = container.get()(true); + std::shared_ptr generation_tracker = + container.get()(true); - auto status_updater = + std::shared_ptr status_updater = container.get()(statusline, generation_tracker); - return construct_as_interface(window, scene, cursor_controller, - statusline, generation_tracker, - status_updater); - })); + return std::make_unique(window, scene, cursor_controller, statusline, + generation_tracker, status_updater); + }); container.bind().to_factory( [](const uint32_t &seed) { - return construct_as_interface( - seed); + return std::make_unique(seed); }); container.bind>().to_factory( [](const Bounds &bounds) { - return construct_as_interface, - Matrix>(bounds); + return std::make_unique>(bounds); }); - container.bind().to_factory(normalize_lambda( + container.bind().to_factory( [&container](const std::shared_ptr &cursor_controller, const std::shared_ptr &window) { auto matrix_factory = container.get>(); - return construct_as_interface(matrix_factory, - cursor_controller, window); - })); + return std::make_unique(matrix_factory, cursor_controller, window); + }); container.bind().to_factory( [](const std::shared_ptr &cursor_controller, const std::shared_ptr &window) { - return construct_as_interface(cursor_controller, - window); + return std::make_unique(cursor_controller, window); }); container.bind().to_factory( [](const std::shared_ptr &statusline, const std::shared_ptr &generation_tracker) { - return construct_as_interface( - statusline, generation_tracker); + return std::make_unique(statusline, generation_tracker); }); container.bind().to_factory( [](bool is_paused) { - return construct_as_interface( - is_paused); + return std::make_unique(is_paused); }); return container; -- cgit v1.2.3-18-g5258