Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
resource.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::ecs {
10
11class World;
12
13/// @brief Type index for resources.
15
16/// @brief Type id for resources.
18
19/**
20 * @brief Concept for valid resource types.
21 * @details A resource must be destructible, be an object, move or copy
22 * constructible, and not polymorphic.
23 */
24template <typename T>
26 std::destructible<T> &&
27 (std::move_constructible<T> || std::copy_constructible<T>) &&
28 std::is_object_v<std::remove_cvref_t<T>> &&
29 !std::is_polymorphic_v<std::remove_cvref_t<T>>;
30
31/**
32 * @brief Concept for resources that provide custom names.
33 * @details A resource with name trait must satisfy `ResourceTrait` and provide:
34 * - `static constexpr std::string_view kName` variable
35 */
36template <typename T>
38 { std::remove_cvref_t<T>::kName } -> std::convertible_to<std::string_view>;
39};
40
41/**
42 * @brief Concept for resources that provide thread-safety trait.
43 * @details A resource with thread-safety trait must satisfy `ResourceTrait` and
44 * provide:
45 * - `static constexpr bool kThreadSafe` variable
46 */
47template <typename T>
49 { std::remove_cvref_t<T>::kThreadSafe } -> std::convertible_to<bool>;
50};
51
52/**
53 * @brief Concept for async resources.
54 * @details An async resource must satisfy `ResourceWithThreadSafetyTrait` and
55 * provide:
56 * - `static constexpr bool kThreadSafe = true` variable
57 */
58template <typename T>
60 requires std::remove_cvref_t<T>::kThreadSafe;
61};
62
63/**
64 * @brief Concept for resources that provide insertion callback.
65 * @details A resource with insertion callback must satisfy `ResourceTrait` and
66 * provide:
67 * - `static void OnInsert(World& world)` function
68 */
69template <typename T>
71 ResourceTrait<T> && requires(T& resource, World& world) {
72 { resource.OnInsert(world) } -> std::same_as<void>;
73 };
74
75/**
76 * @brief Concept for resources that provide removal callback.
77 * @details A resource with removal callback must satisfy `ResourceTrait` and
78 * provide:
79 * - `static void OnRemove(World& world)` function
80 */
81template <typename T>
83 ResourceTrait<T> && requires(T& resource, World& world) {
84 { resource.OnRemove(world) } -> std::same_as<void>;
85 };
86
87/**
88 * @brief Gets name for a resource type.
89 * @tparam T Resource type
90 * @return Name of the resource
91 */
92template <ResourceTrait T>
93[[nodiscard]] constexpr std::string_view ResourceNameOf() noexcept {
94 if constexpr (ResourceWithNameTrait<T>) {
95 return T::kName;
96 } else {
98 }
99}
100
101/**
102 * @brief Gets name for a resource type.
103 * @tparam T Resource type
104 * @param resource Resource instance
105 * @return Name of the resource
106 */
107template <ResourceTrait T>
108[[nodiscard]] constexpr std::string_view ResourceNameOf(
109 const T& /*resource*/) noexcept {
111}
112
113/**
114 * @brief Checks if a resource type is thread-safe.
115 * @tparam T Resource type
116 * @return True if resource is thread-safe, false otherwise
117 */
118template <ResourceTrait T>
119[[nodiscard]] consteval bool IsResourceThreadSafe() noexcept {
121}
122
123/**
124 * @brief Checks if a resource type is thread-safe.
125 * @tparam T Resource type
126 * @param resource Resource instance
127 * @return True if resource is thread-safe, false otherwise
128 */
129template <ResourceTrait T>
130[[nodiscard]] consteval bool IsResourceThreadSafe(
131 const T& /*resource*/) noexcept {
133}
134
135/**
136 * @brief Calls insertion callback for a resource type if it exists.
137 * @tparam T Resource type
138 * @param resource Resource instance
139 * @param world Reference to the world
140 */
141template <ResourceTrait T>
142constexpr void ResourceCallOnInsert(T& resource, World& world) noexcept {
144 resource.OnInsert(world);
145 }
146}
147
148/**
149 * @brief Calls removal callback for a resource type if it exists.
150 * @tparam T Resource type
151 * @param resource Resource instance
152 * @param world Reference to the world
153 */
154template <ResourceTrait T>
155constexpr void ResourceCallOnRemove(T& resource, World& world) noexcept {
157 resource.OnRemove(world);
158 }
159}
160
161} // namespace helios::ecs
The World class manages entities with their components and systems.
Definition world.hpp:39
Concept for async resources.
Definition resource.hpp:59
Concept for valid resource types.
Definition resource.hpp:25
Concept for resources that provide insertion callback.
Definition resource.hpp:70
Concept for resources that provide custom names.
Definition resource.hpp:37
Concept for resources that provide removal callback.
Definition resource.hpp:82
Concept for resources that provide thread-safety trait.
Definition resource.hpp:48
utils::TypeId ResourceTypeId
Type id for resources.
Definition resource.hpp:17
constexpr void ResourceCallOnInsert(T &resource, World &world) noexcept
Calls insertion callback for a resource type if it exists.
Definition resource.hpp:142
constexpr void ResourceCallOnRemove(T &resource, World &world) noexcept
Calls removal callback for a resource type if it exists.
Definition resource.hpp:155
utils::TypeIndex ResourceTypeIndex
Type index for resources.
Definition resource.hpp:14
constexpr std::string_view ResourceNameOf() noexcept
Gets name for a resource type.
Definition resource.hpp:93
consteval bool IsResourceThreadSafe() noexcept
Checks if a resource type is thread-safe.
Definition resource.hpp:119
constexpr std::string_view QualifiedTypeNameOf() noexcept
Retrieves the fully qualified type name of T.