EnTT 3.14.0
Loading...
Searching...
No Matches
range.hpp
1#ifndef ENTT_META_RANGE_HPP
2#define ENTT_META_RANGE_HPP
3
4#include <cstddef>
5#include <iterator>
6#include <utility>
7#include "../core/fwd.hpp"
8#include "../core/iterator.hpp"
9#include "context.hpp"
10
11namespace entt {
12
14namespace internal {
15
16struct meta_base_node;
17
18template<typename Type, typename It>
19struct meta_range_iterator final {
20 using value_type = std::pair<id_type, Type>;
21 using pointer = input_iterator_pointer<value_type>;
22 using reference = value_type;
23 using difference_type = std::ptrdiff_t;
24 using iterator_category = std::input_iterator_tag;
25 using iterator_concept = std::random_access_iterator_tag;
26
27 constexpr meta_range_iterator() noexcept
28 : it{},
29 ctx{} {}
30
31 constexpr meta_range_iterator(const meta_ctx &area, const It iter) noexcept
32 : it{iter},
33 ctx{&area} {}
34
35 constexpr meta_range_iterator &operator++() noexcept {
36 return ++it, *this;
37 }
38
39 constexpr meta_range_iterator operator++(int) noexcept {
40 meta_range_iterator orig = *this;
41 return ++(*this), orig;
42 }
43
44 constexpr meta_range_iterator &operator--() noexcept {
45 return --it, *this;
46 }
47
48 constexpr meta_range_iterator operator--(int) noexcept {
49 meta_range_iterator orig = *this;
50 return operator--(), orig;
51 }
52
53 constexpr meta_range_iterator &operator+=(const difference_type value) noexcept {
54 it += value;
55 return *this;
56 }
57
58 constexpr meta_range_iterator operator+(const difference_type value) const noexcept {
59 meta_range_iterator copy = *this;
60 return (copy += value);
61 }
62
63 constexpr meta_range_iterator &operator-=(const difference_type value) noexcept {
64 return (*this += -value);
65 }
66
67 constexpr meta_range_iterator operator-(const difference_type value) const noexcept {
68 return (*this + -value);
69 }
70
71 [[nodiscard]] constexpr reference operator[](const difference_type value) const noexcept {
72 if constexpr(std::is_same_v<It, typename decltype(meta_context::value)::const_iterator>) {
73 return {it[value].first, Type{*ctx, it[value].second}};
74 } else if constexpr(std::is_same_v<typename std::iterator_traits<It>::value_type, meta_base_node>) {
75 return {it[value].type, Type{*ctx, it[value]}};
76 } else {
77 return {it[value].id, Type{*ctx, it[value]}};
78 }
79 }
80
81 [[nodiscard]] constexpr pointer operator->() const noexcept {
82 return operator*();
83 }
84
85 [[nodiscard]] constexpr reference operator*() const noexcept {
86 return operator[](0);
87 }
88
89 template<typename... Args>
90 friend constexpr std::ptrdiff_t operator-(const meta_range_iterator<Args...> &, const meta_range_iterator<Args...> &) noexcept;
91
92 template<typename... Args>
93 friend constexpr bool operator==(const meta_range_iterator<Args...> &, const meta_range_iterator<Args...> &) noexcept;
94
95 template<typename... Args>
96 friend constexpr bool operator<(const meta_range_iterator<Args...> &, const meta_range_iterator<Args...> &) noexcept;
97
98private:
99 It it;
100 const meta_ctx *ctx;
101};
102
103template<typename... Args>
104[[nodiscard]] constexpr std::ptrdiff_t operator-(const meta_range_iterator<Args...> &lhs, const meta_range_iterator<Args...> &rhs) noexcept {
105 return lhs.it - rhs.it;
106}
107
108template<typename... Args>
109[[nodiscard]] constexpr bool operator==(const meta_range_iterator<Args...> &lhs, const meta_range_iterator<Args...> &rhs) noexcept {
110 return lhs.it == rhs.it;
111}
112
113template<typename... Args>
114[[nodiscard]] constexpr bool operator!=(const meta_range_iterator<Args...> &lhs, const meta_range_iterator<Args...> &rhs) noexcept {
115 return !(lhs == rhs);
116}
117
118template<typename... Args>
119[[nodiscard]] constexpr bool operator<(const meta_range_iterator<Args...> &lhs, const meta_range_iterator<Args...> &rhs) noexcept {
120 return lhs.it < rhs.it;
121}
122
123template<typename... Args>
124[[nodiscard]] constexpr bool operator>(const meta_range_iterator<Args...> &lhs, const meta_range_iterator<Args...> &rhs) noexcept {
125 return rhs < lhs;
126}
127
128template<typename... Args>
129[[nodiscard]] constexpr bool operator<=(const meta_range_iterator<Args...> &lhs, const meta_range_iterator<Args...> &rhs) noexcept {
130 return !(lhs > rhs);
131}
132
133template<typename... Args>
134[[nodiscard]] constexpr bool operator>=(const meta_range_iterator<Args...> &lhs, const meta_range_iterator<Args...> &rhs) noexcept {
135 return !(lhs < rhs);
136}
137
138} // namespace internal
146template<typename Type, typename It>
148
149} // namespace entt
150
151#endif
EnTT default namespace.
Definition dense_map.hpp:22
constexpr bool operator<=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator<(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr type_list< Type..., Other... > operator+(type_list< Type... >, type_list< Other... >)
Concatenates multiple type lists.
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator>=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator>(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Utility class to create an iterable object from a pair of iterators.
Definition iterator.hpp:141