Skip to main content

Application.ixx File

Entry point and central manager for helios applications. More...

Included Headers

#include <memory> #include <vector> #include <list> #include <stdexcept> #include <format> #include <algorithm> #include <cassert> #include <helios.util.log.LogManager> #include <helios.window.WindowConfig> #include <helios.window.Window> #include <helios.event.EventManager> #include <helios.input.InputManager> #include <helios.rendering.RenderTarget> #include <helios.util.log.Logger> #include <helios.rendering.RenderingDevice> #include <helios.app.controller.Controller>

Namespaces Index

namespacehelios
namespaceapp

Application infrastructure and lifecycle management. More...

Classes Index

classApplication

Class providing the entry point for a helios application. More...

Macro Definitions Index

#defineHELIOS_LOG_SCOPE   "helios::app::Application"

Description

Entry point and central manager for helios applications.

Macro Definitions

HELIOS_LOG_SCOPE

#define HELIOS_LOG_SCOPE   "helios::app::Application"

Definition at line 27 of file Application.ixx.

27#define HELIOS_LOG_SCOPE "helios::app::Application"

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file Application.ixx
3 * @brief Entry point and central manager for helios applications.
4 */
5module;
6
7#include <memory>
8#include <vector>
9#include <list>
10#include <stdexcept>
11#include <format>
12#include <algorithm>
13#include <cassert>
14
15export module helios.app.Application;
16
17import helios.app.controller.Controller;
18import helios.rendering.RenderingDevice;
19import helios.rendering.RenderTarget;
20import helios.input.InputManager;
21import helios.event.EventManager;
22import helios.window.Window;
23import helios.window.WindowConfig;
24import helios.util.log.Logger;
25import helios.util.log.LogManager;
26
27#define HELIOS_LOG_SCOPE "helios::app::Application"
28export namespace helios::app {
29
30 /**
31 * @brief Class providing the entry point for a helios application.
32 *
33 * `Application` serves as the central hub for the helios framework, offering convenient access
34 * and management for key subsystems such as the EventManager, the InputManager and the
35 * RenderingDevice.
36 *
37 * It also allows for the registration of `helios.app.controller.Controller`s,
38 * which can provide specialized logic and management for the various subsystems and related events.
39 */
40 class Application {
41
42 protected:
43 /**
44 * @brief Shared logger instance for all Application objects.
45 */
47
48 /**
49 * @brief Flag indicating whether the application has been initialized.
50 */
51 bool initialized_ = false;
52
53 /**
54 * @brief The rendering device owned by this application.
55 */
56 std::unique_ptr<helios::rendering::RenderingDevice> renderingDevice_;
57
58 /**
59 * @brief The input manager owned by this application.
60 */
61 std::unique_ptr<input::InputManager> inputManager_;
62
63 /**
64 * @brief The event manager owned by this application.
65 */
66 std::unique_ptr<event::EventManager> eventManager_;
67
68 /**
69 * @brief A list containing all windows managed by this application.
70 */
71 std::list<std::unique_ptr<helios::window::Window>> windowList_;
72
73 /**
74 * @brief Collection of controllers registered with this application.
75 */
76 std::vector<std::unique_ptr<helios::app::controller::Controller>> controllers_;
77
78 public:
79
80 /**
81 * @todo free resource allocations from renderingDevice,
82 * window and InputManager
83 */
84 virtual ~Application() = default;
85
86
87 /**
88 * @brief Creates a new application instance.
89 *
90 * The application will take ownership of the specified constructor arguments.
91 *
92 * @param renderingDevice Unique rendering device owned by the application.
93 *
94 * @param inputManager Unique input manager instance.
95 *
96 * @param eventManager Unique event manager instance.
97 */
98 explicit Application(
99 std::unique_ptr<helios::rendering::RenderingDevice> renderingDevice,
100 std::unique_ptr<helios::input::InputManager> inputManager,
101 std::unique_ptr<helios::event::EventManager> eventManager
102 ):
104 inputManager_(std::move(inputManager)),
105 eventManager_(std::move(eventManager))
106 {
107 assert(renderingDevice_ && "Unexpected nullptr for RenderingDevice");
108 assert(inputManager_ && "Unexpected nullptr for InputManager");
109 assert(eventManager_ && "Unexpected nullptr for EventManager");
110 }
111
112
113 /**
114 * @brief Registers a new controller with this application.
115 *
116 * Makes sure that the controller subscribes to the event manager's dispatcher
117 * of this application. The application receives ownership of the specified controller.
118 *
119 * @param controller Unique pointer to the controller to add.
120 */
121 void addController(std::unique_ptr<helios::app::controller::Controller> controller) noexcept {
122 if (initialized_) {
123 logger_.info(
124 std::format("Controller {0} added to an already initialized Application, explicitly initializing.", controller->toString())
125 );
126 if (controller->init()) {
127 controller->subscribeTo(eventManager_->dispatcher());
128 }
129 }
130 controllers_.push_back(std::move(controller));
131 }
132
133 /**
134 * @brief Creates the container for the native window and performs all
135 * necessary steps to properly initialize it.
136 *
137 * @param renderTarget Render target that wraps the framebuffer used by the window.
138 *
139 * @param cfg Window configuration defining properties such as title, dimensions, and hints.
140 *
141 * @return Reference to the created window instance.
142 *
143 * @throws std::invalid_argument If the configuration was invalid.
144 */
145 [[nodiscard]] virtual helios::window::Window& createWindow(std::unique_ptr<helios::rendering::RenderTarget> renderTarget, const helios::window::WindowConfig& cfg) = 0;
146
147 /**
148 * @brief Initializes the application.
149 *
150 * Performs any bootstrapping necessary for this application and provides an idiomatic
151 * entry point for APIs using this application. This method ensures that all controllers
152 * are initialized and subscribe to this application's event manager dispatcher.
153 *
154 * @throws std::runtime_error If the application was already initialized.
155 */
156 virtual void init() {
157 logger_.info("Initializing application.");
158
159 if (initialized_) {
160 std::string msg = "Application was already initialized.";
161 logger_.error(msg);
162 throw std::runtime_error(msg);
163 }
164 for (auto& ctrl: controllers_) {
165 if (ctrl->init()) {
166 ctrl->subscribeTo(eventManager_->dispatcher());
167 }
168 }
169
170 initialized_ = true;
171 }
172
173
174 /**
175 * @brief Sets the application's active window.
176 *
177 * Advises the input manager to poll this window's events.
178 *
179 * @param win Reference to the window that becomes the current active window.
180 */
181 virtual void setCurrent(helios::window::Window& win) = 0;
182
183
184 /**
185 * @brief Returns true if the specified window is owned by this application,
186 * otherwise false.
187 *
188 * @param win The window to look up in this application's window list.
189 *
190 * @return True if the window is owned by this application, otherwise false.
191 */
192 [[nodiscard]] bool hasWindow(const helios::window::Window& win) const noexcept {
193 auto it = std::find_if(
194 windowList_.begin(),
195 windowList_.end(),
196 [&win](const auto& window){return *window == win;});
197
198 return it != windowList_.end();
199 }
200
201
202 /**
203 * @brief Returns the currently focused window, or nullptr
204 * if none exists or no window is being treated as current.
205 *
206 * @return Pointer to the current window, or nullptr if none is active.
207 */
208 [[nodiscard]] virtual helios::window::Window* current() const noexcept = 0;
209
210
211 /**
212 * @brief Returns the input manager owned by this application.
213 *
214 * @return Reference to the input manager instance.
215 */
216 [[nodiscard]] helios::input::InputManager& inputManager() const {
217 return *inputManager_;
218 };
219
220 /**
221 * @brief Returns the rendering device owned by this application.
222 *
223 * @return Reference to the rendering device instance.
224 */
226 return *renderingDevice_;
227 };
228
229 /**
230 * @brief Returns the event manager owned by this application.
231 *
232 * @return Reference to the event manager instance.
233 */
235 return *eventManager_;
236 }
237
238
239 };
240
241}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.