Brenta Engine 1.2
Loading...
Searching...
No Matches
engine.hpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#pragma once
7
8#include <brenta/subsystem.hpp>
9
10#include <tenno/vector.hpp>
11#include <tenno/functional.hpp>
12
13namespace brenta
14{
15
16//
17// Engine class
18// ------------
19//
20// This class is used to initialize and terminate multiple
21// subsystems. You can use the builder class to initialize the engine,
22// as with any other subsystem.
23//
24// Note: The subsystems will be initialized in the order they were
25// added, and terminated in reverse order. Make sure that they are
26// ordered correctly if a subsystem depends on another one.
27//
28class Engine : public Subsystem
29{
30public:
31
32 class Manager;
33 class Builder;
34
35 // Subsystem interface
36 static const std::string subsystem_name;
37 std::expected<void, Subsystem::Error> initialize() override;
38 std::expected<void, Subsystem::Error> terminate() override;
39 std::string name() override;
40 bool is_initialized() override;
41
42 Engine() = default;
43 ~Engine() = default;
44
45 // Member functions
46
47 // Get a static object instance
48 static Engine &instance();
49 static Engine::Manager managed();
50
51 // Initialize a subsystem and add it to the managed subsystems (will
52 // be terminated with the others).
53 static std::expected<void, std::string>
54 with(Subsystem::Builder &&builder);
55
56private:
57
58 static tenno::vector<tenno::reference_wrapper<Subsystem>> subsystems;
59 static bool initialized;
60
61};
62
63// Automatically initialize and terminate engine with RAII
65{
66public:
67 // Initializes all subsystems, throws and exeption in case of failure
68 Manager();
69 // Terminates all subsystems
70 ~Manager();
71};
72
74{
75public:
76
77 Builder() = default;
78 ~Builder() = default;
79
80 Builder &with(Subsystem::Builder &builder);
81 Builder &with(Subsystem::Builder &&builder);
82
83 brenta::Subsystem &build() override;
84
85private:
86
87 tenno::vector<tenno::reference_wrapper<brenta::Subsystem>> subsystems;
88
89};
90
91} // namespace brenta