refactor(*): fix EBO issues and remove NAMESPACE_PRIVATE about containers

This commit is contained in:
_Redstone_c_ 2023-03-04 19:12:47 +08:00
parent 5c91059203
commit 9045f9b3b2
5 changed files with 331 additions and 228 deletions

View File

@ -16,96 +16,15 @@ NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
NAMESPACE_PRIVATE_BEGIN
template <typename ArrayType, typename T>
class TArrayIterator
{
public:
using ElementType = T;
FORCEINLINE TArrayIterator() = default;
# if DO_CHECK
FORCEINLINE TArrayIterator(const TArrayIterator<ArrayType, TRemoveConst<ElementType>>& InValue) requires (CConst<ElementType>)
: Owner(InValue.Owner), Pointer(InValue.Pointer)
{ }
# else
FORCEINLINE TArrayIterator(const TArrayIterator<ArrayType, TRemoveConst<ElementType>>& InValue) requires (CConst<ElementType>)
: Pointer(InValue.Pointer)
{ }
# endif
FORCEINLINE TArrayIterator(const TArrayIterator&) = default;
FORCEINLINE TArrayIterator(TArrayIterator&&) = default;
FORCEINLINE TArrayIterator& operator=(const TArrayIterator&) = default;
FORCEINLINE TArrayIterator& operator=(TArrayIterator&&) = default;
NODISCARD friend FORCEINLINE bool operator==(const TArrayIterator& LHS, const TArrayIterator& RHS) { return LHS.Pointer == RHS.Pointer; }
NODISCARD friend FORCEINLINE strong_ordering operator<=>(const TArrayIterator & LHS, const TArrayIterator & RHS) { return LHS.Pointer <=> RHS.Pointer; }
NODISCARD FORCEINLINE ElementType& operator*() const { CheckThis(true); return *Pointer; }
NODISCARD FORCEINLINE ElementType* operator->() const { CheckThis(true); return Pointer; }
NODISCARD FORCEINLINE ElementType& operator[](ptrdiff Index) const { TArrayIterator Temp = *this + Index; return *Temp; }
FORCEINLINE TArrayIterator& operator++() { ++Pointer; CheckThis(); return *this; }
FORCEINLINE TArrayIterator& operator--() { --Pointer; CheckThis(); return *this; }
FORCEINLINE TArrayIterator operator++(int) { TArrayIterator Temp = *this; ++*this; return Temp; }
FORCEINLINE TArrayIterator operator--(int) { TArrayIterator Temp = *this; --*this; return Temp; }
FORCEINLINE TArrayIterator& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; }
FORCEINLINE TArrayIterator& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; }
NODISCARD friend FORCEINLINE TArrayIterator operator+(TArrayIterator Iter, ptrdiff Offset) { TArrayIterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD friend FORCEINLINE TArrayIterator operator+(ptrdiff Offset, TArrayIterator Iter) { TArrayIterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD FORCEINLINE TArrayIterator operator-(ptrdiff Offset) const { TArrayIterator Temp = *this; Temp -= Offset; return Temp; }
NODISCARD friend FORCEINLINE ptrdiff operator-(const TArrayIterator& LHS, const TArrayIterator& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; }
NODISCARD FORCEINLINE explicit operator TObserverPtr<ElementType[]>() const { CheckThis(); return TObserverPtr<ElementType[]>(Pointer); }
private:
# if DO_CHECK
const ArrayType* Owner = nullptr;
# endif
ElementType* Pointer = nullptr;
# if DO_CHECK
FORCEINLINE TArrayIterator(const ArrayType* InContainer, ElementType* InPointer)
: Owner(InContainer), Pointer(InPointer)
{ }
# else
FORCEINLINE TArrayIterator(const ArrayType* InContainer, ElementType* InPointer)
: Pointer(InPointer)
{ }
# endif
FORCEINLINE 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 TArrayIterator;
};
NAMESPACE_PRIVATE_END
/** Dynamic array. The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements. */
template <CElementalObject T, CInstantiableAllocator Allocator = FHeapAllocator> requires (!CConst<T>)
class TArray final
{
private:
template <bool bConst>
class IteratorImpl;
public:
using ElementType = T;
@ -114,8 +33,8 @@ public:
using Reference = T&;
using ConstReference = const T&;
using Iterator = NAMESPACE_PRIVATE::TArrayIterator<TArray, ElementType>;
using ConstIterator = NAMESPACE_PRIVATE::TArrayIterator<TArray, const ElementType>;
using Iterator = IteratorImpl<false>;
using ConstIterator = IteratorImpl<true >;
using ReverseIterator = TReverseIterator< Iterator>;
using ConstReverseIterator = TReverseIterator<ConstIterator>;
@ -155,7 +74,7 @@ public:
{
if constexpr (CForwardIterator<I>)
{
if (CSizedSentinelFor<S, I>) checkf(First <= Last, TEXT("Illegal range iterator. Please check First <= Last."));
if (CSizedSentinelFor<S, I>) { checkf(First <= Last, TEXT("Illegal range iterator. Please check First <= Last.")); }
const size_t Count = Iteration::Distance(First, Last);
@ -615,7 +534,7 @@ public:
if constexpr (CForwardIterator<I>)
{
if (CSizedSentinelFor<S, I>) checkf(First <= Last, TEXT("Illegal range iterator. Please check First <= Last."));
if (CSizedSentinelFor<S, I>) { checkf(First <= Last, TEXT("Illegal range iterator. Please check First <= Last.")); }
const size_t InsertIndex = Iter - Begin();
const size_t Count = Iteration::Distance(First, Last);
@ -1142,6 +1061,89 @@ private:
}
ALLOCATOR_WRAPPER_END(AllocatorType, ElementType, Impl)
private:
template <bool bConst>
class IteratorImpl
{
public:
using ElementType = TConditional<bConst, const T, T>;
FORCEINLINE IteratorImpl() = default;
# if DO_CHECK
FORCEINLINE IteratorImpl(const IteratorImpl<false>& InValue) requires (bConst)
: Owner(InValue.Owner), Pointer(InValue.Pointer)
{ }
# else
FORCEINLINE IteratorImpl(const IteratorImpl<false>& InValue) requires (bConst)
: Pointer(InValue.Pointer)
{ }
# endif
FORCEINLINE IteratorImpl(const IteratorImpl&) = default;
FORCEINLINE IteratorImpl(IteratorImpl&&) = default;
FORCEINLINE IteratorImpl& operator=(const IteratorImpl&) = default;
FORCEINLINE IteratorImpl& operator=(IteratorImpl&&) = default;
NODISCARD friend FORCEINLINE bool operator==(const IteratorImpl& LHS, const IteratorImpl& RHS) { return LHS.Pointer == RHS.Pointer; }
NODISCARD friend FORCEINLINE strong_ordering operator<=>(const IteratorImpl& LHS, const IteratorImpl& RHS) { return LHS.Pointer <=> RHS.Pointer; }
NODISCARD FORCEINLINE ElementType& operator*() const { CheckThis(true); return *Pointer; }
NODISCARD FORCEINLINE ElementType* operator->() const { CheckThis(true); return Pointer; }
NODISCARD FORCEINLINE ElementType& operator[](ptrdiff Index) const { IteratorImpl Temp = *this + Index; return *Temp; }
FORCEINLINE IteratorImpl& operator++() { ++Pointer; CheckThis(); return *this; }
FORCEINLINE IteratorImpl& operator--() { --Pointer; CheckThis(); return *this; }
FORCEINLINE IteratorImpl operator++(int) { IteratorImpl Temp = *this; ++*this; return Temp; }
FORCEINLINE IteratorImpl operator--(int) { IteratorImpl Temp = *this; --*this; return Temp; }
FORCEINLINE IteratorImpl& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; }
FORCEINLINE IteratorImpl& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; }
NODISCARD friend FORCEINLINE IteratorImpl operator+(IteratorImpl Iter, ptrdiff Offset) { IteratorImpl Temp = Iter; Temp += Offset; return Temp; }
NODISCARD friend FORCEINLINE IteratorImpl operator+(ptrdiff Offset, IteratorImpl Iter) { IteratorImpl Temp = Iter; Temp += Offset; return Temp; }
NODISCARD FORCEINLINE IteratorImpl operator-(ptrdiff Offset) const { IteratorImpl Temp = *this; Temp -= Offset; return Temp; }
NODISCARD friend FORCEINLINE ptrdiff operator-(const IteratorImpl& LHS, const IteratorImpl& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; }
NODISCARD FORCEINLINE explicit operator TObserverPtr<ElementType[]>() const { CheckThis(); return TObserverPtr<ElementType[]>(Pointer); }
private:
# if DO_CHECK
const TArray* Owner = nullptr;
# endif
ElementType* Pointer = nullptr;
# if DO_CHECK
FORCEINLINE IteratorImpl(const TArray* InContainer, ElementType* InPointer)
: Owner(InContainer), Pointer(InPointer)
{ }
# else
FORCEINLINE IteratorImpl(const TArray* InContainer, ElementType* InPointer)
: Pointer(InPointer)
{ }
# endif
FORCEINLINE 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()."));
}
template <bool> friend class IteratorImpl;
friend TArray;
};
};
template <typename I, typename S>

