ECSTASY
All in the name
Loading...
Searching...
No Matches
Select.hpp
Go to the documentation of this file.
1
12
13#ifndef ECSTASY_QUERY_SELECT_HPP_
14#define ECSTASY_QUERY_SELECT_HPP_
15
16#include <algorithm>
17#include <stddef.h>
18#include <tuple>
19
20#include "Query.hpp"
21#include "util/meta/Traits.hpp"
24
25namespace ecstasy::query
26{
39 template <Queryable... SelectedQueryables>
40 struct Select {
41 public:
43 using SelectedTuple = std::tuple<SelectedQueryables &...>;
44
45 private:
56 template <Queryable Q>
57 [[nodiscard]] constexpr static bool isQueryableSelected() noexcept
58 {
59 return util::meta::contains<Q, SelectedQueryables...>;
60 }
61
70 template <typename... Valids>
71 struct SorteredTie {
82 [[nodiscard]] static constexpr SelectedTuple sort(Valids &...valids) noexcept
83 {
84 return std::tie(std::forward<Valids &>(valids)...);
85 }
86
102 template <Queryable Q, Queryable... Qs>
103 [[nodiscard]] static constexpr SelectedTuple sort(Valids &...valids, Q &current, Qs &...lefts) noexcept
104 {
105 if constexpr (std::is_same_v<
106 typename util::meta::Traits<SelectedQueryables...>::template Nth<sizeof...(Valids)>,
107 Q>)
109 std::forward<Valids &>(valids)..., current, std::forward<Qs &>(lefts)...);
110 else
112 std::forward<Valids &>(valids)..., std::forward<Qs &>(lefts)..., current);
113 }
114 };
115
128 template <Queryable... Qs>
129 [[nodiscard]] constexpr static SelectedTuple tieQueryables(Qs &...queryables) noexcept
130 {
131 static_assert(util::meta::type_set_eq_v<std::tuple<SelectedQueryables...>, std::tuple<Qs...>>,
132 "Missing queryables in where clause");
133 if constexpr (std::is_same_v<std::tuple<SelectedQueryables...>, std::tuple<Qs...>>)
134 return std::tie(std::forward<Qs &>(queryables)...);
135 else
136 return SorteredTie<>::template sort(std::forward<Qs &>(queryables)...);
137 }
138
149 template <bool ContainsPivot, Queryable Pivot, Queryable... Lefts>
163 template <Queryable Q>
164 [[nodiscard]] constexpr static bool isQueryableRequired() noexcept
165 {
166 return isQueryableSelected<Q>() && !util::meta::contains<Q, Lefts...>;
167 }
168
180 [[nodiscard]] constexpr static SelectedTuple value(Lefts &...lefts, Pivot &pivot) noexcept
181 {
182 if constexpr (ContainsPivot)
183 return tieQueryables(std::forward<Lefts &>(lefts)..., pivot);
184 else {
185 (void)pivot;
186 return tieQueryables(std::forward<Lefts &>(lefts)...);
187 }
188 }
189
211 template <Queryable NextPivot, Queryable... Rights>
212 [[nodiscard]] constexpr static SelectedTuple value(
213 Lefts &...lefts, Pivot &pivot, NextPivot &nextPivot, Rights &...rights) noexcept
214 {
215 if constexpr (ContainsPivot)
216 return FilterQueryables<isQueryableRequired<NextPivot>(), NextPivot, Lefts..., Pivot>::value(
217 std::forward<Lefts &>(lefts)..., pivot, nextPivot, std::forward<Rights &>(rights)...);
218 else {
219 (void)pivot;
220 return FilterQueryables<isQueryableSelected<NextPivot>(), NextPivot, Lefts...>::value(
221 std::forward<Lefts &>(lefts)..., nextPivot, std::forward<Rights &>(rights)...);
222 }
223 }
224 };
225
238 template <Queryable... Queryables>
239 [[nodiscard]] constexpr static SelectedTuple filterQueryables(Queryables &...queryables) noexcept
240 {
241 return FilterQueryables<isQueryableSelected<typename util::meta::Traits<Queryables...>::First>(),
242 typename util::meta::Traits<Queryables...>::First>::value(std::forward<Queryables &>(queryables)...);
243 }
244
245 public:
264 template <typename Conditions = util::meta::Traits<>, Queryable FirstWhere, Queryable... Wheres>
265 static QueryImplementation<util::meta::Traits<SelectedQueryables...>, Conditions> where(
266 FirstWhere &firstWhere, Wheres &...wheres)
267 {
269 if constexpr (is_queryable_with_adjust_v<FirstWhere>
270 || std::disjunction_v<is_queryable_with_adjust<Wheres>...>) {
271 size_t maxSize = std::max({getQueryableMask(firstWhere).size(), getQueryableMask(wheres).size()...});
272
273 adjustMask(firstWhere, maxSize);
274 (adjustMask(wheres, maxSize), ...);
275 }
276
277 util::BitSet mask = (util::BitSet(getQueryableMask(firstWhere)) &= ... &= getQueryableMask(wheres));
278
279 return QueryImplementation<util::meta::Traits<SelectedQueryables...>, Conditions>(
280 mask, filterQueryables(firstWhere, wheres...));
281 }
282 };
283
284} // namespace ecstasy::query
285
286#endif /* !ECSTASY_QUERY_SELECT_HPP_ */
Query components presents in all given queryables.
Helper types for parameter packs.
Query components presents in all given queryables.
Definition Query.hpp:47
Mimics the API of std::bitset but with the dynamic properties of std::vector<bool>
Definition BitSet.hpp:35
constexpr std::size_t size() const noexcept
Definition BitSet.hpp:87
Defines a type that can be queried.
Checks if a type exists in a list of types.
T is_same_v
T max(T... args)
Namespace regrouping the internal ecstasy query system.
Definition Condition.hpp:18
constexpr const util::BitSet & getQueryableMask(const Q &queryable)
Get the mask of the queryable object.
constexpr void adjustMask(T &queryable, size_t maxSize) noexcept
Adjust the queryable mask if needed.
constexpr bool contains
Checks if the type T exists in the types Ts.
Definition contains.hpp:29
bool constexpr type_set_eq_v
Helper for type_set_eq.
static constexpr bool isQueryableRequired() noexcept
Test if the Queryable type Q must be kept in the resulting queryables.
Definition Select.hpp:164
static constexpr SelectedTuple value(Lefts &...lefts, Pivot &pivot, NextPivot &nextPivot, Rights &...rights) noexcept
Primary template recursively called until there is no nextPivot.
Definition Select.hpp:212
static constexpr SelectedTuple value(Lefts &...lefts, Pivot &pivot) noexcept
Primary template to finalize the queryables selection.
Definition Select.hpp:180
static constexpr SelectedTuple sort(Valids &...valids, Q &current, Qs &...lefts) noexcept
Recursively sort the queryables.
Definition Select.hpp:103
static constexpr SelectedTuple sort(Valids &...valids) noexcept
Final condition: all queryables are in the good order so tie them and return the tuple.
Definition Select.hpp:82
Advanced Query.
Definition Select.hpp:40
static constexpr SelectedTuple filterQueryables(Queryables &...queryables) noexcept
Isolate the given queryables to keep only the SelectedQueryables ones.
Definition Select.hpp:239
static constexpr SelectedTuple tieQueryables(Qs &...queryables) noexcept
Definition Select.hpp:129
static QueryImplementation< util::meta::Traits< SelectedQueryables... >, Conditions > where(FirstWhere &firstWhere, Wheres &...wheres)
Execute a AND query with all the given Queryables but returns only the one in SelectedQueryables.
Definition Select.hpp:265
static constexpr bool isQueryableSelected() noexcept
Definition Select.hpp:57
Empty parameter pack helper type.
Definition Traits.hpp:28
T tie(T... args)
Test if two tuple types contains the exact same types independently of the order.