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;
44 if (jobHandle != (mt::JobHandle)mt::JobNull_t{}) {
59 void exec(World& world) {
60 [[maybe_unused]]
auto& queryInfo = query.fetch();
63 const auto name = entity_name(*queryInfo.world(), entity);
64 const char* pScopeName = !name.empty() ? name.data() : sc_system_query_func_str;
65 GAIA_PROF_SCOPE2(pScopeName);
68 auto* pRuntime = world.systems().data_try(entity);
69 GAIA_ASSERT(pRuntime !=
nullptr);
70 if (pRuntime ==
nullptr || !pRuntime->on_each_func)
73 static_cast<void>(pRuntime->on_each_func(query, execType, SystemRuntimeData::RunMode::Immediate));
83 GAIA_NODISCARD mt::JobHandle job_handle(World& world) {
84 if (jobHandle == (mt::JobHandle)mt::JobNull_t{}) {
86 auto func = [
this, &world]() {
89 static_assert(mt::JobFunc::stores_inline<decltype(func)>(),
"System jobs must not allocate");
92 syncJob.func = GAIA_MOV(func);
93 syncJob.flags = mt::JobCreationFlags::ManualDelete;
94 jobHandle = tp.add(GAIA_MOV(syncJob));
108 GAIA_NODISCARD SchedJob job(World& world) {
109 auto* pRuntime = world.systems().data_try(entity);
110 GAIA_ASSERT(pRuntime !=
nullptr);
111 if (pRuntime ==
nullptr || !pRuntime->on_each_func)
114 return pRuntime->on_each_func(query, execType, SystemRuntimeData::RunMode::DeferredJob);
120 template <
typename Serializer>
121 void save(Serializer& s)
const {
127 template <
typename Serializer>
128 void load(Serializer& s) {
140 class SystemBuilder {
147 void validate()
const {
148 GAIA_ASSERT(m_world.valid(m_entity));
154 auto ss = m_world.acc_mut(m_entity);
155 auto& sys = ss.smut<System_>();
161 const System_& data()
const {
162 auto ss = m_world.acc(m_entity);
163 const auto& sys = ss.get<System_>();
169 SystemRuntimeData& runtime_data() {
170 return m_world.systems().data(m_entity);
175 const SystemRuntimeData& runtime_data()
const {
176 return m_world.systems().data(m_entity);
183 SystemBuilder(World& world, Entity entity): m_world(world), m_entity(entity) {}
194 SystemBuilder& kind(QueryCacheKind kind) {
196 data().query.kind(kind);
204 SystemBuilder& scope(QueryCacheScope scope) {
206 data().query.scope(scope);
220 SystemBuilder& add(QueryInput item) {
222 data().query.add(item);
232 SystemBuilder& is(Entity entity,
const QueryTermOptions& options = {}) {
233 return all(Pair(Is, entity), options);
242 SystemBuilder& in(Entity entity, QueryTermOptions options = {}) {
244 return all(Pair(Is, entity), options);
253 SystemBuilder& all(Entity entity,
const QueryTermOptions& options = {}) {
255 data().query.all(entity, options);
263 SystemBuilder& any(Entity entity,
const QueryTermOptions& options = {}) {
265 data().query.any(entity, options);
273 SystemBuilder& or_(Entity entity,
const QueryTermOptions& options = {}) {
275 data().query.or_(entity, options);
283 SystemBuilder& no(Entity entity,
const QueryTermOptions& options = {}) {
285 data().query.no(entity, options);
291 SystemBuilder& match_prefab() {
293 data().query.match_prefab();
300 SystemBuilder& changed(Entity entity) {
302 data().query.changed(entity);
310 template <
typename T>
311 SystemBuilder& all(
const QueryTermOptions& options);
317 template <
typename T>
318 SystemBuilder& any(
const QueryTermOptions& options);
324 template <
typename T>
325 SystemBuilder& or_(
const QueryTermOptions& options);
331 template <
typename T>
332 SystemBuilder& no(
const QueryTermOptions& options);
339 template <
typename T>
340 SystemBuilder& all();
345 template <
typename T>
346 SystemBuilder& any();
351 template <
typename T>
352 SystemBuilder& or_();
357 template <
typename T>
363 template <
typename T>
364 SystemBuilder& changed();
376 SystemBuilder& depth_order(Entity relation = ChildOf) {
377 data().query.depth_order(relation);
384 template <
typename Rel>
385 SystemBuilder& depth_order();
394 SystemBuilder& group_by(Entity entity, TGroupByFunc func = group_by_func_default) {
395 data().query.group_by(entity, func);
403 template <
typename T>
404 SystemBuilder& group_by(TGroupByFunc func = group_by_func_default);
411 template <
typename Rel,
typename Tgt>
412 SystemBuilder& group_by(TGroupByFunc func = group_by_func_default);
420 SystemBuilder& group_dep(Entity relation) {
421 data().query.group_dep(relation);
429 template <
typename Rel>
430 SystemBuilder& group_dep();
437 SystemBuilder& group_id(GroupId groupId) {
438 data().query.group_id(groupId);
445 SystemBuilder& group_id(Entity entity) {
446 GAIA_ASSERT(!entity.pair());
447 data().query.group_id(entity.id());
454 template <
typename T>
455 SystemBuilder& group_id();
467 SystemBuilder& name(
const char* name, uint32_t len = 0) {
468 m_world.name(m_entity, name, len);
476 SystemBuilder& name_raw(
const char* name, uint32_t len = 0) {
477 m_world.name_raw(m_entity, name, len);
496 SystemBuilder& phase(Entity phaseEntity) {
498 GAIA_ASSERT(m_world.valid(phaseEntity));
499 GAIA_ASSERT(!phaseEntity.pair());
500 m_world.add(m_entity, {ChildOf, phaseEntity});
501 m_world.add(m_entity, {DependsOn, phaseEntity});
514 SystemBuilder& mode(QueryExecType type) {
537 SystemBuilder& ctx(
void* pCtx) {
539 data().query.ctx(pCtx);
545 GAIA_NODISCARD
void* ctx()
const {
547 return data().query.ctx();
557 SystemBuilder& main_thread(
bool required =
true) {
559 data().query.main_thread(required);
569 SystemBuilder& no_access() {
571 data().query.no_access();
580 SystemBuilder& reads(Entity entity) {
582 data().query.reads(entity);
590 template <
typename T>
591 SystemBuilder& reads() {
593 data().query.template reads<T>();
602 SystemBuilder& writes(Entity entity) {
604 data().query.writes(entity);
612 template <
typename T>
613 SystemBuilder& writes() {
615 data().query.template writes<T>();
625 template <
typename Func, std::enable_if_t<detail::is_query_iter_callback_v<Func>,
int> = 0>
626 SystemBuilder& on_each(Func func) {
629 auto& runtime = runtime_data();
630 runtime.on_each_func = [func](Query& query, QueryExecType execType, SystemRuntimeData::RunMode mode)
mutable {
631 if (mode == SystemRuntimeData::RunMode::DeferredJob)
632 return query.job(func, execType);
634 query.each_runtime_erased(
635 execType,
static_cast<void*
>(&func), &detail::QueryImpl::template invoke_runtime_iter<Func, Iter>,
636 Constraints::EnabledOnly);
640 return (SystemBuilder&)*
this;
649 template <
typename Func, std::enable_if_t<!detail::is_query_iter_callback_v<Func>,
int> = 0>
650 SystemBuilder& on_each(Func func);
654 GAIA_NODISCARD Entity entity()
const {
674 GAIA_NODISCARD SchedJob job() {
676 return ctx.job(m_world);
681 GAIA_NODISCARD mt::JobHandle job_handle() {
683 return ctx.job_handle(m_world);
691 #include "gaia/ecs/system_typed.inl"
static ThreadPool & get()
Returns the process-wide thread-pool instance.
Definition threadpool.h:168
void load(Reader &reader, T &data)
Read data using Reader at compile-time.
Definition ser_ct.h:112
void save(Writer &writer, const T &data)
Write data using Writer at compile-time.
Definition ser_ct.h:101
Definition system.inl:695