Gaia-ECS v1.0.0
A simple and powerful entity component system
Loading...
Searching...
No Matches
api.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include <cstdarg>
5
6#include "gaia/core/span.h"
7#include "gaia/ecs/command_buffer_fwd.h"
8#include "gaia/ecs/id_fwd.h"
9#include "gaia/ecs/query_fwd.h"
10
11namespace gaia {
12 namespace ecs {
13 class World;
14 class ComponentCache;
15 class Archetype;
16 struct ComponentCacheItem;
17 struct EntityContainer;
18 struct Entity;
19
20 // Component API
21
22 const ComponentCache& comp_cache(const World& world);
23 ComponentCache& comp_cache_mut(World& world);
28 template <typename T>
29 const ComponentCacheItem& comp_cache_add(World& world);
30
31 // Entity API
32
33 const EntityContainer& fetch(const World& world, Entity entity);
34 EntityContainer& fetch_mut(World& world, Entity entity);
41 EntityContainer* try_fetch_mut(World& world, Entity entity);
42
43 void del(World& world, Entity entity);
44
45#if GAIA_USE_SAFE_ENTITY
49 void safe_entity_acquire(World& world, Entity entity);
54 void safe_entity_release(World& world, Entity entity);
55#endif
56
57 Entity entity_from_id(const World& world, EntityId id);
58 Entity id_entity(const World& world, Entity id);
59 Entity pair_rel(const World& world, Entity pair);
60 Entity pair_tgt(const World& world, Entity pair);
61
66 bool valid(const World& world, Entity entity);
67
68 bool is(const World& world, Entity entity, Entity baseEntity);
69 bool is_base(const World& world, Entity entity);
70
71 Archetype* archetype_from_entity(const World& world, Entity entity);
72
73 util::str_view entity_name(const World& world, Entity entity);
74 util::str_view entity_name(const World& world, EntityId entityId);
75 Entity target(const World& world, Entity entity, Entity relation);
78 void world_each_target(const World& world, Entity entity, Entity relation, void* ctx, void (*func)(void*, Entity));
79 Entity world_pair_target_if_alive(const World& world, Entity pair);
80 bool world_entity_enabled(const World& world, Entity entity);
81 bool world_entity_enabled_hierarchy(const World& world, Entity entity, Entity relation);
82 uint32_t world_enabled_hierarchy_version(const World& world);
86 uint32_t world_archetype_delete_version(const World& world);
90 uint32_t world_version(const World& world);
95 uint32_t world_rel_version(const World& world, Entity relation);
97 bool world_relation_is_non_fragmenting(const World& world, Entity relation);
99 bool world_relation_is_hierarchy(const World& world, Entity relation);
101 bool world_relation_is_fragmenting(const World& world, Entity relation);
103 bool world_relation_is_fragmenting_hierarchy(const World& world, Entity relation);
105 bool world_relation_supports_depth_order(const World& world, Entity relation);
107 bool world_relation_depth_order_prunes_disabled_subtrees(const World& world, Entity relation);
108 template <typename T>
109 decltype(auto) world_query_entity_arg_by_id(World& world, Entity entity, Entity id);
110
111 // Traversal API
112
113 template <typename Func>
114 void as_relations_trav(const World& world, Entity target, Func func);
115 template <typename Func>
116 bool as_relations_trav_if(const World& world, Entity target, Func func);
117 template <typename Func>
118 void as_targets_trav(const World& world, Entity relation, Func func);
119 template <typename Func>
120 bool as_targets_trav_if(const World& world, Entity relation, Func func);
121 template <typename Func>
122 void targets_trav(const World& world, Entity relation, Entity source, Func func);
123 template <typename Func>
124 bool targets_trav_if(const World& world, Entity relation, Entity source, Func func);
125 const cnt::darray<Entity>& targets_trav_cache(const World& world, Entity relation, Entity source);
126 template <typename Func>
127 void sources(const World& world, Entity relation, Entity target, Func func);
128 template <typename Func>
129 void sources_if(const World& world, Entity relation, Entity target, Func func);
130 const cnt::darray<Entity>& sources_bfs_trav_cache(const World& world, Entity relation, Entity rootTarget);
131 template <typename Func>
132 void sources_bfs(const World& world, Entity relation, Entity rootTarget, Func func);
133 template <typename Func>
134 bool sources_bfs_if(const World& world, Entity relation, Entity rootTarget, Func func);
135 // Query API
136
137 QuerySerBuffer& query_buffer(World& world, QueryId& serId);
138 void query_buffer_reset(World& world, QueryId& serId);
139
140 Entity expr_to_entity(const World& world, va_list& args, std::span<const char> exprRaw);
141
142 GroupId group_by_func_default(const World& world, const Archetype& archetype, Entity groupBy);
143
144 // Locking API
145
146 void lock(World& world);
147 void unlock(World& world);
148 bool locked(const World& world);
149
150#if GAIA_OBSERVERS_ENABLED
151 // Deferred observer-notification API
152 //
153 // Observer dispatch mutates world-owned state and runs user callbacks, so it cannot happen
154 // on a worker thread. Regions whose writes may run in parallel record notifications instead
155 // and let the coordinator deliver them after the region joins.
156
157 void world_defer_on_set_begin(World& world, uint32_t slotCount);
158 void world_defer_on_set_end(World& world);
159#endif
160
161 // Deferred sorted-query invalidation API
162 //
163 // Sorted-query invalidation flips a shared `SortEntities` dirty bit on every sorted query
164 // keyed by the written component. That shared flag is not safe to touch from a worker
165 // thread, so regions whose writes may run in parallel record which component entities were
166 // written and the coordinator applies the invalidation once the region joins. Work items
167 // identify themselves with the shared deferral slot below, which the observer (`OnSet`)
168 // channel uses as well.
169
171 inline static constexpr uint32_t BadDeferSlot = (uint32_t)-1;
172
173 void world_defer_sort_inv_begin(World& world, uint32_t slotCount);
174 void world_defer_sort_inv_end(World& world);
175
178 void world_defer_parallel_begin(World& world, uint32_t itemCount);
179 void world_defer_parallel_end(World& world);
180
186 GAIA_NODISCARD inline uint32_t& defer_slot_ref() {
187 static thread_local uint32_t s_slot = BadDeferSlot;
188 return s_slot;
189 }
190
192 GAIA_NODISCARD inline uint32_t defer_slot() {
193 return defer_slot_ref();
194 }
195
197 class DeferSlotScope final {
198 uint32_t m_prev;
199
200 public:
201 explicit DeferSlotScope(uint32_t slot): m_prev(defer_slot_ref()) {
202 defer_slot_ref() = slot;
203 }
205 defer_slot_ref() = m_prev;
206 }
207
208 DeferSlotScope(DeferSlotScope&&) = delete;
209 DeferSlotScope(const DeferSlotScope&) = delete;
210 DeferSlotScope& operator=(DeferSlotScope&&) = delete;
211 DeferSlotScope& operator=(const DeferSlotScope&) = delete;
212 };
213
214 // CommandBuffer API
215
216 CommandBufferST& cmd_buffer_st_get(World& world);
217 CommandBufferMT& cmd_buffer_mt_get(World& world);
218 void commit_cmd_buffer_st(World& world);
219 void commit_cmd_buffer_mt(World& world);
220 } // namespace ecs
221} // namespace gaia
Binds the calling thread to a work-item deferral slot for the lifetime of the scope.
Definition api.h:197
Owns entities, components, archetypes, queries, observers, and systems.
Definition world.h:80
Buffer for deferred execution of some operations on entities.
Definition command_buffer.h:56