feat(memory): add ToAddress and refactor the code with it

This commit is contained in:
2024-11-01 15:11:45 +08:00
parent 1cfa252779
commit 9785326b18
12 changed files with 170 additions and 131 deletions

View File

@ -1,6 +1,7 @@
#pragma once
#include "CoreTypes.h"
#include "Memory/Address.h"
#include "Memory/Allocator.h"
#include "Containers/Array.h"
#include "Templates/Utility.h"
@ -58,7 +59,7 @@ public:
{
checkf(Extent == DynamicExtent || Extent == InCount, TEXT("Illegal range count. Please check InCount."));
Impl.Pointer = AddressOf(*InFirst);
Impl.Pointer = ToAddress(InFirst);
if constexpr (Extent == DynamicExtent)
{
@ -72,7 +73,7 @@ public:
{
checkf(Extent == DynamicExtent || Extent == InLast - InFirst, TEXT("Illegal range iterator. Please check InLast - InFirst."));
Impl.Pointer = AddressOf(*InFirst);
Impl.Pointer = ToAddress(InFirst);
if constexpr (Extent == DynamicExtent)
{
@ -84,7 +85,8 @@ public:
template <size_t N> requires (Extent == DynamicExtent || N == Extent)
FORCEINLINE constexpr TArrayView(ElementType(&InArray)[N])
{
Impl.Pointer = AddressOf(InArray[0]);
// @TODO: Refactor this to use the GetData() function.
Impl.Pointer = InArray;
if constexpr (Extent == DynamicExtent)
{
@ -114,7 +116,8 @@ public:
{
checkf(Extent == DynamicExtent || Extent == InValue.Num(), TEXT("Illegal view extent. Please check InValue.Num()."));
Impl.Pointer = AddressOf(InValue[0]);
// @TODO: Refactor this to use the GetData() function.
Impl.Pointer = InValue.GetData().Get();
if constexpr (Extent == DynamicExtent)
{

View File

@ -1,6 +1,7 @@
#pragma once
#include "CoreTypes.h"
#include "Memory/Address.h"
#include "Templates/Invoke.h"
#include "Templates/Utility.h"
#include "Templates/Noncopyable.h"
@ -128,7 +129,7 @@ concept CContiguousIterator = CRandomAccessIterator<I> && CLValueReference<TIter
&& CSameAs<TIteratorElementType<I>, TRemoveCVRef<TIteratorReferenceType<I>>>
&& requires(I& Iter)
{
{ AddressOf(*Iter) } -> CSameAs<TAddPointer<TIteratorReferenceType<I>>>;
{ ToAddress(Iter) } -> CSameAs<TAddPointer<TIteratorReferenceType<I>>>;
};
static_assert(CContiguousIterator<int32*>);
@ -173,8 +174,8 @@ public:
template <CBidirectionalIterator J> requires (CSizedSentinelFor<J, IteratorType>)
NODISCARD friend FORCEINLINE constexpr TCompareThreeWayResult<J, IteratorType> operator<=>(const TReverseIterator& LHS, const TReverseIterator<J>& RHS) { return RHS.GetBase() <=> LHS.GetBase(); }
NODISCARD FORCEINLINE constexpr TIteratorReferenceType<IteratorType> operator*() const { IteratorType Temp = GetBase(); return *--Temp; }
NODISCARD FORCEINLINE constexpr TIteratorPointerType<IteratorType> operator->() const { return AddressOf(operator*()); }
NODISCARD FORCEINLINE constexpr TIteratorReferenceType<IteratorType> operator*() const { IteratorType Temp = GetBase(); return *--Temp; }
NODISCARD FORCEINLINE constexpr TIteratorPointerType<IteratorType> operator->() const { IteratorType Temp = GetBase(); return ToAddress(--Temp); }
NODISCARD FORCEINLINE constexpr TIteratorReferenceType<IteratorType> operator[](ptrdiff Index) const requires (CRandomAccessIterator<IteratorType>) { return GetBase()[-Index - 1]; }
@ -402,7 +403,7 @@ public:
NODISCARD FORCEINLINE constexpr TIteratorReferenceType<IteratorType> operator*() const requires (CDereferenceable<const IteratorType>) { CheckThis(true); return *Current; }
NODISCARD FORCEINLINE constexpr TIteratorPointerType<IteratorType> operator->() const requires (CContiguousIterator<IteratorType>) { CheckThis(true); return AddressOf(operator*()); }
NODISCARD FORCEINLINE constexpr TIteratorPointerType<IteratorType> operator->() const requires (CContiguousIterator<IteratorType>) { CheckThis(true); return ToAddress(Current); }
NODISCARD FORCEINLINE constexpr TIteratorReferenceType<IteratorType> operator[](ptrdiff Index) const requires (CRandomAccessIterator<IteratorType>) { TCountedIterator Temp = *this + Index; return *Temp; }