Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
resource.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 <atomic>
9#include <concepts>
10#include <cstddef>
11#include <string_view>
12#include <type_traits>
13
14namespace helios::ecs {
15
16/**
17 * @brief Concept for valid resource types.
18 * @details Resources must be object types that can be stored and accessed.
19 */
20template <typename T>
21concept ResourceTrait = std::is_object_v<std::remove_cvref_t<T>> && std::is_destructible_v<std::remove_cvref_t<T>>;
22
23/**
24 * @brief Concept for resources that provide custom names.
25 * @details A resource with name trait must satisfy ResourceTrait and provide:
26 * - `static constexpr std::string_view GetName() noexcept`
27 */
28template <typename T>
30 { T::GetName() } -> std::same_as<std::string_view>;
31};
32
33/**
34 * @brief Concept for resources that provide thread-safety trait.
35 * @details A resource with thread-safety trait must satisfy ResourceTrait and provide:
36 * - `static constexpr bool ThreadSafe() noexcept`
37 */
38template <typename T>
40 { T::ThreadSafe() } -> std::same_as<bool>;
41};
42
43/**
44 * @brief Concept for atomic resources that can be accessed concurrently.
45 * @details Atomic resources don't affect scheduling and are thread-safe.
46 */
47template <typename T>
48concept AtomicResourceTrait = ResourceTrait<T> && requires { std::atomic<T>{}; };
49
50/**
51 * @brief Type ID for resources.
52 */
54
55/**
56 * @brief Gets type ID for a resource type.
57 * @tparam T Resource type
58 * @return Unique type ID for the resource
59 */
60template <ResourceTrait T>
62 return ctti::type_index_of<T>().hash();
63}
64
65/**
66 * @brief Gets name for a resource type.
67 * @tparam T Resource type
68 * @return Name of the resource
69 */
70template <ResourceTrait T>
71[[nodiscard]] constexpr std::string_view ResourceNameOf() noexcept {
72 if constexpr (ResourceWithNameTrait<T>) {
73 return T::GetName();
74 } else {
75 return ctti::name_of<T>();
76 }
77}
78
79/**
80 * @brief Checks if a resource type is thread-safe.
81 * @tparam T Resource type
82 * @return True if resource is thread-safe, false otherwise
83 */
84template <ResourceTrait T>
87 return T::ThreadSafe();
88 } else {
89 return false;
90 }
91}
92
93} // namespace helios::ecs
Concept for atomic resources that can be accessed concurrently.
Definition resource.hpp:48
Concept for valid resource types.
Definition resource.hpp:21
Concept for resources that provide custom names.
Definition resource.hpp:29
Concept for resources that provide thread-safety trait.
Definition resource.hpp:39
constexpr ResourceTypeId ResourceTypeIdOf() noexcept
Gets type ID for a resource type.
Definition resource.hpp:61
constexpr std::string_view ResourceNameOf() noexcept
Gets name for a resource type.
Definition resource.hpp:71
constexpr bool IsResourceThreadSafe() noexcept
Checks if a resource type is thread-safe.
Definition resource.hpp:85
BasicQuery< World, Allocator, Components... > Query
Type alias for query with mutable world access.
Definition query.hpp:2481