Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
module.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <helios/core_pch.hpp>
4
5#include <ctti/name.hpp>
6#include <ctti/type_id.hpp>
7
8#include <concepts>
9#include <cstddef>
10#include <string_view>
11
12namespace helios::app {
13
14class App;
15
16/**
17 * @brief Base class for all modules.
18 * @details Derived classes must implement:
19 * - `Build(helios::app::App&)` for initialization
20 * - `Destroy(helios::app::App&)` for cleanup (optional)
21 * - `IsReady(const helios::app::App&)` to check if ready (optional, default: true)
22 * - `Finish(helios::app::App&)` for finalization after all modules are ready (optional)
23 * - `static constexpr std::string_view GetName() noexcept` (optional)
24 */
25class Module {
26public:
27 virtual ~Module() = default;
28
29 /**
30 * @brief Builds the module.
31 * @details Called during application initialization to set up the module.
32 * This is where you should register systems, resources, and events.
33 * @param app Application for initialization
34 */
35 virtual void Build(App& app) = 0;
36
37 /**
38 * @brief Finishes adding this module to the App, once all modules are ready.
39 * @details This can be useful for modules that depend on another module's asynchronous
40 * setup, like the renderer. Called after all modules' `Build` methods have been called
41 * and all modules return true from `IsReady`.
42 * @param app The application instance
43 */
44 virtual void Finish(App& /*app*/) {}
45
46 /**
47 * @brief Destroys the module and cleans up resources.
48 * @details Called during application shutdown.
49 * @param app The application instance
50 */
51 virtual void Destroy(App& /*app*/) {}
52
53 /**
54 * @brief Checks if the module is ready for finalization.
55 * @details This can be useful for modules that need something asynchronous to happen
56 * before they can finish their setup, like the initialization of a renderer.
57 * Once the module is ready, `Finish` will be called.
58 * @param app The application instance (const)
59 * @return True if the module is ready, false otherwise
60 */
61 [[nodiscard]] virtual bool IsReady(const App& /*app*/) const noexcept { return true; }
62};
63
64/**
65 * @brief Concept to ensure a type is a valid Module.
66 * @details A valid Module must derive from Module.
67 */
68template <typename T>
69concept ModuleTrait = std::derived_from<T, Module>;
70
71/**
72 * @brief Concept for modules that can be default constructed.
73 * @details Used by AddModule<T>() overload that creates the module internally.
74 */
75template <typename T>
76concept DefaultConstructibleModuleTrait = ModuleTrait<T> && std::default_initializable<T>;
77
78/**
79 * @brief Concept to check if a Module provides a GetName() method.
80 * @details A Module with name trait must satisfy ModuleTrait and provide:
81 * - `static constexpr std::string_view GetName() noexcept`
82 */
83template <typename T>
84concept ModuleWithNameTrait = ModuleTrait<T> && requires {
85 { T::GetName() } -> std::same_as<std::string_view>;
86};
87
88using ModuleTypeId = size_t;
89
90template <typename T>
91constexpr ModuleTypeId ModuleTypeIdOf() noexcept {
92 return ctti::type_index_of<T>().hash();
93}
94
95/**
96 * @brief Gets the name of a module.
97 * @details Returns provided name or type name as fallback.
98 * @tparam T Module type
99 * @return Module name
100 */
101template <ModuleTrait T>
102[[nodiscard]] constexpr std::string_view ModuleNameOf() noexcept {
103 if constexpr (ModuleWithNameTrait<T>) {
104 return T::GetName();
105 } else {
106 return ctti::name_of<T>();
107 }
108}
109
110} // namespace helios::app
Application class.
Definition app.hpp:97
Base class for all modules.
Definition module.hpp:25
virtual void Finish(App &)
Finishes adding this module to the App, once all modules are ready.
Definition module.hpp:44
virtual bool IsReady(const App &) const noexcept
Checks if the module is ready for finalization.
Definition module.hpp:61
virtual ~Module()=default
virtual void Build(App &app)=0
Builds the module.
virtual void Destroy(App &)
Destroys the module and cleans up resources.
Definition module.hpp:51
Concept for modules that can be default constructed.
Definition module.hpp:76
Concept to ensure a type is a valid Module.
Definition module.hpp:69
Concept to check if a Module provides a GetName() method.
Definition module.hpp:84
size_t ModuleTypeId
Definition module.hpp:88
constexpr ModuleTypeId ModuleTypeIdOf() noexcept
Definition module.hpp:91
constexpr std::string_view ModuleNameOf() noexcept
Gets the name of a module.
Definition module.hpp:102