stellarlib 0.1.0
Loading...
Searching...
No Matches
query.hpp
1/* clang-format off */
2
3/*
4 stellarlib
5 Copyright (C) 2025-2026 Domán Zana
6
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
10
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
14
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software
17 in a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source distribution.
22*/
23
24#ifndef STELLARLIB_ECS_QUERY_HPP
25#define STELLARLIB_ECS_QUERY_HPP
26
27#include <functional>
28#include <iterator>
29#include <utility>
30
31namespace stellarlib::ecs::internal
32{
33template <typename Query>
34class query final
35{
36public:
37 [[nodiscard]]
38 constexpr query(Query &&query, const std::function<void ()> &callback) noexcept
39 : _query{std::move(query)}
40 , _callback{callback}
41 {}
42
43 [[nodiscard]]
44 constexpr query(const query &) noexcept = delete;
45
46 [[nodiscard]]
47 constexpr query(query &&) noexcept = delete;
48
49 constexpr auto operator=(const query &) noexcept
50 -> query & = delete;
51
52 constexpr auto operator=(query &&) noexcept
53 -> query & = delete;
54
55 constexpr ~query() noexcept
56 {
57 _callback();
58 }
59
60 [[nodiscard]]
61 constexpr auto size() noexcept
62 {
63 return std::ranges::distance(_query);
64 }
65
66 [[nodiscard]]
67 constexpr auto begin() noexcept
68 {
69 return std::ranges::begin(_query);
70 }
71
72 [[nodiscard]]
73 constexpr auto end() noexcept
74 {
75 return std::ranges::end(_query);
76 }
77
78private:
79 Query _query;
80 const std::function<void ()> &_callback;
81};
82
83template <typename Query>
84query(Query &&, const std::function<void ()> &)
85 -> query<Query>;
86}
87
88#endif