1#ifndef LIBSBX_CORE_MODULE_HPP_
2#define LIBSBX_CORE_MODULE_HPP_
5#include <unordered_set>
6#include <unordered_map>
14#include <libsbx/utility/noncopyable.hpp>
15#include <libsbx/utility/type_id.hpp>
30template<
typename Type>
33template<
typename Derived,
typename Base>
34concept derived_from = std::is_base_of_v<Base, Derived>;
51 enum class stage : std::uint8_t {
61 template<
typename... Types>
63 auto get()
const noexcept -> std::unordered_set<std::uint32_t> {
64 auto types = std::unordered_set<std::uint32_t>{};
71 virtual ~module_base() =
default;
72 virtual auto update() ->
void = 0;
75 struct module_factory {
76 module_manager::stage stage{};
77 std::unordered_set<std::uint32_t> dependencies{};
78 std::function<module_base*()> create{};
79 std::function<void(module_base*)> destroy{};
82 static auto _factories() -> std::vector<std::optional<module_factory>>& {
83 static auto instance = std::vector<std::optional<module_factory>>{};
89template<
typename Derived>
95 static_assert(!std::is_abstract_v<Derived>,
"Class may not be abstract.");
96 static_assert(std::is_base_of_v<module<Derived>, Derived>,
"Class must inherit from module<Class>.");
101 using base_type = module_manager::module_base;
103 template<
typename... Dependencies>
104 using dependencies = module_manager::dependencies<Dependencies...>;
106 using stage = module_manager::stage;
108 template<derived_from<base_type>... Dependencies>
109 static auto register_module(stage stage, dependencies<Dependencies...>&& dependencies = {}) ->
bool {
112 auto& factories = module_manager::_factories();
114 factories.resize(std::max(factories.size(),
static_cast<std::size_t
>(type + 1u)));
116 factories[type] = module_manager::module_factory{
118 .dependencies = dependencies.get(),
120 auto* instance =
reinterpret_cast<Derived*
>(std::malloc(
sizeof(Derived)));
123 throw std::bad_alloc{};
126 std::construct_at(instance);
130 .destroy = [](module_base* instance){
131 std::destroy_at(
reinterpret_cast<Derived*
>(instance));
Definition: engine.hpp:34
Definition: module.hpp:36
Definition: module.hpp:90
Definition: module.hpp:21
Definition: noncopyable.hpp:6
A scoped type ID generator. Allows for generating unique IDs for types within a specific scope.
Definition: type_id.hpp:30
static auto value() noexcept -> std::uint32_t
Generates a unique ID for the type.
Definition: type_id.hpp:40