aboutsummaryrefslogtreecommitdiff
path: root/src/bootstrap.cpp
blob: 276b73a545012bec2659e0f875870263d88137c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "bootstrap.hpp"

// Interfaces
#include "interfaces/argument_parser.hpp"
#include "interfaces/cursor.hpp"
#include "interfaces/engine.hpp"
#include "interfaces/game.hpp"
#include "interfaces/generation_tracker.hpp"
#include "interfaces/input.hpp"
#include "interfaces/matrix.hpp"
#include "interfaces/randomization.hpp"
#include "interfaces/scene.hpp"
#include "interfaces/status_updater.hpp"
#include "interfaces/statusline.hpp"
#include "interfaces/window.hpp"

// Implementations
#include "argument_parser.hpp"
#include "engine/data/bounds.hpp"
#include "engine/data/vector2.hpp"
#include "engine/engine.hpp"
#include "engine/graphics/matrix.hpp"
#include "engine/graphics/scene.hpp"
#include "engine/graphics/window.hpp"
#include "engine/user/cursor.hpp"
#include "engine/user/input.hpp"
#include "game/game.hpp"
#include "game/generation_tracker.hpp"
#include "game/status_updater.hpp"
#include "game/statusline.hpp"
#include "randomization/generator.hpp"
#include "randomization/seed_generator.hpp"

#include "DI/concepts.hpp"
#include "util/function.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();

	container.bind<IArgumentParser>().to<ArgumentParser>();
	container.bind<IInputHandler>().to<InputHandler>();
	container.bind<ICursorController>().to<CursorController>();
	container.bind<ICLIGameEngine>().to<CLIGameEngine>();
	container.bind<IWindow>().to<Window>();
	container.bind<ISeedGenerator>().to<SeedGenerator>();

	container.bind<IGameFactory>().to_factory(normalize_lambda(
		[&container](const std::shared_ptr<IWindow> &window,
					 const std::shared_ptr<IScene> &scene,
					 const std::shared_ptr<ICursorController> &cursor_controller)
		{
			auto statusline =
				container.get<IStatusLineFactory>()(cursor_controller, window);

			auto generation_tracker = container.get<IGenerationTrackerFactory>()(true);

			auto status_updater =
				container.get<IStatusUpdaterFactory>()(statusline, generation_tracker);

			return construct_as_interface<IGame, 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);
		});

	container.bind<IMatrixFactory<std::string_view>>().to_factory(
		[](const Bounds &bounds)
		{
			return construct_as_interface<IMatrix<std::string_view>,
										  Matrix<std::string_view>>(bounds);
		});

	container.bind<ISceneFactory>().to_factory(normalize_lambda(
		[&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);
		}));

	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);
		});

	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);
		});

	container.bind<IGenerationTrackerFactory>().to_factory(
		[](bool is_paused)
		{
			return construct_as_interface<IGenerationTracker, GenerationTracker>(
				is_paused);
		});

	return container;
}