feat(containers): add TArrayView and use TObserverPtr instead of raw pointer

This commit is contained in:
_Redstone_c_ 2023-02-26 21:55:36 +08:00
parent 343ff8d240
commit c8650b4aa5
8 changed files with 526 additions and 113 deletions

View File

@ -13,6 +13,7 @@ void TestContainers()
{
TestArray();
TestStaticArray();
TestArrayView();
}
NAMESPACE_UNNAMED_BEGIN
@ -178,6 +179,68 @@ void TestStaticArray()
}
}
void TestArrayView()
{
{
int32 ArrayA[] = { 0, 0, 0, 0 };
TStaticArray ArrayB = { 4, 4, 4, 4 };
TArray ArrayC = { 0, 1, 2, 3 };
TArrayView<int32, 0> ViewA;
TArrayView<int32, 4> ViewB(ArrayA);
TArrayView<int32, 4> ViewC(ArrayB);
TArrayView<int32, 4> ViewD(ViewC);
TArrayView<int32, 4> ViewE(MoveTemp(ViewB));
TArrayView<int32, 4> ViewF(ArrayC);
TArrayView<int32> ViewG;
TArrayView<int32> ViewH;
TArrayView<int32> ViewI;
ViewG = ViewD;
ViewH = MoveTemp(ViewE);
ViewI = ArrayC;
always_check(ViewC == ArrayB);
always_check(ViewD == ArrayB);
always_check(ViewG == ArrayB);
always_check(ViewF == ArrayC);
always_check(ViewI == ArrayC);
}
{
int32 Array[] = { 0, 1, 2, 3 };
TArrayView<int32, 4> View = Array;
int32 First2[] = { 0, 1 };
always_check(View.First<2>() == First2);
always_check(View.First(2) == First2);
int32 Last2[] = { 2, 3 };
always_check(View.Last<2>() == Last2);
always_check(View.Last(2) == Last2);
int32 Subview2[] = { 1, 2 };
always_check((View.Subview<1, 2>() == Subview2));
always_check((View.Subview(1, 2) == Subview2));
}
{
int32 Array[] = { 0, 1, 2, 3 };
TArrayView<int32, 4> View = Array;
always_check(View.Num() == 4);
always_check(View.NumBytes() == 16);
TArrayView ViewBytes = View.AsBytes();
always_check(ViewBytes.Num() == 16);
always_check(ViewBytes.NumBytes() == 16);
}
}
NAMESPACE_END(Testing)
NAMESPACE_MODULE_END(Utility)

View File

@ -139,8 +139,7 @@ public:
NODISCARD friend FORCEINLINE ptrdiff operator-(const TArrayIterator& LHS, const TArrayIterator& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; }
NODISCARD FORCEINLINE explicit operator ElementType*() requires (!CConst<ElementType>) { CheckThis(); return Pointer; }
NODISCARD FORCEINLINE explicit operator const ElementType*() const { CheckThis(); return Pointer; }
NODISCARD FORCEINLINE explicit operator TObserverPtr<ElementType[]>() const { CheckThis(); return TObserverPtr<ElementType[]>(Pointer); }
private:

View File

