Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
world_view.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <helios/assert.hpp>
11
12#include <array>
13#include <cstddef>
14#include <functional>
15
16namespace helios::ecs {
17
18/**
19 * @brief Thread-safe read-only view into the world.
20 * @details Provides read-only introspection methods for entity, component,
21 * resource, and message queries. All operations are thread-safe for read
22 * access.
23 * @note Thread-safe for read operations.
24 */
25class WorldView {
26public:
27 /**
28 * @brief Constructs a WorldView from a World reference.
29 * @param world Reference to the World to view.
30 */
31 explicit constexpr WorldView(const World& world) noexcept : world_(world) {}
32 WorldView(const WorldView&) = delete;
33 WorldView(WorldView&&) = delete;
34 constexpr ~WorldView() noexcept = default;
35
36 WorldView& operator=(const WorldView&) = delete;
37 WorldView& operator=(WorldView&&) = delete;
38
39 /**
40 * @brief Checks if entity exists in the world.
41 * @note Thread-safe.
42 * @warning Triggers assertion in next cases:
43 * - Entity is invalid.
44 * - World does not own entity.
45 * @param entity Entity to check
46 * @return True if entity exists, false otherwise
47 */
48 [[nodiscard]] bool EntityExists(Entity entity) const noexcept;
49
50 /**
51 * @brief Checks if entity has component.
52 * @note Thread-safe.
53 * @warning Triggers assertion in next cases:
54 * - Entity is invalid.
55 * - World does not own entity.
56 * @tparam T Component type to check
57 * @param entity Entity to check
58 * @return True if entity has the component
59 */
60 template <ComponentTrait T>
61 [[nodiscard]] bool HasComponent(Entity entity) const;
62
63 /**
64 * @brief Checks if entity has components.
65 * @note Thread-safe.
66 * @warning Triggers assertion in next cases:
67 * - Entity is invalid.
68 * - World does not own entity.
69 * @tparam Ts Components types
70 * @param entity Entity to check
71 * @return Array of bools indicating whether entity has each component
72 */
73 template <ComponentTrait... Ts>
74 requires utils::UniqueTypes<Ts...> && (sizeof...(Ts) > 0)
75 [[nodiscard]] auto HasComponents(Entity entity) const
76 -> std::array<bool, sizeof...(Ts)>;
77
78 /**
79 * @brief Checks if a resource exists.
80 * @note Thread-safe.
81 * @tparam T Resource type
82 * @return True if resource exists, false otherwise
83 */
84 template <ResourceTrait T>
85 [[nodiscard]] bool HasResource() const {
86 return world_.get().template HasResource<T>();
87 }
88
89 /**
90 * @brief Checks if a message type is registered.
91 * @note Thread-safe.
92 * @tparam T Message type
93 * @return True if message type is registered, false otherwise
94 */
95 template <AnyMessageTrait T>
96 [[nodiscard]] bool HasMessage() const noexcept {
97 return world_.get().template HasMessage<T>();
98 }
99
100 /**
101 * @brief Checks if messages of a specific type exist in message queue.
102 * @note Thread-safe.
103 * @tparam T Message type
104 * @return True if messages exist, false otherwise
105 */
106 template <AnyMessageTrait T>
107 [[nodiscard]] bool HasMessages() const noexcept {
108 return world_.get().template HasMessages<T>();
109 }
110
111 /**
112 * @brief Gets the number of entities in the world.
113 * @note Thread-safe.
114 * @return Number of entities in the world
115 */
116 [[nodiscard]] size_t EntityCount() const noexcept {
117 return world_.get().EntityCount();
118 }
119
120 /**
121 * @brief Gets the number of resources in the world.
122 * @note Thread-safe.
123 * @return Number of resources in the world
124 */
125 [[nodiscard]] size_t ResourceCount() const noexcept {
126 return world_.get().ResourceCount();
127 }
128
129private:
130 std::reference_wrapper<const World> world_;
131};
132
133inline bool WorldView::EntityExists(Entity entity) const noexcept {
134 HELIOS_ASSERT(entity.Valid(), "Entity '{}' is invalid!", entity);
135 const auto& world = world_.get();
136 HELIOS_ASSERT(world.Exists(entity),
137 "Entity '{}' does not exist in the world!", entity);
138 return world.Exists(entity);
139}
140
141template <ComponentTrait T>
142inline bool WorldView::HasComponent(Entity entity) const {
143 HELIOS_ASSERT(entity.Valid(), "Entity '{}' is invalid!", entity);
144 const auto& world = world_.get();
145 HELIOS_ASSERT(world.Exists(entity),
146 "Entity '{}' does not exist in the world!", entity);
147 return world.HasComponent<T>(entity);
148}
149
150template <ComponentTrait... Ts>
151 requires utils::UniqueTypes<Ts...> && (sizeof...(Ts) > 0)
152inline auto WorldView::HasComponents(Entity entity) const
153 -> std::array<bool, sizeof...(Ts)> {
154 HELIOS_ASSERT(entity.Valid(), "Entity '{}' is invalid!", entity);
155 const auto& world = world_.get();
156 HELIOS_ASSERT(world.Exists(entity),
157 "Entity '{}' does not exist in the world!", entity);
158 return world.template HasComponents<Ts...>(entity);
159}
160
161} // namespace helios::ecs
#define HELIOS_ASSERT(condition,...)
Assertion macro that aborts execution in debug builds.
Definition assert.hpp:259
Unique identifier for entities with generation counter to handle recycling.
Definition entity.hpp:22
constexpr bool Valid() const noexcept
Checks if the entity is valid.
Definition entity.hpp:64
auto HasComponents(Entity entity) const -> std::array< bool, sizeof...(Ts)>
Checks if entity has components.
bool EntityExists(Entity entity) const noexcept
Checks if entity exists in the world.
bool HasComponent(Entity entity) const
Checks if entity has component.
WorldView(const WorldView &)=delete
constexpr WorldView(const World &world) noexcept
Constructs a WorldView from a World reference.
WorldView(WorldView &&)=delete
bool HasMessage() const noexcept
Checks if a message type is registered.
size_t EntityCount() const noexcept
Gets the number of entities in the world.
constexpr ~WorldView() noexcept=default
size_t ResourceCount() const noexcept
Gets the number of resources in the world.
bool HasMessages() const noexcept
Checks if messages of a specific type exist in message queue.
bool HasResource() const
Checks if a resource exists.
The World class manages entities with their components and systems.
Definition world.hpp:39
Concept to check if a type can be used as a component.
Definition component.hpp:31
Concept for valid resource types.
Definition resource.hpp:25
Concept that checks if all types in a pack are unique (after removing cv/ref qualifiers).
STL namespace.