diff options
author | HampusM <hampus@hampusmat.com> | 2022-04-10 17:20:49 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:58 +0200 |
commit | b36d072ad7a7b9c6e30fcb25d6bbb001a8393468 (patch) | |
tree | 7749fc877652bb2cf7bbd2b7c1fed5e3abe397fc /src/bootstrap.cpp | |
parent | b828d4799a84beb1afbd889e5c4cb939a8b3df78 (diff) |
refactor: add factory class & make DI container return unique ptrs
Diffstat (limited to 'src/bootstrap.cpp')
-rw-r--r-- | src/bootstrap.cpp | 50 |
1 files changed, 17 insertions, 33 deletions
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 <concepts> #include <memory> #include <random> #include <string_view> -template <typename Interface, typename Impl, typename... Params> -requires Abstract<Interface> && std::derived_from<Impl, Interface> && - std::constructible_from<Impl, Params...> -auto construct_as_interface(Params &&...parameters) -> std::shared_ptr<Interface> -{ - return std::dynamic_pointer_cast<Interface>(std::make_shared<Impl>(parameters...)); -} - auto bootstrap() noexcept -> Container { auto container = Container(); @@ -58,69 +48,63 @@ auto bootstrap() noexcept -> Container container.bind<IWindow>().to<Window>(); container.bind<ISeedGenerator>().to<SeedGenerator>(); - container.bind<IGameFactory>().to_factory(normalize_lambda( + container.bind<IGameFactory>().to_factory( [&container](const std::shared_ptr<IWindow> &window, const std::shared_ptr<IScene> &scene, const std::shared_ptr<ICursorController> &cursor_controller) { - auto statusline = + std::shared_ptr<IStatusLine> statusline = container.get<IStatusLineFactory>()(cursor_controller, window); - auto generation_tracker = container.get<IGenerationTrackerFactory>()(true); + std::shared_ptr<IGenerationTracker> generation_tracker = + container.get<IGenerationTrackerFactory>()(true); - auto status_updater = + std::shared_ptr<IStatusUpdater> status_updater = container.get<IStatusUpdaterFactory>()(statusline, generation_tracker); - return construct_as_interface<IGame, Game>(window, scene, cursor_controller, - statusline, generation_tracker, - status_updater); - })); + return std::make_unique<Game>(window, scene, cursor_controller, statusline, + generation_tracker, status_updater); + }); container.bind<IRandomNumberGeneratorFactory>().to_factory( [](const uint32_t &seed) { - return construct_as_interface<IRandomNumberGenerator, RandomNumberGenerator>( - seed); + return std::make_unique<RandomNumberGenerator>(seed); }); container.bind<IMatrixFactory<std::string_view>>().to_factory( [](const Bounds &bounds) { - return construct_as_interface<IMatrix<std::string_view>, - Matrix<std::string_view>>(bounds); + return std::make_unique<Matrix<std::string_view>>(bounds); }); - container.bind<ISceneFactory>().to_factory(normalize_lambda( + container.bind<ISceneFactory>().to_factory( [&container](const std::shared_ptr<ICursorController> &cursor_controller, const std::shared_ptr<IWindow> &window) { auto matrix_factory = container.get<IMatrixFactory<std::string_view>>(); - return construct_as_interface<IScene, Scene>(matrix_factory, - cursor_controller, window); - })); + return std::make_unique<Scene>(matrix_factory, cursor_controller, window); + }); container.bind<IStatusLineFactory>().to_factory( [](const std::shared_ptr<ICursorController> &cursor_controller, const std::shared_ptr<IWindow> &window) { - return construct_as_interface<IStatusLine, StatusLine>(cursor_controller, - window); + return std::make_unique<StatusLine>(cursor_controller, window); }); container.bind<IStatusUpdaterFactory>().to_factory( [](const std::shared_ptr<IStatusLine> &statusline, const std::shared_ptr<IGenerationTracker> &generation_tracker) { - return construct_as_interface<IStatusUpdater, StatusUpdater>( - statusline, generation_tracker); + return std::make_unique<StatusUpdater>(statusline, generation_tracker); }); container.bind<IGenerationTrackerFactory>().to_factory( [](bool is_paused) { - return construct_as_interface<IGenerationTracker, GenerationTracker>( - is_paused); + return std::make_unique<GenerationTracker>(is_paused); }); return container; |