@ -0,0 +1,436 @@
#pragma once
#include "CoreTypes.h"
#include "Templates/Utility.h"
#include "Templates/TypeHash.h"
#include "Templates/Container.h"
#include "Containers/Iterator.h"
#include "TypeTraits/TypeTraits.h"
#include "Miscellaneous/Compare.h"
#include "Memory/ObserverPointer.h"
#include "Miscellaneous/AssertionMacros.h"
NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
template <CObject T, size_t InExtent>
class TArrayView;
NAMESPACE_PRIVATE_BEGIN
template <typename T>
class TArrayViewIterator
{
public:
using ElementType = T;
FORCEINLINE constexpr TArrayViewIterator() = default;
# if DO_CHECK
FORCEINLINE constexpr TArrayViewIterator(const TArrayViewIterator<TRemoveConst<ElementType>>& InValue) requires (CConst<ElementType>)
: Pointer(InValue.Pointer), FirstSentinel(InValue.FirstSentinel), EndSentinel(InValue.EndSentinel)
{ }
# else
FORCEINLINE constexpr TArrayViewIterator(const TArrayViewIterator<TRemoveConst<ElementType>>& InValue) requires (CConst<ElementType>)
: Pointer(InValue.Pointer)
{ }
# endif
FORCEINLINE constexpr TArrayViewIterator(const TArrayViewIterator&) = default;
FORCEINLINE constexpr TArrayViewIterator(TArrayViewIterator&&) = default;
FORCEINLINE constexpr TArrayViewIterator& operator=(const TArrayViewIterator&) = default;
FORCEINLINE constexpr TArrayViewIterator& operator=(TArrayViewIterator&&) = default;
NODISCARD friend FORCEINLINE constexpr bool operator==(const TArrayViewIterator& LHS, const TArrayViewIterator& RHS) { return LHS.Pointer == RHS.Pointer; }
NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const TArrayViewIterator & LHS, const TArrayViewIterator & RHS) { return LHS.Pointer <=> RHS.Pointer; }
NODISCARD FORCEINLINE constexpr ElementType& operator*() const { CheckThis(true); return *Pointer; }
NODISCARD FORCEINLINE constexpr ElementType* operator->() const { CheckThis(true); return Pointer; }
NODISCARD FORCEINLINE constexpr ElementType& operator[](ptrdiff Index) const { TArrayViewIterator Temp = *this + Index; return *Temp; }
FORCEINLINE constexpr TArrayViewIterator& operator++() { ++Pointer; CheckThis(); return *this; }
FORCEINLINE constexpr TArrayViewIterator& operator--() { --Pointer; CheckThis(); return *this; }
FORCEINLINE constexpr TArrayViewIterator operator++(int) { TArrayViewIterator Temp = *this; ++Pointer; CheckThis(); return Temp; }
FORCEINLINE constexpr TArrayViewIterator operator--(int) { TArrayViewIterator Temp = *this; --Pointer; CheckThis(); return Temp; }
FORCEINLINE constexpr TArrayViewIterator& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; }
FORCEINLINE constexpr TArrayViewIterator& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; }
NODISCARD friend FORCEINLINE constexpr TArrayViewIterator operator+(TArrayViewIterator Iter, ptrdiff Offset) { TArrayViewIterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD friend FORCEINLINE constexpr TArrayViewIterator operator+(ptrdiff Offset, TArrayViewIterator Iter) { TArrayViewIterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD FORCEINLINE constexpr TArrayViewIterator operator-(ptrdiff Offset) const { TArrayViewIterator Temp = *this; Temp -= Offset; return Temp; }
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const TArrayViewIterator& LHS, const TArrayViewIterator& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; }
NODISCARD FORCEINLINE constexpr explicit operator TObserverPtr<ElementType[]>() const { CheckThis(); return TObserverPtr<ElementType[]>(Pointer); }
private:
ElementType* Pointer = nullptr;
# if DO_CHECK
ElementType* FirstSentinel = nullptr;
ElementType* EndSentinel = nullptr;
# endif
# if DO_CHECK
FORCEINLINE constexpr TArrayViewIterator(ElementType* InPointer, ElementType* InFirstSentinel, ElementType* InEndSentinel)
: Pointer(InPointer), FirstSentinel(InFirstSentinel), EndSentinel(InEndSentinel)
{ }
# else
FORCEINLINE constexpr TArrayViewIterator(ElementType* InPointer, ElementType* InFirstSentinel, ElementType* InEndSentinel)
: Pointer(InPointer)
{ }
# endif
FORCEINLINE constexpr void CheckThis(bool bExceptEnd = false) const
{
check_code
({
const bool bInLegalRange = FirstSentinel && EndSentinel && FirstSentinel <= Pointer && Pointer <= EndSentinel;
const bool bIsDereferenceable = Pointer != EndSentinel;
checkf(bInLegalRange && (!bExceptEnd || bIsDereferenceable), TEXT("Read access violation. Please check IsValidIterator()."));
});
}
template <typename U>
friend class TArrayViewIterator;
template <CObject U, size_t InExtent>
friend class NAMESPACE_REDCRAFT::TArrayView;
};
template <bool bEnable>
struct TEnableArrayNum { size_t ArrayNum; };
template <>
struct TEnableArrayNum<false> { size_t ArrayNum; };
NAMESPACE_PRIVATE_END
template <CObject T, size_t N>
struct TStaticArray;
template <CObject T, typename A> requires (!CConst<T> && CDestructible<T> && CInstantiableAllocator<A>)
class TArray;
inline constexpr size_t DynamicExtent = INDEX_NONE;
/**
* The class template TArrayView describes an object that can refer to a contiguous sequence of objects with the first element of
* the sequence at position zero. A TArrayView can either have a static extent, in which case the number of elements in the sequence
* is known at compile-time and encoded in the type, or a dynamic extent.
*/
template <CObject T, size_t InExtent = DynamicExtent>
class TArrayView final : private NAMESPACE_PRIVATE::TEnableArrayNum<InExtent == DynamicExtent>
{
private:
using Impl = NAMESPACE_PRIVATE::TEnableArrayNum<InExtent == DynamicExtent>;
public:
using ElementType = T;
using Iterator = NAMESPACE_PRIVATE::TArrayViewIterator<ElementType>;
using ReverseIterator = TReverseIterator<Iterator>;
static_assert(CContiguousIterator<Iterator>);
static constexpr size_t Extent = InExtent;
/** Constructs an empty array view. */
FORCEINLINE constexpr TArrayView() requires (Extent == 0 || Extent == DynamicExtent) = default;
/** Constructs an array view that is a view over the range ['InFirst', 'InFirst' + 'Count'). */
template <CContiguousIterator I> requires (CConvertibleTo<TIteratorElementType<I>(*)[], ElementType(*)[]>)
FORCEINLINE constexpr explicit (Extent != DynamicExtent) TArrayView(I InFirst, size_t InCount) : Pointer(static_cast<TObserverPtr<TIteratorElementType<I>[]>>(InFirst))
{
checkf(Extent == DynamicExtent || Extent == InCount, TEXT("Illegal range count. Please check InCount."));
if constexpr (Extent == DynamicExtent)
{
Impl::ArrayNum = InCount;
}
}
/** Constructs an array view that is a view over the range ['InFirst', 'InLast'). */
template <CContiguousIterator I, CSizedSentinelFor<I> S> requires (CConvertibleTo<TIteratorElementType<I>(*)[], ElementType(*)[]>)
FORCEINLINE constexpr explicit (Extent != DynamicExtent) TArrayView(I InFirst, S InLast) : Pointer(static_cast<TObserverPtr<TIteratorElementType<I>[]>>(InFirst))
{
checkf(Extent == DynamicExtent || Extent == InLast - InFirst, TEXT("Illegal range iterator. Please check InLast - InFirst."));
if constexpr (Extent == DynamicExtent)
{
Impl::ArrayNum = InLast - InFirst;
}
}
/** Constructs an array view that is a view over the array 'InArray'. */
template <size_t N> requires (Extent == DynamicExtent || N == Extent)
FORCEINLINE constexpr TArrayView(ElementType(&InArray)[N]) : Pointer(InArray)
{
if constexpr (Extent == DynamicExtent)
{
Impl::ArrayNum = N;
}
}
/** Constructs an array view that is a view over the array 'InArray'. */
template <typename U, size_t N> requires (CConvertibleTo<U(*)[], ElementType(*)[]>)
FORCEINLINE constexpr TArrayView(TStaticArray<U, N>& InArray) : TArrayView(InArray.GetData().Get(), InArray.Num()) { }
/** Constructs an array view that is a view over the array 'InArray'. */
template <typename U, size_t N> requires (CConvertibleTo<U(*)[], ElementType(*)[]>)
FORCEINLINE constexpr TArrayView(const TStaticArray<U, N>& InArray) : TArrayView(InArray.GetData().Get(), InArray.Num()) { }
template <typename U, size_t N>
FORCEINLINE constexpr TArrayView(const TStaticArray<U, N>&&) = delete;
/** Constructs an array view that is a view over the array 'InArray'. */
template <typename U> requires (CConvertibleTo<U(*)[], ElementType(*)[]>)
FORCEINLINE constexpr TArrayView(TArray<U>& InArray) : TArrayView(InArray.GetData().Get(), InArray.Num()) { }
/** Constructs an array view that is a view over the array 'InArray'. */
template <typename U> requires (CConvertibleTo<U(*)[], ElementType(*)[]>)
FORCEINLINE constexpr TArrayView(const TArray<U>& InArray) : TArrayView(InArray.GetData().Get(), InArray.Num()) { }
template <typename U>
FORCEINLINE constexpr TArrayView(const TArray<U>&&) = delete;
/** Converting constructor from another array view 'InValue'. */
template <typename U, size_t N> requires ((Extent == DynamicExtent || N == DynamicExtent || N == Extent) && CConvertibleTo<U(*)[], ElementType(*)[]>)
FORCEINLINE constexpr explicit (Extent != DynamicExtent && N == DynamicExtent) TArrayView(TArrayView<U, N> InValue) : Pointer(InValue.GetData())
{
checkf(Extent == DynamicExtent || Extent == InValue.Num(), TEXT("Illegal view extent. Please check InValue.Num()."));
if constexpr (Extent == DynamicExtent)
{
Impl::ArrayNum = InValue.Num();
}
}
/** Defaulted copy constructor copies the size and data pointer. */
FORCEINLINE constexpr TArrayView(const TArrayView&) = default;
/** Assigns other to *this. This defaulted assignment operator performs a shallow copy of the data pointer and the size. */
FORCEINLINE constexpr TArrayView& operator=(const TArrayView&) noexcept = default;
/** Compares the contents of two array views. */
NODISCARD friend constexpr bool operator==(TArrayView LHS, TArrayView RHS) requires (CWeaklyEqualityComparable<ElementType>)
{
if (LHS.Num() != RHS.Num()) return false;
Iterator LHSIter = LHS.Begin();
Iterator RHSIter = RHS.Begin();
while (LHSIter != LHS.End())
{
if (*LHSIter != *RHSIter) return false;
++LHSIter;
++RHSIter;
}
check(RHSIter == RHS.End());
return true;
}
/** Compares the contents of two array views. */
NODISCARD friend constexpr auto operator<=>(TArrayView LHS, TArrayView RHS) requires (CSynthThreeWayComparable<ElementType>)
{
using OrderingType = TSynthThreeWayResult<ElementType>;
if (LHS.Num() < RHS.Num()) return OrderingType::less;
if (LHS.Num() > RHS.Num()) return OrderingType::greater;
Iterator LHSIter = LHS.Begin();
Iterator RHSIter = RHS.Begin();
while (LHSIter != LHS.End())
{
TSynthThreeWayResult<ElementType> Ordering = SynthThreeWayCompare(*LHSIter, *RHSIter);
if (Ordering != OrderingType::equivalent) return Ordering;
++LHSIter;
++RHSIter;
}
check(RHSIter == RHS.End());
return OrderingType::equivalent;
}
/** Obtains an array view that is a view over the first 'Count' elements of this array view. */
template <size_t Count> requires (Extent == DynamicExtent || Extent >= Count)
NODISCARD FORCEINLINE constexpr TArrayView<ElementType, Count> First() const
{
checkf(Count <= Num(), TEXT("Illegal subview range. Please check Count."));
return TArrayView<ElementType, Count>(Begin(), Count);
}
/** Obtains an array view that is a view over the first 'Count' elements of this array view. */
NODISCARD FORCEINLINE constexpr TArrayView<ElementType, DynamicExtent> First(size_t Count) const
{
checkf(Count <= Num(), TEXT("Illegal subview range. Please check Count."));
return TArrayView<ElementType, DynamicExtent>(Begin(), Count);
}
/** Obtains an array view that is a view over the last 'Count' elements of this array view. */
template <size_t Count> requires (Extent == DynamicExtent || Extent >= Count)
NODISCARD FORCEINLINE constexpr TArrayView<ElementType, Count> Last() const
{
checkf(Count <= Num(), TEXT("Illegal subview range. Please check Count."));
return TArrayView<ElementType, Count>(End() - Count, Count);
}
/** Obtains an array view that is a view over the last 'Count' elements of this array view. */
NODISCARD FORCEINLINE constexpr TArrayView<ElementType, DynamicExtent> Last(size_t Count) const
{
checkf(Count <= Num(), TEXT("Illegal subview range. Please check Count."));
return TArrayView<ElementType, DynamicExtent>(End() - Count, Count);
}
/** Obtains an array view that is a view over the 'Count' elements of this array view starting at 'Offset'. */
template <size_t Offset, size_t Count = DynamicExtent> requires (Extent == DynamicExtent || Count == DynamicExtent || Extent >= Offset + Count)
NODISCARD FORCEINLINE constexpr auto Subview() const
{
checkf(Offset <= Num() && (Count == DynamicExtent || Offset + Count <= Num()), TEXT("Illegal subview range. Please check Offset and Count."));
constexpr size_t SubviewExtent = Count != DynamicExtent ? Count : (Extent != DynamicExtent ? Extent - Offset : DynamicExtent);
if constexpr (Count != DynamicExtent)
{
return TArrayView<ElementType, SubviewExtent>(Begin() + Offset, Count);
}
else
{
return TArrayView<ElementType, SubviewExtent>(Begin() + Offset, Num() - Offset);
}
}
/** Obtains an array view that is a view over the 'Count' elements of this array view starting at 'Offset'. */
NODISCARD FORCEINLINE constexpr auto Subview(size_t Offset, size_t Count = DynamicExtent) const
{
checkf(Offset <= Num() && (Count == DynamicExtent || Offset + Count <= Num()), TEXT("Illegal subview range. Please check Offset and Count."));
if (Count != DynamicExtent)
{
return TArrayView<ElementType, DynamicExtent>(Begin() + Offset, Count);
}
else
{
return TArrayView<ElementType, DynamicExtent>(Begin() + Offset, Num() - Offset);
}
}
/** Obtains an array view to the object representation of the elements of the array view. */
NODISCARD FORCEINLINE constexpr auto AsBytes()
{
constexpr size_t BytesExtent = Extent != DynamicExtent ? sizeof(ElementType) * Extent : DynamicExtent;
if constexpr (!CConst<ElementType>)
{
return TArrayView<uint8, BytesExtent>(reinterpret_cast<uint8*>(GetData().Get()), NumBytes());
}
else
{
return TArrayView<const uint8, BytesExtent>(reinterpret_cast<const uint8*>(GetData().Get()), NumBytes());
}
}
/** Obtains an array view to the object representation of the elements of the array view. */
NODISCARD FORCEINLINE constexpr auto AsBytes() const
{
constexpr size_t BytesExtent = Extent != DynamicExtent ? sizeof(ElementType) * Extent : DynamicExtent;
return TArrayView<const uint8, BytesExtent>(reinterpret_cast<const uint8*>(GetData().Get()), NumBytes());
}
/** @return The pointer to the underlying element storage. */
NODISCARD FORCEINLINE constexpr TObserverPtr<ElementType[]> GetData() const { return Pointer; }
/** @return The iterator to the first or end element. */
NODISCARD FORCEINLINE constexpr Iterator Begin() const { return Iterator(Pointer.Get() , Pointer.Get(), Pointer.Get() + Num()); }
NODISCARD FORCEINLINE constexpr Iterator End() const { return Iterator(Pointer.Get() + Num(), Pointer.Get(), Pointer.Get() + Num()); }
/** @return The reverse iterator to the first or end element. */
NODISCARD FORCEINLINE constexpr ReverseIterator RBegin() const { return ReverseIterator(End()); }
NODISCARD FORCEINLINE constexpr ReverseIterator REnd() const { return ReverseIterator(Begin()); }
/** @return The number of elements in the container. */
NODISCARD FORCEINLINE constexpr size_t Num() const { if constexpr (Extent == DynamicExtent) return Impl::ArrayNum; return Extent; }
/** @return The number of bytes in the container. */
NODISCARD FORCEINLINE constexpr size_t NumBytes() const { return Num() * sizeof(ElementType); }
/** @return true if the container is empty, false otherwise. */
NODISCARD FORCEINLINE constexpr bool IsEmpty() const { return Num() == 0; }
/** @return true if the iterator is valid, false otherwise. */
NODISCARD FORCEINLINE constexpr bool IsValidIterator(Iterator Iter) const { return Begin() <= Iter && Iter <= End(); }
/** @return The reference to the requested element. */
NODISCARD FORCEINLINE constexpr ElementType& operator[](size_t Index) const { checkf(Index < Num(), TEXT("Read access violation. Please check IsValidIterator().")); return Pointer[Index]; }
/** @return The reference to the first or last element. */
NODISCARD FORCEINLINE constexpr ElementType& Front() const { return *Begin(); }
NODISCARD FORCEINLINE constexpr ElementType& Back() const { return *(End() - 1); }
/** Overloads the GetTypeHash algorithm for TArrayView. */
NODISCARD friend FORCEINLINE constexpr size_t GetTypeHash(TArrayView A) requires (CHashable<ElementType>)
{
size_t Result = 0;
for (Iterator Iter = A.Begin(); Iter != A.End(); ++Iter)
{
Result = HashCombine(Result, GetTypeHash(*Iter));
}
return Result;
}
ENABLE_RANGE_BASED_FOR_LOOP_SUPPORT
private:
TObserverPtr<ElementType[]> Pointer;
};
template <typename I, typename S>
TArrayView(I, S) -> TArrayView<TRemoveReference<TIteratorReferenceType<I>>>;
template <typename T, size_t N>
TArrayView(T(&)[N]) -> TArrayView<T, N>;
template <typename T, size_t N>
TArrayView(TStaticArray<T, N>&) -> TArrayView<T, N>;
template <typename T, size_t N>
TArrayView(const TStaticArray<T, N>&) -> TArrayView<const T, N>;
template <typename T>
TArrayView(TArray<T>&) -> TArrayView<T, DynamicExtent>;
template <typename T>
TArrayView(const TArray<T>&) -> TArrayView<const T, DynamicExtent>;
NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft)
NAMESPACE_REDCRAFT_END

