Gaia-ECS v1.0.0
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 {
7 template <typename T>
8 GAIA_NODISCARD inline Entity typed_query_raw_entity(World& world) {
9 using U = typename component_type_t<T>::Type;
10 static_assert(core::is_raw_v<U>, "Use raw types only");
11
12 const auto& desc = comp_cache_add<T>(world);
13 return desc.entity;
14 }
15
16 template <typename Rel, typename Tgt>
17 GAIA_NODISCARD inline Entity typed_query_pair_entity(World& world) {
18 using URel = typename component_type_t<Rel>::Type;
19 using UTgt = typename component_type_t<Tgt>::Type;
20 static_assert(core::is_raw_v<URel>, "Use raw relation types only");
21 static_assert(core::is_raw_v<UTgt>, "Use raw target types only");
22
23 const auto& descRel = comp_cache_add<Rel>(world);
24 const auto& descTgt = comp_cache_add<Tgt>(world);
25 return Pair(descRel.entity, descTgt.entity);
26 }
27
28 template <typename T>
29 GAIA_NODISCARD inline Entity typed_query_term_entity(World& world) {
30 using FT = typename component_type_t<T>::TypeFull;
31 if constexpr (is_pair<FT>::value)
32 return typed_query_pair_entity<typename FT::rel_type, typename FT::tgt_type>(world);
33 else
34 return typed_query_raw_entity<T>(world);
35 }
36
37 template <typename T>
38 GAIA_NODISCARD inline QueryTermOptions typed_query_term_options(QueryOpKind op, QueryTermOptions options) {
39 if (op != QueryOpKind::Not && op != QueryOpKind::Any && options.access == QueryAccess::None) {
40 constexpr auto isReadWrite = core::is_mut_v<T>;
41 options.access = isReadWrite ? QueryAccess::Write : QueryAccess::Read;
42 }
43 return options;
44 }
45
46 template <typename T>
47 inline QueryImpl& QueryImpl::all(const QueryTermOptions& options) {
48 return all(
49 typed_query_term_entity<T>(*m_storage.world()), typed_query_term_options<T>(QueryOpKind::All, options));
50 }
51
52 template <typename T>
53 inline QueryImpl& QueryImpl::all() {
54 return all<T>(QueryTermOptions{});
55 }
56
57 template <typename T>
58 inline QueryImpl& QueryImpl::any(const QueryTermOptions& options) {
59 return any(
60 typed_query_term_entity<T>(*m_storage.world()), typed_query_term_options<T>(QueryOpKind::Any, options));
61 }
62
63 template <typename T>
64 inline QueryImpl& QueryImpl::any() {
65 return any<T>(QueryTermOptions{});
66 }
67
68 template <typename T>
69 inline QueryImpl& QueryImpl::or_(const QueryTermOptions& options) {
70 return or_(
71 typed_query_term_entity<T>(*m_storage.world()), typed_query_term_options<T>(QueryOpKind::Or, options));
72 }
73
74 template <typename T>
75 inline QueryImpl& QueryImpl::or_() {
76 return or_<T>(QueryTermOptions{});
77 }
78
79 template <typename T>
80 inline QueryImpl& QueryImpl::no(const QueryTermOptions& options) {
81 return no(
82 typed_query_term_entity<T>(*m_storage.world()), typed_query_term_options<T>(QueryOpKind::Not, options));
83 }
84
85 template <typename T>
86 inline QueryImpl& QueryImpl::no() {
87 return no<T>(QueryTermOptions{});
88 }
89
90 template <typename T>
91 inline QueryImpl& QueryImpl::changed() {
92 return changed(typed_query_raw_entity<T>(*m_storage.world()));
93 }
94
95 template <typename T>
96 inline QueryImpl& QueryImpl::sort_by(TSortByFunc func) {
97 using UO = typename component_type_t<T>::TypeOriginal;
98 if constexpr (std::is_same_v<UO, Entity>)
99 return sort_by(EntityBad, func);
100 else
101 return sort_by(typed_query_raw_entity<T>(*m_storage.world()), func);
102 }
103
104 template <typename Rel, typename Tgt>
105 inline QueryImpl& QueryImpl::sort_by(TSortByFunc func) {
106 return sort_by(typed_query_pair_entity<Rel, Tgt>(*m_storage.world()), func);
107 }
108
109 template <typename Rel>
110 inline QueryImpl& QueryImpl::depth_order() {
111 return depth_order(typed_query_raw_entity<Rel>(*m_storage.world()));
112 }
113
114 template <typename Rel>
115 inline QueryImpl::OrderByTravView QueryImpl::order_by(TravOrder order) {
116 return order_by(typed_query_raw_entity<Rel>(*m_storage.world()), order);
117 }
118
119 template <typename T>
120 inline QueryImpl& QueryImpl::group_by(TGroupByFunc func) {
121 return group_by(typed_query_raw_entity<T>(*m_storage.world()), func);
122 }
123
124 template <typename Rel, typename Tgt>
125 inline QueryImpl& QueryImpl::group_by(TGroupByFunc func) {
126 return group_by(typed_query_pair_entity<Rel, Tgt>(*m_storage.world()), func);
127 }
128
129 template <typename Rel>
130 inline QueryImpl& QueryImpl::group_dep() {
131 return group_dep(typed_query_raw_entity<Rel>(*m_storage.world()));
132 }
133
134 template <typename T>
135 inline QueryImpl& QueryImpl::group_id() {
136 return group_id(typed_query_raw_entity<T>(*m_storage.world()));
137 }
139 } // namespace detail
140 } // namespace ecs
141} // namespace gaia
QueryImpl & sort_by(Entity entity, TSortByFunc func)
Sorts the query by the specified entity and function.
Definition query.h:5915
QueryImpl & or_()
Adds an OR typed term.
QueryImpl & any()
Adds an optional typed term.
QueryImpl & all()
Adds a required typed term.
GAIA_NODISCARD OrderByTravView order_by(Entity relation, TravOrder order)
Iterates matching entities in an explicit relation traversal order.
Definition query.h:5978
QueryImpl & group_dep()
Declares an explicit relation dependency for grouped cache invalidation. Useful for custom group_by c...
QueryImpl & no()
Adds an excluded typed term.
QueryImpl & group_id()
Selects the group to iterate over.
QueryImpl & changed()
Marks a typed term for changed() filtering.
QueryImpl & depth_order()
Orders cached query entries by fragmenting relation depth so iteration runs breadth-first top-down.
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:6020
static constexpr bool value
True when the type derives from the pair base.
Definition id.h:287