View File

@ -14,108 +14,6 @@ NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
template <CElementalObject 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; ++*this; return Temp; }
FORCEINLINE constexpr TArrayViewIterator operator--(int) { TArrayViewIterator Temp = *this; --*this; 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 <CElementalObject 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 <CElementalObject T, size_t N>
struct TStaticArray;
@ -130,19 +28,15 @@ inline constexpr size_t DynamicExtent = INDEX_NONE;
* is known at compile-time and encoded in the type, or a dynamic extent.
*/
template <CElementalObject T, size_t InExtent = DynamicExtent>
class TArrayView final : private NAMESPACE_PRIVATE::TEnableArrayNum<InExtent == DynamicExtent>
class TArrayView final
{
private:
using Impl = NAMESPACE_PRIVATE::TEnableArrayNum<InExtent == DynamicExtent>;
public:
using ElementType = T;
using Reference = T&;
using Iterator = NAMESPACE_PRIVATE::TArrayViewIterator<ElementType>;
class Iterator;
using ReverseIterator = TReverseIterator<Iterator>;
@ -155,35 +49,41 @@ public:
/** 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))
FORCEINLINE constexpr explicit (Extent != DynamicExtent) TArrayView(I InFirst, size_t InCount)
{
checkf(Extent == DynamicExtent || Extent == InCount, TEXT("Illegal range count. Please check InCount."));
Impl.Pointer = AddressOf(*InFirst);
if constexpr (Extent == DynamicExtent)
{
Impl::ArrayNum = InCount;
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))
FORCEINLINE constexpr explicit (Extent != DynamicExtent) TArrayView(I InFirst, S InLast)
{
checkf(Extent == DynamicExtent || Extent == InLast - InFirst, TEXT("Illegal range iterator. Please check InLast - InFirst."));
Impl.Pointer = AddressOf(*InFirst);
if constexpr (Extent == DynamicExtent)
{
Impl::ArrayNum = InLast - InFirst;
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)
FORCEINLINE constexpr TArrayView(ElementType(&InArray)[N])
{
Impl.Pointer = AddressOf(InArray[0]);
if constexpr (Extent == DynamicExtent)
{
Impl::ArrayNum = N;
Impl.ArrayNum = N;
}
}
@ -211,13 +111,15 @@ public:
/** 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())
FORCEINLINE constexpr explicit (Extent != DynamicExtent && N == DynamicExtent) TArrayView(TArrayView<U, N> InValue)
{
checkf(Extent == DynamicExtent || Extent == InValue.Num(), TEXT("Illegal view extent. Please check InValue.Num()."));
Impl.Pointer = AddressOf(InValue[0]);
if constexpr (Extent == DynamicExtent)
{
Impl::ArrayNum = InValue.Num();
Impl.ArrayNum = InValue.Num();
}
}
@ -365,18 +267,18 @@ public:
}
/** @return The pointer to the underlying element storage. */
NODISCARD FORCEINLINE constexpr TObserverPtr<ElementType[]> GetData() const { return Pointer; }
NODISCARD FORCEINLINE constexpr TObserverPtr<ElementType[]> GetData() const { return TObserverPtr<ElementType[]>(Impl.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()); }
NODISCARD FORCEINLINE constexpr Iterator Begin() const { return Iterator(this, Impl.Pointer); }
NODISCARD FORCEINLINE constexpr Iterator End() const { return Iterator(this, Impl.Pointer + 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; }
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); }
@ -388,7 +290,7 @@ public:
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]; }
NODISCARD FORCEINLINE constexpr ElementType& operator[](size_t Index) const { checkf(Index < Num(), TEXT("Read access violation. Please check IsValidIterator().")); return Impl.Pointer[Index]; }
/** @return The reference to the first or last element. */
NODISCARD FORCEINLINE constexpr ElementType& Front() const { return *Begin(); }
@ -411,7 +313,80 @@ public:
private:
TObserverPtr<ElementType[]> Pointer;
struct FImplWithoutNum { ElementType* Pointer; };
struct FImplWithNum : FImplWithoutNum { size_t ArrayNum; };
TConditional<InExtent == DynamicExtent, FImplWithNum, FImplWithoutNum> Impl;
public:
class Iterator
{
public:
using ElementType = T;
FORCEINLINE constexpr Iterator() = default;
FORCEINLINE constexpr Iterator(const Iterator&) = default;
FORCEINLINE constexpr Iterator(Iterator&&) = default;
FORCEINLINE constexpr Iterator& operator=(const Iterator&) = default;
FORCEINLINE constexpr Iterator& operator=(Iterator&&) = default;
NODISCARD friend FORCEINLINE constexpr bool operator==(const Iterator& LHS, const Iterator& RHS) { return LHS.Pointer == RHS.Pointer; }
NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const Iterator& LHS, const Iterator& 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 { Iterator Temp = *this + Index; return *Temp; }
FORCEINLINE constexpr Iterator& operator++() { ++Pointer; CheckThis(); return *this; }
FORCEINLINE constexpr Iterator& operator--() { --Pointer; CheckThis(); return *this; }
FORCEINLINE constexpr Iterator operator++(int) { Iterator Temp = *this; ++*this; return Temp; }
FORCEINLINE constexpr Iterator operator--(int) { Iterator Temp = *this; --*this; return Temp; }
FORCEINLINE constexpr Iterator& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; }
FORCEINLINE constexpr Iterator& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; }
NODISCARD friend FORCEINLINE constexpr Iterator operator+(Iterator Iter, ptrdiff Offset) { Iterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD friend FORCEINLINE constexpr Iterator operator+(ptrdiff Offset, Iterator Iter) { Iterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD FORCEINLINE constexpr Iterator operator-(ptrdiff Offset) const { Iterator Temp = *this; Temp -= Offset; return Temp; }
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const Iterator& LHS, const Iterator& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; }
NODISCARD FORCEINLINE constexpr explicit operator TObserverPtr<ElementType[]>() const { CheckThis(); return TObserverPtr<ElementType[]>(Pointer); }
private:
# if DO_CHECK
const TArrayView* Owner = nullptr;
# endif
ElementType* Pointer = nullptr;
# if DO_CHECK
FORCEINLINE constexpr Iterator(const TArrayView* InContainer, ElementType* InPointer)
: Owner(InContainer), Pointer(InPointer)
{ }
# else
FORCEINLINE constexpr Iterator(const TArrayView* 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 TArrayView;
};
};

