1#include "gaia/config/config.h"
5#include "gaia/ecs/chunk_iterator.h"
6#include "gaia/ecs/id.h"
7#include "gaia/ecs/query.h"
8#include "gaia/mt/jobhandle.h"
10#if GAIA_SYSTEMS_ENABLED
15 inline constexpr const char* sc_system_query_func_str =
"System_exec";
16 util::str_view entity_name(
const World& world, Entity entity);
27 Entity entity = EntityBad;
31 QueryExecType execType = QueryExecType::Default;
33 mt::JobHandle jobHandle = mt::JobNull;
45 if (jobHandle != (mt::JobHandle)mt::JobNull_t{}) {
46 auto& tp = mt::ThreadPool::get();
60 void exec(World& world) {
61 [[maybe_unused]]
auto& queryInfo = query.fetch();
64 const auto name = entity_name(*queryInfo.world(), entity);
65 const char* pScopeName = !name.empty() ? name.data() : sc_system_query_func_str;
66 GAIA_PROF_SCOPE2(pScopeName);
69 auto* pRuntime = world.systems().data_try(entity);
70 GAIA_ASSERT(pRuntime !=
nullptr);
71 if (pRuntime ==
nullptr || !pRuntime->on_each_func)
74 static_cast<void>(pRuntime->on_each_func(query, execType, SystemRuntimeData::RunMode::Immediate));
84 GAIA_NODISCARD mt::JobHandle job_handle(World& world) {
85 if (jobHandle == (mt::JobHandle)mt::JobNull_t{}) {
86 auto& tp = mt::ThreadPool::get();
88 syncJob.func = [
this, &world]() {
91 syncJob.flags = mt::JobCreationFlags::ManualDelete;
92 jobHandle = tp.add(GAIA_MOV(syncJob));
106 GAIA_NODISCARD SchedJob job(World& world) {
107 auto* pRuntime = world.systems().data_try(entity);
108 GAIA_ASSERT(pRuntime !=
nullptr);
109 if (pRuntime ==
nullptr || !pRuntime->on_each_func)
112 return pRuntime->on_each_func(query, execType, SystemRuntimeData::RunMode::DeferredJob);
118 template <
typename Serializer>
119 void save(Serializer& s)
const {
125 template <
typename Serializer>
126 void load(Serializer& s) {
138 class SystemBuilder {
145 void validate()
const {
146 GAIA_ASSERT(m_world.valid(m_entity));
152 auto ss = m_world.acc_mut(m_entity);
153 auto& sys = ss.smut<System_>();
159 const System_& data()
const {
160 auto ss = m_world.acc(m_entity);
161 const auto& sys = ss.get<System_>();
167 SystemRuntimeData& runtime_data() {
168 return m_world.systems().data(m_entity);
173 const SystemRuntimeData& runtime_data()
const {
174 return m_world.systems().data(m_entity);
181 SystemBuilder(World& world, Entity entity): m_world(world), m_entity(entity) {}
192 SystemBuilder& kind(QueryCacheKind kind) {
194 data().query.kind(kind);
202 SystemBuilder& scope(QueryCacheScope scope) {
204 data().query.scope(scope);
218 SystemBuilder& add(QueryInput item) {
220 data().query.add(item);
230 SystemBuilder& is(Entity entity,
const QueryTermOptions& options = {}) {
231 return all(Pair(Is, entity), options);
240 SystemBuilder& in(Entity entity, QueryTermOptions options = {}) {
242 return all(Pair(Is, entity), options);
251 SystemBuilder& all(Entity entity,
const QueryTermOptions& options = {}) {
253 data().query.all(entity, options);
261 SystemBuilder& any(Entity entity,
const QueryTermOptions& options = {}) {
263 data().query.any(entity, options);
271 SystemBuilder& or_(Entity entity,
const QueryTermOptions& options = {}) {
273 data().query.or_(entity, options);
281 SystemBuilder& no(Entity entity,
const QueryTermOptions& options = {}) {
283 data().query.no(entity, options);
289 SystemBuilder& match_prefab() {
291 data().query.match_prefab();
298 SystemBuilder& changed(Entity entity) {
300 data().query.changed(entity);
308 template <
typename T>
309 SystemBuilder& all(
const QueryTermOptions& options);
315 template <
typename T>
316 SystemBuilder& any(
const QueryTermOptions& options);
322 template <
typename T>
323 SystemBuilder& or_(
const QueryTermOptions& options);
329 template <
typename T>
330 SystemBuilder& no(
const QueryTermOptions& options);
337 template <
typename T>
338 SystemBuilder& all();
343 template <
typename T>
344 SystemBuilder& any();
349 template <
typename T>
350 SystemBuilder& or_();
355 template <
typename T>
361 template <
typename T>
362 SystemBuilder& changed();
374 SystemBuilder& depth_order(Entity relation = ChildOf) {
375 data().query.depth_order(relation);
382 template <
typename Rel>
383 SystemBuilder& depth_order();
392 SystemBuilder& group_by(Entity entity, TGroupByFunc func = group_by_func_default) {
393 data().query.group_by(entity, func);
401 template <
typename T>
402 SystemBuilder& group_by(TGroupByFunc func = group_by_func_default);
409 template <
typename Rel,
typename Tgt>
410 SystemBuilder& group_by(TGroupByFunc func = group_by_func_default);
418 SystemBuilder& group_dep(Entity relation) {
419 data().query.group_dep(relation);
427 template <
typename Rel>
428 SystemBuilder& group_dep();
435 SystemBuilder& group_id(GroupId groupId) {
436 data().query.group_id(groupId);
443 SystemBuilder& group_id(Entity entity) {
444 GAIA_ASSERT(!entity.pair());
445 data().query.group_id(entity.id());
452 template <
typename T>
453 SystemBuilder& group_id();
465 SystemBuilder& name(
const char* name, uint32_t len = 0) {
466 m_world.name(m_entity, name, len);
474 SystemBuilder& name_raw(
const char* name, uint32_t len = 0) {
475 m_world.name_raw(m_entity, name, len);
493 SystemBuilder& phase(Entity phaseEntity) {
495 GAIA_ASSERT(m_world.valid(phaseEntity));
496 GAIA_ASSERT(!phaseEntity.pair());
497 m_world.add(m_entity, {ChildOf, phaseEntity});
498 m_world.add(m_entity, {DependsOn, phaseEntity});
511 SystemBuilder& mode(QueryExecType type) {
530 SystemBuilder& ctx(
void* pCtx) {
532 data().query.ctx(pCtx);
538 GAIA_NODISCARD
void* ctx()
const {
540 return data().query.ctx();
550 SystemBuilder& main_thread(
bool required =
true) {
552 data().query.main_thread(required);
563 SystemBuilder& reads(Entity entity) {
565 data().query.reads(entity);
573 template <
typename T>
574 SystemBuilder& reads() {
576 data().query.template reads<T>();
584 SystemBuilder& writes(Entity entity) {
586 data().query.writes(entity);
594 template <
typename T>
595 SystemBuilder& writes() {
597 data().query.template writes<T>();
607 template <
typename Func, std::enable_if_t<detail::is_query_iter_callback_v<Func>,
int> = 0>
608 SystemBuilder& on_each(Func func) {
611 auto& runtime = runtime_data();
612 runtime.on_each_func = [func](Query& query, QueryExecType execType, SystemRuntimeData::RunMode mode)
mutable {
613 if (mode == SystemRuntimeData::RunMode::DeferredJob)
614 return query.job(func, execType);
616 query.each_runtime_erased(
617 execType,
static_cast<void*
>(&func), &detail::QueryImpl::template invoke_runtime_iter<Func, Iter>,
618 Constraints::EnabledOnly);
622 return (SystemBuilder&)*
this;
631 template <
typename Func, std::enable_if_t<!detail::is_query_iter_callback_v<Func>,
int> = 0>
632 SystemBuilder& on_each(Func func);
636 GAIA_NODISCARD Entity entity()
const {
656 GAIA_NODISCARD SchedJob job() {
658 return ctx.job(m_world);
663 GAIA_NODISCARD mt::JobHandle job_handle() {
665 return ctx.job_handle(m_world);
673 #include "gaia/ecs/system_typed.inl"
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9