UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
StridedView.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
10#include "HAL/PlatformString.h" // for INT64_FMT
11
12#include <type_traits>
13
14/*
15* TStridedView is similar to TArrayView, but allows flexible byte stride between elements.
16* Stride must be positive and a multiple of the element alignment.
17* Stride can be zero to duplicate the same element over the whole range.
18*
19* Example usage:
20*
21* struct FMyStruct
22* {
23* uint32 SomeData;
24* FVector Position;
25* };
26*
27* FVector ComputeMean(TStridedView<const FVector> Positions)
28* {
29* return Algo::Accumulate(Positions, FVector(0.0f)) / Positions.Num();
30* }
31*
32* FVector ComputeMeanPosition(TArrayView<const FMyStruct> Structs)
33* {
34* return ComputeMean(MakeStridedView(Structs, &FMyStruct::Position))
35* }
36*
37* See StridedViewTest.cpp for more examples.
38*
39*/
40
42template<typename InElementType, typename InSizeType>
44{
45public:
46
49
50 static_assert(std::is_signed_v<SizeType>, "TStridedView only supports signed index types");
51
52 [[nodiscard]] TStridedView() = default;
53
54 template <
55 typename OtherElementType
56 UE_REQUIRES(std::is_convertible_v<OtherElementType**, ElementType* const*>)
57 >
59 : FirstElementPtr(InFirstElementPtr)
60 , BytesBetweenElements(InBytesBetweenElements)
61 , NumElements(InNumElements)
62 {
63 check(NumElements >= 0);
64 check(BytesBetweenElements >= 0); // NOTE: Zero stride is valid to allow duplicating a single element.
65 check(BytesBetweenElements % alignof(ElementType) == 0);
66 }
67
68 template <
69 typename OtherElementType
70 UE_REQUIRES(std::is_convertible_v<OtherElementType**, ElementType* const*>)
71 >
73 : FirstElementPtr(nullptr)
74 , BytesBetweenElements(Other.GetStride())
75 , NumElements(Other.Num())
76 {
77 if (Other.Num())
78 {
79 FirstElementPtr = &Other[0];
80 }
81 }
82
84 {
85 return (Index >= 0) && (Index < NumElements);
86 }
87
89 {
90 return NumElements == 0;
91 }
92
94 {
95 return NumElements;
96 }
97
99 {
100 return BytesBetweenElements;
101 }
102
104 {
105 return *GetElementPtrUnsafe(Index);
106 }
107
109 {
110 return *GetElementPtr(Index);
111 }
112
114 {
117
119 {
120 ++Index;
121 return *this;
122 }
123
125 {
126 return *Owner->GetElementPtrUnsafe(Index);
127 }
128
129 [[nodiscard]] inline bool operator==(const FIterator& Other) const
130 {
131 return Owner == Other.Owner
132 && Index == Other.Index;
133 }
134
136 {
137 return !(*this == Other);
138 }
139 };
140
143 [[nodiscard]] UE_FORCEINLINE_HINT FIterator begin() const { return FIterator{ this, 0 }; }
144 [[nodiscard]] UE_FORCEINLINE_HINT FIterator end() const { return FIterator{ this, Num() }; }
145
146private:
147
148 UE_FORCEINLINE_HINT void RangeCheck(SizeType Index) const
149 {
150 checkf((Index >= 0) & (Index < NumElements), TEXT("Array index out of bounds: %" INT64_FMT " from an array of size %" INT64_FMT), int64(Index), int64(NumElements))
151 }
152
153 [[nodiscard]] inline ElementType* GetElementPtrUnsafe(SizeType Index) const
154 {
155 using ByteType = typename std::conditional_t<std::is_const_v<ElementType>, const uint8, uint8>;
156
157 ByteType* AsBytes = reinterpret_cast<ByteType*>(FirstElementPtr);
158 ElementType* AsElement = reinterpret_cast<ElementType*>(AsBytes + uint64(Index) * uint64(BytesBetweenElements));
159
160 return AsElement;
161 }
162
163 [[nodiscard]] inline ElementType* GetElementPtr(SizeType Index) const
164 {
165 RangeCheck(Index);
166 return GetElementPtrUnsafe(Index);
167 }
168
169private:
170
171 ElementType* FirstElementPtr = nullptr;
172 SizeType BytesBetweenElements = 0;
173 SizeType NumElements = 0;
174};
175
176template <typename ElementType>
178{
179 return TStridedView<ElementType>(BytesBetweenElements, FirstElement, Count);
180}
181
182template <typename ElementType>
184{
185 return TConstStridedView<ElementType>(BytesBetweenElements, FirstElement, Count);
186}
187
188template <typename BaseStructureType, typename DerivedStructureType>
190{
191 static_assert(std::is_base_of_v<BaseStructureType, DerivedStructureType>, "Expecting derived structure type");
193}
194
195template <typename BaseStructureType, typename DerivedStructureType>
197{
198 static_assert(std::is_base_of_v<BaseStructureType, DerivedStructureType>, "Expecting derived structure type");
200}
201
202template <typename StructureType>
207
208template <typename StructureType>
213
214template <
215 typename StructuredRangeType,
216 typename ElementType,
217 typename StructureType,
218 decltype(GetData(std::declval<StructuredRangeType>()))* = nullptr,
219 decltype(GetNum(std::declval<StructuredRangeType>()))* = nullptr
220>
225
226template <
227 typename StructuredRangeType,
228 typename ElementType,
229 typename StructureType,
230 decltype(GetData(std::declval<StructuredRangeType>()))* = nullptr,
231 decltype(GetNum(std::declval<StructuredRangeType>()))* = nullptr
232>
237
238template <typename StructuredRangeType>
244
245template <typename StructuredRangeType>
#define INT64_FMT
Definition AndroidPlatformString.h:59
constexpr auto MakeArrayView(OtherRangeType &&Other)
Definition ArrayView.h:873
#define check(expr)
Definition AssertionMacros.h:314
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define UE_REQUIRES(...)
Definition Requires.h:86
TStridedView< BaseStructureType > MakeStridedViewOfBase(TArrayView< DerivedStructureType > StructuredView)
Definition StridedView.h:189
TStridedView< ElementType > MakeStridedView(int32 BytesBetweenElements, ElementType *FirstElement, int32 Count)
Definition StridedView.h:177
TConstStridedView< ElementType > MakeConstStridedView(int32 BytesBetweenElements, const ElementType *FirstElement, int32 Count)
Definition StridedView.h:183
TConstStridedView< BaseStructureType > MakeConstStridedViewOfBase(TConstArrayView< DerivedStructureType > StructuredView)
Definition StridedView.h:196
auto GetNum(const TStringConversion< Converter, DefaultConversionSize > &Conversion) -> decltype(Conversion.Length())
Definition StringConv.h:808
auto GetData(const TStringConversion< Converter, DefaultConversionSize > &Conversion) -> decltype(Conversion.Get())
Definition StringConv.h:802
uint8_t uint8
Definition binka_ue_file_header.h:8
Definition ArrayView.h:139
Definition StridedView.h:44
UE_FORCEINLINE_HINT bool IsEmpty() const
Definition StridedView.h:88
UE_FORCEINLINE_HINT SizeType GetStride() const
Definition StridedView.h:98
UE_FORCEINLINE_HINT FIterator begin() const
Definition StridedView.h:143
InElementType ElementType
Definition StridedView.h:47
TStridedView(SizeType InBytesBetweenElements, OtherElementType *InFirstElementPtr, SizeType InNumElements)
Definition StridedView.h:58
InSizeType SizeType
Definition StridedView.h:48
UE_FORCEINLINE_HINT bool IsValidIndex(SizeType Index) const
Definition StridedView.h:83
UE_FORCEINLINE_HINT FIterator end() const
Definition StridedView.h:144
TStridedView()=default
UE_FORCEINLINE_HINT ElementType & operator[](SizeType Index) const
Definition StridedView.h:108
TStridedView(const TStridedView< OtherElementType, SizeType > &Other)
Definition StridedView.h:72
UE_FORCEINLINE_HINT ElementType & GetUnsafe(SizeType Index) const
Definition StridedView.h:103
UE_FORCEINLINE_HINT SizeType Num() const
Definition StridedView.h:93
ByteType
Definition PacketView.h:38
U16 Index
Definition radfft.cpp:71
Definition StridedView.h:114
UE_FORCEINLINE_HINT bool operator!=(const FIterator &Other) const
Definition StridedView.h:135
const TStridedView * Owner
Definition StridedView.h:115
SizeType Index
Definition StridedView.h:116
bool operator==(const FIterator &Other) const
Definition StridedView.h:129
UE_FORCEINLINE_HINT ElementType & operator*()
Definition StridedView.h:124
UE_FORCEINLINE_HINT FIterator & operator++()
Definition StridedView.h:118