View File

@ -4,3 +4,4 @@
#include "Containers/Iterator.h"
#include "Containers/Array.h"
#include "Containers/StaticArray.h"
#include "Containers/ArrayView.h"

View File

@ -7,6 +7,7 @@
#include "Templates/Noncopyable.h"
#include "TypeTraits/TypeTraits.h"
#include "Miscellaneous/Compare.h"
#include "Memory/ObserverPointer.h"
#include "Miscellaneous/AssertionMacros.h"
NAMESPACE_REDCRAFT_BEGIN
@ -117,7 +118,7 @@ concept CContiguousIterator = CRandomAccessIterator<I> && CLValueReference<TIter
&& CSameAs<TIteratorElementType<I>, TRemoveReference<TIteratorReferenceType<I>>>
&& requires(I& Iter)
{
static_cast<TAddPointer<TIteratorReferenceType<I>>>(Iter);
static_cast<TObserverPtr<TIteratorElementType<I>[]>>(Iter);
{ AddressOf(*Iter) } -> CSameAs<TAddPointer<TIteratorReferenceType<I>>>;
};
@ -416,8 +417,7 @@ public:
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const TCountedIterator& LHS, FDefaultSentinel) { CheckThis(); return -LHS.Num(); }
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(FDefaultSentinel, const TCountedIterator& RHS) { CheckThis(); return RHS.Num(); }
NODISCARD FORCEINLINE constexpr explicit operator ElementType*() requires (CContiguousIterator<IteratorType> && !CConst<ElementType>) { CheckThis(); return Current; }
NODISCARD FORCEINLINE constexpr explicit operator const ElementType*() const requires (CContiguousIterator<IteratorType>) { CheckThis(); return Current; }
NODISCARD FORCEINLINE constexpr explicit operator TObserverPtr<ElementType[]>() const requires (CContiguousIterator<IteratorType>) { CheckThis(); return TObserverPtr<ElementType[]>(Current); }
NODISCARD FORCEINLINE constexpr const IteratorType& GetBase() const& { CheckThis(); return Current; }
NODISCARD FORCEINLINE constexpr IteratorType GetBase() && { CheckThis(); return Current; }
@ -755,7 +755,7 @@ template <typename T, size_t N> FORCEINLINE constexpr decltype(auto) RBegin(
template <typename T, size_t N> FORCEINLINE constexpr decltype(auto) RBegin(const T(& Container)[N]) { return TReverseIterator(End(Container)); }
template <typename T, size_t N> FORCEINLINE constexpr decltype(auto) RBegin(const T(&& Container)[N]) { return TReverseIterator(End(Container)); }
/** Overloads the RBegin algorithm for T::rbegin(). */
/** Overloads the RBegin algorithm for initializer_list. */
template <typename T>
FORCEINLINE constexpr decltype(auto) RBegin(initializer_list<T> Container)
{
@ -775,7 +775,7 @@ template <typename T, size_t N> FORCEINLINE constexpr decltype(auto) REnd(
template <typename T, size_t N> FORCEINLINE constexpr decltype(auto) REnd(const T(& Container)[N]) { return TReverseIterator(Begin(Container)); }
template <typename T, size_t N> FORCEINLINE constexpr decltype(auto) REnd(const T(&& Container)[N]) { return TReverseIterator(Begin(Container)); }
/** Overloads the REnd algorithm for T::end(). */
/** Overloads the REnd algorithm for initializer_list. */
template <typename T>
FORCEINLINE constexpr decltype(auto) REnd(initializer_list<T> Container)
{

View File

@ -6,6 +6,7 @@
#include "Templates/TypeHash.h"
#include "Templates/Container.h"
#include "Containers/Iterator.h"
#include "Containers/ArrayView.h"
#include "TypeTraits/TypeTraits.h"
#include "Miscellaneous/Compare.h"
#include "Memory/ObserverPointer.h"
@ -15,93 +16,6 @@ NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
NAMESPACE_PRIVATE_BEGIN
template <typename ArrayType, typename T>
class TStaticArrayIterator
{
public:
using ElementType = T;
FORCEINLINE constexpr TStaticArrayIterator() = default;
# if DO_CHECK
FORCEINLINE constexpr TStaticArrayIterator(const TStaticArrayIterator<ArrayType, TRemoveConst<ElementType>>& InValue) requires (CConst<ElementType>)
: Owner(InValue.Owner), Pointer(InValue.Pointer)
{ }
# else
FORCEINLINE constexpr TStaticArrayIterator(const TStaticArrayIterator<ArrayType, TRemoveConst<ElementType>>& InValue) requires (CConst<ElementType>)
: Pointer(InValue.Pointer)
{ }
# endif
FORCEINLINE constexpr TStaticArrayIterator(const TStaticArrayIterator&) = default;
FORCEINLINE constexpr TStaticArrayIterator(TStaticArrayIterator&&) = default;
FORCEINLINE constexpr TStaticArrayIterator& operator=(const TStaticArrayIterator&) = default;
FORCEINLINE constexpr TStaticArrayIterator& operator=(TStaticArrayIterator&&) = default;
NODISCARD friend FORCEINLINE constexpr bool operator==(const TStaticArrayIterator& LHS, const TStaticArrayIterator& RHS) { return LHS.Pointer == RHS.Pointer; }
NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const TStaticArrayIterator & LHS, const TStaticArrayIterator & RHS) { return LHS.Pointer <=> RHS.Pointer; }
NODISCARD FORCEINLINE constexpr ElementType& operator*() const { CheckThis(true); return *Pointer; }
NODISCARD FORCEINLINE constexpr ElementType* operator->() const { CheckThis(true); return Pointer; }
NODISCARD FORCEINLINE constexpr ElementType& operator[](ptrdiff Index) const { TStaticArrayIterator Temp = *this + Index; return *Temp; }
FORCEINLINE constexpr TStaticArrayIterator& operator++() { ++Pointer; CheckThis(); return *this; }
FORCEINLINE constexpr TStaticArrayIterator& operator--() { --Pointer; CheckThis(); return *this; }
FORCEINLINE constexpr TStaticArrayIterator operator++(int) { TStaticArrayIterator Temp = *this; ++Pointer; CheckThis(); return Temp; }
FORCEINLINE constexpr TStaticArrayIterator operator--(int) { TStaticArrayIterator Temp = *this; --Pointer; CheckThis(); return Temp; }
FORCEINLINE constexpr TStaticArrayIterator& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; }
FORCEINLINE constexpr TStaticArrayIterator& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; }
NODISCARD friend FORCEINLINE constexpr TStaticArrayIterator operator+(TStaticArrayIterator Iter, ptrdiff Offset) { TStaticArrayIterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD friend FORCEINLINE constexpr TStaticArrayIterator operator+(ptrdiff Offset, TStaticArrayIterator Iter) { TStaticArrayIterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD FORCEINLINE constexpr TStaticArrayIterator operator-(ptrdiff Offset) const { TStaticArrayIterator Temp = *this; Temp -= Offset; return Temp; }
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const TStaticArrayIterator& LHS, const TStaticArrayIterator& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; }
NODISCARD FORCEINLINE constexpr explicit operator ElementType*() requires (!CConst<ElementType>) { CheckThis(); return Pointer; }
NODISCARD FORCEINLINE constexpr explicit operator const ElementType*() const { CheckThis(); return Pointer; }
private:
# if DO_CHECK
const ArrayType* Owner = nullptr;
# endif
ElementType* Pointer = nullptr;
# if DO_CHECK
FORCEINLINE constexpr TStaticArrayIterator(const ArrayType* InContainer, ElementType* InPointer)
: Owner(InContainer), Pointer(InPointer)
{ }
# else
FORCEINLINE constexpr TStaticArrayIterator(const ArrayType* InContainer, ElementType* InPointer)
: Pointer(InPointer)
{ }
# endif
FORCEINLINE constexpr void CheckThis(bool bExceptEnd = false) const
{
checkf(Owner && Owner->IsValidIterator(*this), TEXT("Read access violation. Please check IsValidIterator()."));
checkf(!(bExceptEnd && Owner->End() == *this), TEXT("Read access violation. Please check IsValidIterator()."));
}
friend ArrayType;
template <typename InArrayType, typename InElementType>
friend class TStaticArrayIterator;
};
NAMESPACE_PRIVATE_END
/** TStaticArray is a container that encapsulates fixed size arrays. */
template <CObject T, size_t N>
struct TStaticArray final
@ -109,8 +23,8 @@ struct TStaticArray final
using ElementType = T;
using Iterator = NAMESPACE_PRIVATE::TStaticArrayIterator<TStaticArray, ElementType>;
using ConstIterator = NAMESPACE_PRIVATE::TStaticArrayIterator<TStaticArray, const ElementType>;
using Iterator = TArrayView< T, N>::Iterator;
using ConstIterator = TArrayView<const T, N>::Iterator;
using ReverseIterator = TReverseIterator< Iterator>;
using ConstReverseIterator = TReverseIterator<ConstIterator>;
@ -170,10 +84,10 @@ struct TStaticArray final
NODISCARD FORCEINLINE constexpr TObserverPtr<const ElementType[]> GetData() const { return TObserverPtr<const ElementType[]>(_); }
/** @return The iterator to the first or end element. */
NODISCARD FORCEINLINE constexpr Iterator Begin() { return Iterator(this, _); }
NODISCARD FORCEINLINE constexpr ConstIterator Begin() const { return ConstIterator(this, _); }
NODISCARD FORCEINLINE constexpr Iterator End() { return Iterator(this, _ + Num()); }
NODISCARD FORCEINLINE constexpr ConstIterator End() const { return ConstIterator(this, _ + Num()); }
NODISCARD FORCEINLINE constexpr Iterator Begin() { return TArrayView< T, N>(*this).Begin(); }
NODISCARD FORCEINLINE constexpr ConstIterator Begin() const { return TArrayView<const T, N>(*this).Begin(); }
NODISCARD FORCEINLINE constexpr Iterator End() { return TArrayView< T, N>(*this).End(); }
NODISCARD FORCEINLINE constexpr ConstIterator End() const { return TArrayView<const T, N>(*this).End(); }
/** @return The reverse iterator to the first or end element. */
NODISCARD FORCEINLINE constexpr ReverseIterator RBegin() { return ReverseIterator(End()); }
@ -183,7 +97,6 @@ struct TStaticArray final
/** @return The number of elements in the container. */
NODISCARD FORCEINLINE constexpr size_t Num() const { return N; }
NODISCARD FORCEINLINE constexpr size_t Max() const { return N; }
/** @return true if the container is empty, false otherwise. */
NODISCARD FORCEINLINE constexpr bool IsEmpty() const { return Num() == 0; }

View File

@ -567,16 +567,16 @@ public:
/** Remove the default initialization and disallow the construction of a TFunctionRef that does not store the function target. */
FORCEINLINE constexpr TFunctionRef() = delete;
FORCEINLINE constexpr TFunctionRef(const TFunctionRef& InValue) = default;
FORCEINLINE constexpr TFunctionRef(TFunctionRef&& InValue) = default;
FORCEINLINE constexpr TFunctionRef(const TFunctionRef&) = default;
FORCEINLINE constexpr TFunctionRef(TFunctionRef&&) = default;
/**
* We delete the assignment operators because we don't want it to be confused with being related to
* regular C++ reference assignment - i.e. calling the assignment operator of whatever the reference
* is bound to - because that's not what TFunctionRef does, nor is it even capable of doing that.
*/
FORCEINLINE constexpr TFunctionRef& operator=(const TFunctionRef& InValue) = delete;
FORCEINLINE constexpr TFunctionRef& operator=(TFunctionRef&& InValue) = delete;
FORCEINLINE constexpr TFunctionRef& operator=(const TFunctionRef&) = delete;
FORCEINLINE constexpr TFunctionRef& operator=(TFunctionRef&&) = delete;
/** Constructor which binds a TFunctionRef to a callable object. */
template <typename T> requires (!CTFunctionRef<TDecay<T>>
@ -617,10 +617,10 @@ public:
/** Default constructor. */
FORCEINLINE constexpr TFunction(nullptr_t = nullptr) { Impl::Invalidate(); }
FORCEINLINE TFunction(const TFunction& InValue) = default;
FORCEINLINE TFunction(TFunction&& InValue) = default;
FORCEINLINE TFunction& operator=(const TFunction& InValue) = default;
FORCEINLINE TFunction& operator=(TFunction&& InValue) = default;
FORCEINLINE TFunction(const TFunction&) = default;
FORCEINLINE TFunction(TFunction&&) = default;
FORCEINLINE TFunction& operator=(const TFunction&) = default;
FORCEINLINE TFunction& operator=(TFunction&&) = default;
/**
* Constructs an TFunction with initial content an function object of type TDecay<T>,
@ -746,10 +746,10 @@ public:
/** Default constructor. */
FORCEINLINE constexpr TUniqueFunction(nullptr_t = nullptr) { Impl::Invalidate(); }
FORCEINLINE TUniqueFunction(const TUniqueFunction& InValue) = delete;
FORCEINLINE TUniqueFunction(TUniqueFunction&& InValue) = default;
FORCEINLINE TUniqueFunction& operator=(const TUniqueFunction& InValue) = delete;
FORCEINLINE TUniqueFunction& operator=(TUniqueFunction&& InValue) = default;
FORCEINLINE TUniqueFunction(const TUniqueFunction&) = delete;
FORCEINLINE TUniqueFunction(TUniqueFunction&&) = default;
FORCEINLINE TUniqueFunction& operator=(const TUniqueFunction&) = delete;
FORCEINLINE TUniqueFunction& operator=(TUniqueFunction&&) = default;
/** Constructor from TFunction to TUniqueFunction. */
FORCEINLINE TUniqueFunction(const TFunction<F>& InValue)

View File

@ -11,6 +11,7 @@ NAMESPACE_BEGIN(Testing)
REDCRAFTUTILITY_API void TestContainers();
REDCRAFTUTILITY_API void TestArray();
REDCRAFTUTILITY_API void TestStaticArray();
REDCRAFTUTILITY_API void TestArrayView();
NAMESPACE_END(Testing)