Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
EnumerateRange.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Templates/UnrealTemplate.h"
6
7#include <iterator>
8#include <type_traits>
9
10template <typename ElementType, typename SizeType = int32>
12{
13 TEnumerateRef(ElementType& InRef, SizeType InIndex)
14 : Ref (InRef)
15 , Index(InIndex)
16 {
17 }
18
19 SizeType GetIndex() const
20 {
21 return this->Index;
22 }
23
24 ElementType& operator*() const
25 {
26 return this->Ref;
27 }
28
29 ElementType* operator->() const
30 {
31 return &this->Ref;
32 }
33
35 {
36 return { Ref, Index };
37 }
38
39private:
41 SizeType Index;
42};
43
44template <typename ElementType, typename SizeType = int32>
45using TConstEnumerateRef = TEnumerateRef<const ElementType, SizeType>;
46
47namespace UE::Core::Private
48{
49 template <typename IteratorType, typename SizeType>
51 {
52 IteratorType Iterator;
53 SizeType Index = 0;
54
55 auto operator*() const -> TEnumerateRef<std::remove_reference_t<decltype(*this->Iterator)>, SizeType>
56 {
57 return { *this->Iterator, this->Index };
58 }
59
60 template <typename EndIteratorType>
61 bool operator!=(EndIteratorType End) const
62 {
63 return this->Iterator != End;
64 }
65
66 void operator++()
67 {
68 ++this->Iterator;
69 ++this->Index;
70 }
71 };
72}
73
74template <typename RangeType, typename SizeType>
76{
77 RangeType Range;
78
79 auto begin() const -> UE::Core::Private::TEnumerateIter<decltype(std::begin(Range)), SizeType>
80 {
81 return { std::begin(Range) };
82 }
83
84 auto end() const
85 {
86 return std::end(Range);
87 }
88};
89
90/**
91 * Allows iterating over a range while also keeping track of the current iteration index.
92 *
93 * Usage:
94 *
95 * for (TEnumerateRef<T> Elem : EnumerateRange(Container))
96 * {
97 * Elem->MemberFunctionOfT();
98 * FunctionTakingT(*Elem);
99 * int32 Index = Elem.GetIndex();
100 * }
101 *
102 * TEnumerateRef<const T> or TConstEnumerateRef<T> can be used for const iteration.
103 *
104 * Iterating over larger containers like TArray64 will require an explicit SizeType
105 * parameter: TEnumerateRef<T, int64>
106 */
107template <typename RangeType>
108auto EnumerateRange(RangeType&& Range) -> TEnumerateRange<RangeType, decltype(GetNum(Range))>
109{
110 return { (RangeType&&)Range };
111}
112template <typename T, SIZE_T N>
113TEnumerateRange<T(&)[N], int32> EnumerateRange(T(&Range)[N])
114{
115 static_assert(N <= MAX_int32, "Array size is not supported by Enumerate");
116 return { Range };
117}
auto EnumerateRange(RangeType &&Range) -> TEnumerateRange< RangeType, decltype(GetNum(Range))>
TEnumerateRange< T(&) EnumerateRange[N], int32 >(T(&Range)[N])
#define MAX_int32
Definition Vector.h:40
Definition json.hpp:4518
auto begin() const -> UE::Core::Private::TEnumerateIter< decltype(std::begin(Range)), SizeType >
auto end() const
ElementType & operator*() const
SizeType GetIndex() const
TEnumerateRef(ElementType &InRef, SizeType InIndex)
ElementType * operator->() const
bool operator!=(EndIteratorType End) const
auto operator*() const -> TEnumerateRef< std::remove_reference_t< decltype(*this->Iterator)>, SizeType >