View File

@ -20,14 +20,20 @@ NAMESPACE_MODULE_BEGIN(Utility)
template <CElementalObject T, size_t N>
struct TStaticArray final
{
private:
template <bool bConst>
class IteratorImpl;
public:
using ElementType = T;
using Reference = T&;
using ConstReference = const T&;
using Iterator = TArrayView< T, N>::Iterator;
using ConstIterator = TArrayView<const T, N>::Iterator;
using Iterator = IteratorImpl<false>;
using ConstIterator = IteratorImpl<true >;
using ReverseIterator = TReverseIterator< Iterator>;
using ConstReverseIterator = TReverseIterator<ConstIterator>;
@ -87,10 +93,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 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(); }
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()); }
/** @return The reverse iterator to the first or end element. */
NODISCARD FORCEINLINE constexpr ReverseIterator RBegin() { return ReverseIterator(End()); }
@ -137,6 +143,89 @@ struct TStaticArray final
T _[N];
private:
template <bool bConst>
class IteratorImpl
{
public:
using ElementType = TConditional<bConst, const T, T>;
FORCEINLINE constexpr IteratorImpl() = default;
# if DO_CHECK
FORCEINLINE IteratorImpl(const IteratorImpl<false>& InValue) requires (bConst)
: Owner(InValue.Owner), Pointer(InValue.Pointer)
{ }
# else
FORCEINLINE IteratorImpl(const IteratorImpl<false>& InValue) requires (bConst)
: Pointer(InValue.Pointer)
{ }
# endif
FORCEINLINE constexpr IteratorImpl(const IteratorImpl&) = default;
FORCEINLINE constexpr IteratorImpl(IteratorImpl&&) = default;
FORCEINLINE constexpr IteratorImpl& operator=(const IteratorImpl&) = default;
FORCEINLINE constexpr IteratorImpl& operator=(IteratorImpl&&) = default;
NODISCARD friend FORCEINLINE constexpr bool operator==(const IteratorImpl& LHS, const IteratorImpl& RHS) { return LHS.Pointer == RHS.Pointer; }
NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const IteratorImpl& LHS, const IteratorImpl& 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 { IteratorImpl Temp = *this + Index; return *Temp; }
FORCEINLINE constexpr IteratorImpl& operator++() { ++Pointer; CheckThis(); return *this; }
FORCEINLINE constexpr IteratorImpl& operator--() { --Pointer; CheckThis(); return *this; }
FORCEINLINE constexpr IteratorImpl operator++(int) { IteratorImpl Temp = *this; ++*this; return Temp; }
FORCEINLINE constexpr IteratorImpl operator--(int) { IteratorImpl Temp = *this; --*this; return Temp; }
FORCEINLINE constexpr IteratorImpl& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; }
FORCEINLINE constexpr IteratorImpl& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; }
NODISCARD friend FORCEINLINE constexpr IteratorImpl operator+(IteratorImpl Iter, ptrdiff Offset) { IteratorImpl Temp = Iter; Temp += Offset; return Temp; }
NODISCARD friend FORCEINLINE constexpr IteratorImpl operator+(ptrdiff Offset, IteratorImpl Iter) { IteratorImpl Temp = Iter; Temp += Offset; return Temp; }
NODISCARD FORCEINLINE constexpr IteratorImpl operator-(ptrdiff Offset) const { IteratorImpl Temp = *this; Temp -= Offset; return Temp; }
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const IteratorImpl& LHS, const IteratorImpl& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; }
NODISCARD FORCEINLINE constexpr explicit operator TObserverPtr<ElementType[]>() const { CheckThis(); return TObserverPtr<ElementType[]>(Pointer); }
private:
# if DO_CHECK
const TStaticArray* Owner = nullptr;
# endif
ElementType* Pointer = nullptr;
# if DO_CHECK
FORCEINLINE constexpr IteratorImpl(const TStaticArray* InContainer, ElementType* InPointer)
: Owner(InContainer), Pointer(InPointer)
{ }
# else
FORCEINLINE constexpr IteratorImpl(const TStaticArray* 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()."));
}
template <bool> friend class IteratorImpl;
friend TStaticArray;
};
};
template <typename T, typename... U> requires (true && ... && CSameAs<T, U>)

