Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
query_builder_typed.inl
1#pragma once
2
3namespace gaia {
4 namespace ecs {
5 namespace detail {
6 template <typename T>
7 GAIA_NODISCARD inline Entity typed_query_raw_entity(World& world) {
8 using U = typename component_type_t<T>::Type;
9 static_assert(core::is_raw_v<U>, "Use raw types only");
10
11 const auto& desc = comp_cache_add<T>(world);
12 return desc.entity;
13 }
14
15 template <typename Rel, typename Tgt>
16 GAIA_NODISCARD inline Entity typed_query_pair_entity(World& world) {
17 using URel = typename component_type_t<Rel>::Type;
18 using UTgt = typename component_type_t<Tgt>::Type;
19 static_assert(core::is_raw_v<URel>, "Use raw relation types only");
20 static_assert(core::is_raw_v<UTgt>, "Use raw target types only");
21
22 const auto& descRel = comp_cache_add<Rel>(world);
23 const auto& descTgt = comp_cache_add<Tgt>(world);
24 return Pair(descRel.entity, descTgt.entity);
25 }
26
27 template <typename T>
28 GAIA_NODISCARD inline Entity typed_query_term_entity(World& world) {
29 using FT = typename component_type_t<T>::TypeFull;
30 if constexpr (is_pair<FT>::value)
31 return typed_query_pair_entity<typename FT::rel_type, typename FT::tgt_type>(world);
32 else
33 return typed_query_raw_entity<T>(world);
34 }
35
36 template <typename T>
37 GAIA_NODISCARD inline QueryTermOptions typed_query_term_options(QueryOpKind op, QueryTermOptions options) {
38 if (op != QueryOpKind::Not && op != QueryOpKind::Any && options.access == QueryAccess::None) {
39 constexpr auto isReadWrite = core::is_mut_v<T>;
40 options.access = isReadWrite ? QueryAccess::Write : QueryAccess::Read;
41 }
42 return options;
43 }
44
45 template <typename T>
46 inline QueryImpl& QueryImpl::all(const QueryTermOptions& options) {
47 return all(
48 typed_query_term_entity<T>(*m_storage.world()), typed_query_term_options<T>(QueryOpKind::All, options));
49 }
50
51 template <typename T>
53 return all<T>(QueryTermOptions{});
54 }
55
56 template <typename T>
57 inline QueryImpl& QueryImpl::any(const QueryTermOptions& options) {
58 return any(
59 typed_query_term_entity<T>(*m_storage.world()), typed_query_term_options<T>(QueryOpKind::Any, options));
60 }
61
62 template <typename T>
64 return any<T>(QueryTermOptions{});
65 }
66
67 template <typename T>
68 inline QueryImpl& QueryImpl::or_(const QueryTermOptions& options) {
69 return or_(
70 typed_query_term_entity<T>(*m_storage.world()), typed_query_term_options<T>(QueryOpKind::Or, options));
71 }
72
73 template <typename T>
75 return or_<T>(QueryTermOptions{});
76 }
77
78 template <typename T>
79 inline QueryImpl& QueryImpl::no(const QueryTermOptions& options) {
80 return no(
81 typed_query_term_entity<T>(*m_storage.world()), typed_query_term_options<T>(QueryOpKind::Not, options));
82 }
83
84 template <typename T>
86 return no<T>(QueryTermOptions{});
87 }
88
89 template <typename T>
91 return changed(typed_query_raw_entity<T>(*m_storage.world()));
92 }
93
94 template <typename T>
95 inline QueryImpl& QueryImpl::sort_by(TSortByFunc func) {
96 using UO = typename component_type_t<T>::TypeOriginal;
97 if constexpr (std::is_same_v<UO, Entity>)
98 return sort_by(EntityBad, func);
99 else
100 return sort_by(typed_query_raw_entity<T>(*m_storage.world()), func);
101 }
102
103 template <typename Rel, typename Tgt>
104 inline QueryImpl& QueryImpl::sort_by(TSortByFunc func) {
105 return sort_by(typed_query_pair_entity<Rel, Tgt>(*m_storage.world()), func);
106 }
107
108 template <typename Rel>
110 return depth_order(typed_query_raw_entity<Rel>(*m_storage.world()));
111 }
112
113 template <typename Rel>
114 inline QueryImpl::OrderByTravView QueryImpl::order_by(TravOrder order) {
115 return order_by(typed_query_raw_entity<Rel>(*m_storage.world()), order);
116 }
117
118 template <typename T>
119 inline QueryImpl& QueryImpl::group_by(TGroupByFunc func) {
120 return group_by(typed_query_raw_entity<T>(*m_storage.world()), func);
121 }
122
123 template <typename Rel, typename Tgt>
124 inline QueryImpl& QueryImpl::group_by(TGroupByFunc func) {
125 return group_by(typed_query_pair_entity<Rel, Tgt>(*m_storage.world()), func);
126 }
127
128 template <typename Rel>
130 return group_dep(typed_query_raw_entity<Rel>(*m_storage.world()));
131 }
132
133 template <typename T>
135 return group_id(typed_query_raw_entity<T>(*m_storage.world()));
136 }
137 } // namespace detail
138 } // namespace ecs
139} // namespace gaia
Definition query.h:500
QueryImpl & sort_by(Entity entity, TSortByFunc func)
Sorts the query by the specified entity and function.
Definition query.h:5709
QueryImpl & or_()
Adds an OR typed term.
Definition query_builder_typed.inl:74
QueryImpl & any()
Adds an optional typed term.
Definition query_builder_typed.inl:63
QueryImpl & all()
Adds a required typed term.
Definition query_builder_typed.inl:52
GAIA_NODISCARD OrderByTravView order_by(Entity relation, TravOrder order)
Iterates matching entities in an explicit relation traversal order.
Definition query.h:5769
QueryImpl & group_dep()
Declares an explicit relation dependency for grouped cache invalidation. Useful for custom group_by c...
Definition query_builder_typed.inl:129
QueryImpl & no()
Adds an excluded typed term.
Definition query_builder_typed.inl:85
QueryImpl & group_id()
Selects the group to iterate over.
Definition query_builder_typed.inl:134
QueryImpl & changed()
Marks a typed term for changed() filtering.
Definition query_builder_typed.inl:90
QueryImpl & depth_order()
Orders cached query entries by fragmenting relation depth so iteration runs breadth-first top-down.
Definition query_builder_typed.inl:109
QueryImpl & group_by(Entity entity, TGroupByFunc func=group_by_func_default)
Organizes matching archetypes into groups according to the grouping function and entity....
Definition query.h:5811
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Additional options for query terms. This can be used to configure source lookup, traversal and access...
Definition query_common.h:271
QueryAccess access
Access mode for the term. When None, typed query terms infer read/write access from template mutabili...
Definition query_common.h:285
GAIA_NODISCARD World * world()
Returns the world associated with this query storage.
Definition query.h:389