Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
plugin.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <concepts>
6#include <string_view>
7#include <type_traits>
8
9namespace helios::app {
10
11class App;
12
13/// @brief Base class for all plugins.
14class Plugin {
15public:
16 virtual ~Plugin() = default;
17
18 /**
19 * @brief Builds the plugin.
20 * @details Called during application initialization to set up the plugin.
21 * This is where you should register systems, plugins, and events.
22 * @param app Application for initialization
23 */
24 virtual void Build(App& app) = 0;
25
26 /**
27 * @brief Finishes adding this plugin to the App, once all plugins are ready.
28 * @details This can be useful for plugins that depend on another plugin's
29 * asynchronous setup, like the renderer. Called after all plugins' `Build`
30 * methods have been called and all plugins return true from `IsReady`.
31 * @param app The application instance
32 */
33 virtual void Finish(App& /*app*/) {}
34
35 /**
36 * @brief Destroys the plugin and cleans up plugins.
37 * @details Called during application shutdown.
38 * @param app The application instance
39 */
40 virtual void Destroy(App& /*app*/) {}
41
42 /**
43 * @brief Advances asynchronous setup while the app waits for readiness.
44 * @details Called from `App::WaitForPluginsReady` between readiness checks.
45 * Override to pump GPU/window/network init on the main thread or submit
46 * follow-up work to the app executor.
47 * @param app The application instance
48 */
49 virtual void Poll(App& /*app*/) {}
50
51 /**
52 * @brief Checks if the plugin is ready for finalization.
53 * @details This can be useful for plugins that need something asynchronous to
54 * happen before they can finish their setup, like the initialization of a
55 * renderer. Once the plugin is ready, `Finish` will be called.
56 * @param app The application instance (const)
57 * @return True if the plugin is ready, false otherwise
58 */
59 [[nodiscard]] virtual bool IsReady(const App& /*app*/) const noexcept {
60 return true;
61 }
62};
63
64/// @brief Type index for plugins.
66
67/// @brief Type id for plugins.
69
70/**
71 * @brief Concept to ensure a type is a valid Plugin.
72 * @details A valid Plugin must derive from `Plugin` and must not be abstract
73 * (i.e., it must implement all pure virtual methods).
74 */
75template <typename T>
76concept PluginTrait =
77 std::derived_from<T, Plugin> && !std::is_abstract_v<std::remove_cvref_t<T>>;
78
79/**
80 * @brief Concept for plugins that provide a name.
81 * @details A plugin with name trait must satisfy `PluginTrait` and provide:
82 * - `static constexpr std::string_view kName` variable
83 */
84template <typename T>
85concept PluginWithNameTrait = PluginTrait<T> && requires {
86 { std::remove_cvref_t<T>::kName } -> std::convertible_to<std::string_view>;
87};
88
89/**
90 * @brief Gets name for a plugin type.
91 * @tparam T Plugin type
92 * @return Name of the plugin
93 */
94template <PluginTrait T>
95[[nodiscard]] constexpr std::string_view PluginNameOf() noexcept {
96 if constexpr (PluginWithNameTrait<T>) {
97 return T::kName;
98 } else {
100 }
101}
102
103/**
104 * @brief Gets name for a plugin type.
105 * @tparam T Plugin type
106 * @param plugin Plugin instance
107 * @return Name of the plugin
108 */
109template <PluginTrait T>
110[[nodiscard]] constexpr std::string_view PluginNameOf(
111 const T& /*plugin*/) noexcept {
113}
114
115} // namespace helios::app
Application class.
Base class for all plugins.
Definition plugin.hpp:14
virtual ~Plugin()=default
virtual void Poll(App &)
Advances asynchronous setup while the app waits for readiness.
Definition plugin.hpp:49
virtual bool IsReady(const App &) const noexcept
Checks if the plugin is ready for finalization.
Definition plugin.hpp:59
virtual void Build(App &app)=0
Builds the plugin.
virtual void Finish(App &)
Finishes adding this plugin to the App, once all plugins are ready.
Definition plugin.hpp:33
virtual void Destroy(App &)
Destroys the plugin and cleans up plugins.
Definition plugin.hpp:40
Concept to ensure a type is a valid Plugin.
Definition plugin.hpp:76
Concept for plugins that provide a name.
Definition plugin.hpp:85
utils::TypeIndex PluginTypeIndex
Type index for plugins.
Definition plugin.hpp:65
constexpr std::string_view PluginNameOf() noexcept
Gets name for a plugin type.
Definition plugin.hpp:95
utils::TypeId PluginTypeId
Type id for plugins.
Definition plugin.hpp:68
constexpr std::string_view QualifiedTypeNameOf() noexcept
Retrieves the fully qualified type name of T.