MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
Concepts.h
Go to the documentation of this file.
1/** @noop License Comment
2 * @file
3 * @copyright
4 * This Source Code is subject to the terms of the Mozilla Public License, v2.0.
5 * If a copy of the MPL was not distributed with this file You can obtain one at
6 * https://mozilla.org/MPL/2.0/
7 *
8 * @author David Mórász
9 * @date 2025
10 */
11
12#pragma once
13
14#include "CoreMinimal.h"
15
16#include "Mcro/Concepts.h"
17#include "Mcro/FunctionTraits.h"
18
19namespace Mcro::Range
20{
21 using namespace Mcro::Concepts;
22 using namespace Mcro::FunctionTraits;
23
25 {
27 Forward,
29 };
30 enum class EIteratorStep
31 {
33 Single,
34 Jump,
36 };
37
38 template <typename L, typename R>
39 concept CHasEquals = requires (L& a, R& b) { a == b; };
40
41 template <typename L, typename R>
42 concept CHasNotEquals = requires (L& a, R& b) { a != b; };
43
44 template <typename L, typename R>
45 bool IteratorEquals(L const& l, R const& r)
46 {
47 if constexpr (CHasEquals<L, R>)
48 return l == r;
49 else return !(l != r);
50 }
51
52 /** @brief The most basic minimal iterator which can only proceed forward one element at a time */
53 template <typename T>
54 concept CBasicForwardIteratorBase = CPointer<T> || requires(T& i) { ++i; *i; };
55
56 template <typename T>
59 ;
60
61 template <typename T>
62 concept CHasMemberAccessOperator = requires(T& i) { i.operator->(); };
63
64 /** @brief Basic minimal iterator which can only proceed forward or backward one element at a time */
65 template <typename T>
66 concept CBasicBidirectionalIterator = CPointer<T> || (CBasicForwardIterator<T> && requires(T& i) { --i; });
67
68 /** @brief An iterator type which can natively proceed forward in arbitrarily large steps */
69 template <typename T>
70 concept CJumpForwardIterator = CPointer<T> || (CBasicForwardIterator<T> && requires(T& i) { i += 2; });
71
72 /** @brief An iterator type which can natively proceed forward in arbitrarily large steps and exposes a + operator */
73 template <typename T>
74 concept CJumpForwardPlusIterator = CPointer<T> || (CJumpForwardIterator<T> && requires(T& i) { { i + 2 } -> CConvertibleToDecayed<T>; });
75
76 /** @brief An iterator type which can natively seek its associated content in arbitrarily large steps */
77 template <typename T>
78 concept CRandomAccessIterator = CPointer<T> || (CJumpForwardIterator<T> && requires(T& i) { i -= 2; });
79
80 /**
81 * @brief
82 * An iterator type which can natively seek its associated content in arbitrarily large steps and exposes +- operators
83 */
84 template <typename T>
85 concept CRandomAccessPlusMinusIterator = CPointer<T> ||
86 (
89 && requires(T& i)
90 {
91 { i - 2 } -> CConvertibleToDecayed<T>;
92 }
93 );
94
95 /** @brief Get the direction given iterator is capable of */
96 template <typename T>
103 ;
104
105 /** @brief Get the maximum steps the given iterator can be seeking with */
106 template <typename T>
115 ;
116
117 /** @brief Assume an STL iterator category from input iterator */
118 template <typename T>
119 using TIteratorCategory = std::conditional_t<
121 std::random_access_iterator_tag,
122 std::conditional_t<
124 std::bidirectional_iterator_tag,
125 std::conditional_t<
127 std::forward_iterator_tag,
128 std::input_iterator_tag
129 >
130 >
131 >;
132
133 /**
134 * @brief
135 * Constraint given iterator to its best stepping capability.
136 *
137 * To constrain for "at least" method of capability use `CBasicForwardIterator` and refining concepts.
138 */
139 template <typename T, EIteratorStep Step>
141
142 /**
143 * @brief
144 * Constraint given iterator to its best directional capability.
145 *
146 * To constrain for "at least" method of capability use `CBasicForwardIterator` and refining concepts.
147 */
148 template <typename T, EIteratorDirection Direction>
150
151 /** @brief Constraint given iterator to its best capabilies both in stepping and in direction */
152 template <typename T, EIteratorDirection Direction, EIteratorStep Step>
154
155 /** @brief return the iterator's associated content type when they're dereferenced. */
156 template <CBasicForwardIterator T>
157 using TIteratorElementType = std::decay_t<decltype(*DeclVal<T>())>;
158
159 /** @brief return a range's associated content type determined by dereferencing their iterator. */
160 template <CRangeMember T>
162
163 template <typename T>
164 concept CUnrealRange = CRangeMember<T>
165 && requires(T& container, TRangeElementType<T> const& item)
166 {
167 container.Add(item);
168 };
169
170 template <typename T>
171 concept CStdDistanceCompatible = requires(T& l, T& r) { std::distance(l, r); };
172
173 template <typename T>
174 concept CHasGetIndex = requires(T& i) { i.GetIndex(); };
175
176 template <typename T>
177 concept CHasElementIndex = requires(T& i) { i.ElementIndex; };
178
179 template <typename T>
181 CTotallyOrdered<T>
184 ;
185
186 template <typename T>
187 concept CCountableRange = requires(T&& t) { size(t); };
188
189 template <typename Range>
190 concept CRangeOfTuples = CRangeMember<Range> && CTuple<TRangeElementType<Range>>;
191
192 template <typename Range, typename Function>
194 CRangeMember<Range> && CFunctionLike<Function>
195 && CTupleCompatibleWithFunction<TRangeElementType<Range>, Function>
196 ;
197}
This header exists because STL headers in Android doesn't define STL concepts (other than same_as whi...
size_t size(TArray< T, A > const &r)
Definition Range.h:101
Basic minimal iterator which can only proceed forward or backward one element at a time.
Definition Concepts.h:66
The most basic minimal iterator which can only proceed forward one element at a time.
Definition Concepts.h:54
Constraint given iterator to its best directional capability.
Definition Concepts.h:149
Constraint given iterator to its best stepping capability.
Definition Concepts.h:140
Constraint given iterator to its best capabilies both in stepping and in direction.
Definition Concepts.h:153
An iterator type which can natively proceed forward in arbitrarily large steps.
Definition Concepts.h:70
An iterator type which can natively proceed forward in arbitrarily large steps and exposes a + operat...
Definition Concepts.h:74
An iterator type which can natively seek its associated content in arbitrarily large steps.
Definition Concepts.h:78
An iterator type which can natively seek its associated content in arbitrarily large steps and expose...
Definition Concepts.h:85
constexpr EIteratorStep TIteratorStep
Get the maximum steps the given iterator can be seeking with.
Definition Concepts.h:107
std::decay_t< decltype(*DeclVal< T >())> TIteratorElementType
return the iterator's associated content type when they're dereferenced.
Definition Concepts.h:157
constexpr EIteratorDirection TIteratorDirection
Get the direction given iterator is capable of.
Definition Concepts.h:97
bool IteratorEquals(L const &l, R const &r)
Definition Concepts.h:45
TIteratorElementType< decltype(DeclVal< T >().begin())> TRangeElementType
return a range's associated content type determined by dereferencing their iterator.
Definition Concepts.h:161
std::conditional_t< CRandomAccessIterator< T >, std::random_access_iterator_tag, std::conditional_t< CBasicBidirectionalIterator< T >, std::bidirectional_iterator_tag, std::conditional_t< CBasicForwardIterator< T >, std::forward_iterator_tag, std::input_iterator_tag > > > TIteratorCategory
Assume an STL iterator category from input iterator.
Definition Concepts.h:119