View File

@ -12,8 +12,17 @@ NAMESPACE_MODULE_BEGIN(Utility)
struct FAllocatorInterface;
template <typename T>
concept CInstantiableAllocator = CDerivedFrom<T, FAllocatorInterface> && !CSameAs<T, FAllocatorInterface>;
template <typename A, typename T = int>
concept CInstantiableAllocator = !CSameAs<A, FAllocatorInterface>
&& requires (typename A::template ForElementType<T>& Allocator, T* InPtr, size_t Num, size_t NumAllocated)
{
{ Allocator.Allocate(Num) } -> CSameAs<T*>;
{ Allocator.Deallocate(InPtr) } -> CSameAs<void>;
{ AsConst(Allocator).IsTransferable(InPtr) } -> CBooleanTestable;
{ AsConst(Allocator).CalculateSlackGrow(Num, NumAllocated) } -> CSameAs<size_t>;
{ AsConst(Allocator).CalculateSlackShrink(Num, NumAllocated) } -> CSameAs<size_t>;
{ AsConst(Allocator).CalculateSlackReserve(Num) } -> CSameAs<size_t>;
};
/**
* This is the allocator interface, the allocator does not use virtual, this contains the default of
@ -24,10 +33,16 @@ concept CInstantiableAllocator = CDerivedFrom<T, FAllocatorInterface> && !CSameA
struct FAllocatorInterface
{
template <CObject T>
class ForElementType : private FSingleton
class ForElementType /*: private FSingleton*/
{
public:
ForElementType() = default;
ForElementType(const ForElementType&) = delete;
ForElementType(ForElementType&&) = delete;
ForElementType& operator=(const ForElementType&) = delete;
ForElementType& operator=(ForElementType&&) = delete;
/**
* Allocates uninitialized storage.
* Should be allocated according to the results given by the CalculateSlackReserve() family,
@ -57,7 +72,7 @@ struct FAllocatorInterface
#define ALLOCATOR_WRAPPER_BEGIN(Allocator, Type, Name) \
\
struct PREPROCESSOR_JOIN(F, Name) : private FSingleton
struct PREPROCESSOR_JOIN(F, Name) /*: private FSingleton*/
#define ALLOCATOR_WRAPPER_END(Allocator, Type, Name) ; \
\
@ -90,13 +105,19 @@ struct FAllocatorInterface
PREPROCESSOR_JOIN(T, Name)<typename Allocator::template ForElementType<Type>> Name;
/** This is heap allocator that calls Memory::Malloc() directly for memory allocation. */
struct FHeapAllocator : public FAllocatorInterface
struct FHeapAllocator
{
template <CObject T>
class ForElementType : public FAllocatorInterface::ForElementType<T>
class ForElementType
{
public:
ForElementType() = default;
ForElementType(const ForElementType&) = delete;
ForElementType(ForElementType&&) = delete;
ForElementType& operator=(const ForElementType&) = delete;
ForElementType& operator=(ForElementType&&) = delete;
NODISCARD FORCEINLINE T* Allocate(size_t InNum)
{
return InNum != 0 ? static_cast<T*>(Memory::Malloc(Memory::QuantizeSize(InNum * sizeof(T)), alignof(T))) : nullptr;
@ -107,6 +128,8 @@ struct FHeapAllocator : public FAllocatorInterface
Memory::Free(InPtr);
}
NODISCARD FORCEINLINE bool IsTransferable(T* InPtr) const { return true; }
NODISCARD FORCEINLINE size_t CalculateSlackGrow(size_t Num, size_t NumAllocated) const
{
const size_t FirstGrow = 4;
@ -159,13 +182,19 @@ struct FHeapAllocator : public FAllocatorInterface
* Any allocation needed beyond that causes all data to be moved into an indirect allocation.
*/
template <size_t NumInline, CInstantiableAllocator SecondaryAllocator = FHeapAllocator>
struct TInlineAllocator : public FAllocatorInterface
struct TInlineAllocator
{
template <CObject T>
class ForElementType : public FAllocatorInterface::ForElementType<T>
class ForElementType
{
public:
ForElementType() = default;
ForElementType(const ForElementType&) = delete;
ForElementType(ForElementType&&) = delete;
ForElementType& operator=(const ForElementType&) = delete;
ForElementType& operator=(ForElementType&&) = delete;
NODISCARD FORCEINLINE T* Allocate(size_t InNum)
{
if (InNum == 0) return nullptr;
@ -233,13 +262,19 @@ struct TInlineAllocator : public FAllocatorInterface
};
/** This is a null allocator for which all operations are illegal. */
struct FNullAllocator : public FAllocatorInterface
struct FNullAllocator
{
template <CObject T>
class ForElementType : public FAllocatorInterface::ForElementType<T>
class ForElementType
{
public:
ForElementType() = default;
ForElementType(const ForElementType&) = delete;
ForElementType(ForElementType&&) = delete;
ForElementType& operator=(const ForElementType&) = delete;
ForElementType& operator=(ForElementType&&) = delete;
NODISCARD FORCEINLINE T* Allocate(size_t InNum) { check_no_entry(); return nullptr; }
FORCEINLINE void Deallocate(T* InPtr) { check_no_entry(); }

View File

@ -6,6 +6,8 @@ NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
// WARNING: Using these helper classes as base classes may raise potential EBO issues and is not recommended for objects with strict size constraints.
/** A class indicates that a derived class cannot be copied. */
struct FNoncopyable
{