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);
35
36 void del(World& world, Entity entity);
37
38 Entity entity_from_id(const World& world, EntityId id);
39 Entity id_entity(const World& world, Entity id);
40 Entity pair_rel(const World& world, Entity pair);
41 Entity pair_tgt(const World& world, Entity pair);
42
47 bool valid(const World& world, Entity entity);
48
49 bool is(const World& world, Entity entity, Entity baseEntity);
50 bool is_base(const World& world, Entity entity);
51
52 Archetype* archetype_from_entity(const World& world, Entity entity);
53
54 util::str_view entity_name(const World& world, Entity entity);
55 util::str_view entity_name(const World& world, EntityId entityId);
56 Entity target(const World& world, Entity entity, Entity relation);
59 void
60 world_for_each_target(const World& world, Entity entity, Entity relation, void* ctx, void (*func)(void*, Entity));
61 Entity world_pair_target_if_alive(const World& world, Entity pair);
62 bool world_entity_enabled(const World& world, Entity entity);
63 bool world_entity_enabled_hierarchy(const World& world, Entity entity, Entity relation);
64 uint32_t world_enabled_hierarchy_version(const World& world);
68 uint32_t world_archetype_delete_version(const World& world);
72 uint32_t world_version(const World& world);
77 uint32_t world_rel_version(const World& world, Entity relation);
79 bool world_relation_is_non_fragmenting(const World& world, Entity relation);
81 bool world_relation_is_hierarchy(const World& world, Entity relation);
83 bool world_relation_is_fragmenting(const World& world, Entity relation);
85 bool world_relation_is_fragmenting_hierarchy(const World& world, Entity relation);
87 bool world_relation_supports_depth_order(const World& world, Entity relation);
89 bool world_relation_depth_order_prunes_disabled_subtrees(const World& world, Entity relation);
90 template <typename T>
91 decltype(auto) world_query_entity_arg_by_id(World& world, Entity entity, Entity id);
92
93 // Traversal API
94
95 template <typename Func>
96 void as_relations_trav(const World& world, Entity target, Func func);
97 template <typename Func>
98 bool as_relations_trav_if(const World& world, Entity target, Func func);
99 template <typename Func>
100 void as_targets_trav(const World& world, Entity relation, Func func);
101 template <typename Func>
102 bool as_targets_trav_if(const World& world, Entity relation, Func func);
103 template <typename Func>
104 void targets_trav(const World& world, Entity relation, Entity source, Func func);
105 template <typename Func>
106 bool targets_trav_if(const World& world, Entity relation, Entity source, Func func);
107 const cnt::darray<Entity>& targets_trav_cache(const World& world, Entity relation, Entity source);
108 template <typename Func>
109 void sources(const World& world, Entity relation, Entity target, Func func);
110 template <typename Func>
111 void sources_if(const World& world, Entity relation, Entity target, Func func);
112 const cnt::darray<Entity>& sources_bfs_trav_cache(const World& world, Entity relation, Entity rootTarget);
113 template <typename Func>
114 void sources_bfs(const World& world, Entity relation, Entity rootTarget, Func func);
115 template <typename Func>
116 bool sources_bfs_if(const World& world, Entity relation, Entity rootTarget, Func func);
117 // Query API
118
119 QuerySerBuffer& query_buffer(World& world, QueryId& serId);
120 void query_buffer_reset(World& world, QueryId& serId);
121
122 Entity expr_to_entity(const World& world, va_list& args, std::span<const char> exprRaw);
123
124 GroupId group_by_func_default(const World& world, const Archetype& archetype, Entity groupBy);
125
126 // Locking API
127
128 void lock(World& world);
129 void unlock(World& world);
130 bool locked(const World& world);
131
132#if GAIA_OBSERVERS_ENABLED
133 // Deferred observer-notification API
134 //
135 // Observer dispatch mutates world-owned state and runs user callbacks, so it cannot happen
136 // on a worker thread. Regions whose writes may run in parallel record notifications instead
137 // and let the coordinator deliver them after the region joins.
138
139 void world_defer_on_set_begin(World& world, uint32_t slotCount);
140 void world_defer_on_set_end(World& world);
141#endif
142
143 // Deferred sorted-query invalidation API
144 //
145 // Sorted-query invalidation flips a shared `SortEntities` dirty bit on every sorted query
146 // keyed by the written component. That shared flag is not safe to touch from a worker
147 // thread, so regions whose writes may run in parallel record which component entities were
148 // written and the coordinator applies the invalidation once the region joins. Work items
149 // identify themselves with the shared deferral slot below, which the observer (`OnSet`)
150 // channel uses as well.
151
153 inline static constexpr uint32_t BadDeferSlot = (uint32_t)-1;
154
155 void world_defer_sort_inv_begin(World& world, uint32_t slotCount);
156 void world_defer_sort_inv_end(World& world);
157
160 void world_defer_parallel_begin(World& world, uint32_t itemCount);
161 void world_defer_parallel_end(World& world);
162
168 GAIA_NODISCARD inline uint32_t& defer_slot_ref() {
169 static thread_local uint32_t s_slot = BadDeferSlot;
170 return s_slot;
171 }
172
174 GAIA_NODISCARD inline uint32_t defer_slot() {
175 return defer_slot_ref();
176 }
177
179 class DeferSlotScope final {
180 uint32_t m_prev;
181
182 public:
183 explicit DeferSlotScope(uint32_t slot): m_prev(defer_slot_ref()) {
184 defer_slot_ref() = slot;
185 }
187 defer_slot_ref() = m_prev;
188 }
189
190 DeferSlotScope(DeferSlotScope&&) = delete;
191 DeferSlotScope(const DeferSlotScope&) = delete;
192 DeferSlotScope& operator=(DeferSlotScope&&) = delete;
193 DeferSlotScope& operator=(const DeferSlotScope&) = delete;
194 };
195
196 // CommandBuffer API
197
198 CommandBufferST& cmd_buffer_st_get(World& world);
199 CommandBufferMT& cmd_buffer_mt_get(World& world);
200 void commit_cmd_buffer_st(World& world);
201 void commit_cmd_buffer_mt(World& world);
202 } // namespace ecs
203} // namespace gaia
Binds the calling thread to a work-item deferral slot for the lifetime of the scope.
Definition api.h:179
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:52