Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
query_adapter_typed.inl
1#pragma once
2
3namespace gaia {
4 namespace ecs {
6 uint32_t argCount = 0;
7 Entity argIds[MAX_ITEMS_IN_QUERY]{};
8 bool writeFlags[MAX_ITEMS_IN_QUERY]{};
9 uint32_t firstWriteArg = MAX_ITEMS_IN_QUERY;
10 bool hasWriteArgs = false;
11 bool needsInheritedArgIds = false;
12 bool canUseDirectChunkEval = false;
13 bool hasInheritedTerms = false;
14 };
15
17 Entity termId = EntityBad;
18 bool isWrite = false;
19 bool isEntity = false;
20 bool isPair = false;
21 };
22
23 template <typename T>
24 GAIA_NODISCARD inline TypedQueryArgMeta typed_query_arg_meta(World& world) {
25 using Arg = std::remove_cv_t<std::remove_reference_t<T>>;
26 const bool isWrite =
27 std::is_lvalue_reference_v<T> && !std::is_const_v<std::remove_reference_t<T>> && !std::is_same_v<Arg, Entity>;
28 if constexpr (std::is_same_v<Arg, Entity>)
29 return TypedQueryArgMeta{EntityBad, isWrite, true, false};
30 else {
31 using FT = typename component_type_t<Arg>::TypeFull;
32 if constexpr (is_pair<FT>::value)
33 return TypedQueryArgMeta{EntityBad, isWrite, false, true};
34 else
35 return TypedQueryArgMeta{world_query_arg_id<Arg>(world), isWrite, false, false};
36 }
37 }
38
39 template <typename... T>
40 inline uint32_t
41 init_typed_query_arg_metas(TypedQueryArgMeta* pMetas, World& world, [[maybe_unused]] core::func_type_list<T...>) {
42 if constexpr (sizeof...(T) > 0) {
43 const TypedQueryArgMeta descs[] = {typed_query_arg_meta<T>(world)...};
44 GAIA_FOR(sizeof...(T)) {
45 pMetas[i] = descs[i];
46 }
47 }
48 return (uint32_t)sizeof...(T);
49 }
50
51#if GAIA_ASSERT_ENABLED
52 template <typename... T>
53 GAIA_NODISCARD inline bool
54 typed_query_args_match_query(const QueryInfo& queryInfo, [[maybe_unused]] core::func_type_list<T...>) {
55 if constexpr (sizeof...(T) > 0)
56 return queryInfo.template has_all<T...>();
57 else
58 return true;
59 }
60#endif
61
62 template <typename... T, typename Func, size_t... I>
63 inline void invoke_typed_query_args_by_id(
64 World& world, Entity entity, const Entity* pArgIds, Func& func, std::index_sequence<I...>) {
65 func(([&]() -> decltype(auto) {
66 using Arg = std::remove_cv_t<std::remove_reference_t<T>>;
67 if constexpr (std::is_same_v<Arg, Entity>)
68 return entity;
69 else if constexpr (std::is_lvalue_reference_v<T> && !std::is_const_v<std::remove_reference_t<T>>)
70 return world_query_entity_arg_by_id_raw<T>(world, entity, pArgIds[I]);
71 else
72 return world_query_entity_arg_by_id<T>(world, entity, pArgIds[I]);
73 }())...);
74 }
75
76 template <typename Func, typename... T>
77 inline void invoke_typed_query_args_by_id_erased(World& world, Entity entity, const Entity* pArgIds, void* pFunc) {
78 auto& func = *static_cast<Func*>(pFunc);
79 invoke_typed_query_args_by_id<T...>(world, entity, pArgIds, func, std::index_sequence_for<T...>{});
80 }
81
82 template <typename Func, typename... T>
83 GAIA_NODISCARD inline auto typed_invoke_inherited_ptr(core::func_type_list<T...>) {
84 return &invoke_typed_query_args_by_id_erased<Func, T...>;
85 }
86
87 inline void finish_typed_query_args_by_id(
88 World& world, Entity entity, const Entity* pArgIds, const bool* pWriteFlags, uint32_t argCnt) {
89 Entity seenTerms[ChunkHeader::MAX_COMPONENTS]{};
90 uint32_t seenCnt = 0;
91 const auto finish_term = [&](Entity term) {
92 GAIA_FOR(seenCnt) {
93 if (seenTerms[i] == term)
94 return;
95 }
96
97 seenTerms[seenCnt++] = term;
98 world_finish_write(world, term, entity);
99 };
100
101 GAIA_FOR(argCnt) {
102 if (pWriteFlags[i])
103 finish_term(pArgIds[i]);
104 }
105 }
106 } // namespace ecs
107} // namespace gaia
Definition world.h:174
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
static constexpr uint32_t MAX_COMPONENTS
Maximum number of components on archetype.
Definition chunk_header.h:53
Definition id.h:247
Definition query_adapter_typed.inl:16
Definition query_adapter_typed.inl:5