From ec2a240c234e659af15d1474e765e2ecc744e7fd Mon Sep 17 00:00:00 2001 From: Redstone1024 <2824517378@qq.com> Date: Fri, 1 Nov 2024 19:51:44 +0800 Subject: [PATCH] refactor(*): remove TObserverPtr and use raw pointer instead --- .../Source/Private/Testing/MemoryTesting.cpp | 67 -------- .../Source/Public/Containers/Array.h | 19 +-- .../Source/Public/Containers/ArrayView.h | 27 ++- .../Source/Public/Containers/Bitset.h | 4 +- .../Source/Public/Containers/Iterator.h | 5 +- .../Source/Public/Containers/List.h | 1 - .../Source/Public/Containers/StaticArray.h | 11 +- .../Source/Public/Containers/StaticBitset.h | 4 +- .../Source/Public/Memory/ObserverPointer.h | 155 ------------------ .../Public/Miscellaneous/ConstantIterator.h | 4 +- .../Source/Public/String/String.h | 25 ++- .../Source/Public/String/StringView.h | 5 +- .../Source/Public/Templates/Container.h | 29 ++-- 13 files changed, 58 insertions(+), 298 deletions(-) delete mode 100644 Redcraft.Utility/Source/Public/Memory/ObserverPointer.h diff --git a/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp b/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp index 519d6b9..0157732 100644 --- a/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp +++ b/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp @@ -6,7 +6,6 @@ #include "Memory/UniquePointer.h" #include "Memory/SharedPointer.h" #include "Memory/MemoryOperator.h" -#include "Memory/ObserverPointer.h" #include "Memory/InOutPointer.h" #include "Miscellaneous/AssertionMacros.h" @@ -26,7 +25,6 @@ void TestMemory() TestPointerTraits(); TestUniquePointer(); TestSharedPointer(); - TestObserverPointer(); TestInOutPointer(); } @@ -1047,71 +1045,6 @@ void TestSharedPointer() } -void TestObserverPointer() -{ - { - int32 IntA; - int32 IntB; - - TObserverPtr TempA; - TObserverPtr TempB = nullptr; - TObserverPtr TempC(&IntA); - TObserverPtr TempD(TempC); - - TempA = TempC; - TempB = MakeObserver(&IntB); - - always_check(TempA == TempC); - always_check(TempB == &IntB); - always_check(TempB.IsValid()); - - always_check(TempA.Release() == &IntA); - always_check(!TempA.IsValid()); - - TempA.Reset(&IntA); - - always_check(TempA == &IntA); - - always_check(GetTypeHash(TempA) == GetTypeHash(&IntA)); - - Swap(TempA, TempB); - - always_check(TempA == &IntB); - always_check(TempB == &IntA); - } - - { - int32 IntA[4]; - int32 IntB[4]; - - TObserverPtr TempA; - TObserverPtr TempB = nullptr; - TObserverPtr TempC(IntA); - TObserverPtr TempD(TempC); - - TempA = TempC; - TempB = MakeObserver(IntB); - - always_check(TempA == TempC); - always_check(TempB == IntB); - always_check(TempB.IsValid()); - - always_check(TempA.Release() == IntA); - always_check(!TempA.IsValid()); - - TempA.Reset(IntA); - - always_check(TempA == IntA); - - always_check(GetTypeHash(TempA) == GetTypeHash(&IntA)); - - Swap(TempA, TempB); - - always_check(TempA == IntB); - always_check(TempB == IntA); - } -} - void TestInOutPointer() { { diff --git a/Redcraft.Utility/Source/Public/Containers/Array.h b/Redcraft.Utility/Source/Public/Containers/Array.h index 424a0be..f31bd1b 100644 --- a/Redcraft.Utility/Source/Public/Containers/Array.h +++ b/Redcraft.Utility/Source/Public/Containers/Array.h @@ -9,7 +9,6 @@ #include "TypeTraits/TypeTraits.h" #include "Miscellaneous/Compare.h" #include "Memory/MemoryOperator.h" -#include "Memory/ObserverPointer.h" #include "Miscellaneous/AssertionMacros.h" #include "Miscellaneous/ConstantIterator.h" @@ -256,20 +255,20 @@ public: Impl.ArrayMax = NumToAllocate; Impl.Pointer = Impl->Allocate(Max()); - Memory::CopyConstruct(Impl.Pointer, NAMESPACE_REDCRAFT::GetData(IL).Get(), Num()); + Memory::CopyConstruct(Impl.Pointer, NAMESPACE_REDCRAFT::GetData(IL), Num()); return *this; } if (GetNum(IL) <= Num()) { - Memory::CopyAssign(Impl.Pointer, NAMESPACE_REDCRAFT::GetData(IL).Get(), GetNum(IL)); + Memory::CopyAssign(Impl.Pointer, NAMESPACE_REDCRAFT::GetData(IL), GetNum(IL)); Memory::Destruct(Impl.Pointer + GetNum(IL), Num() - GetNum(IL)); } else if (GetNum(IL) <= Max()) { - Memory::CopyAssign(Impl.Pointer, NAMESPACE_REDCRAFT::GetData(IL).Get(), Num()); - Memory::CopyConstruct(Impl.Pointer + Num(), NAMESPACE_REDCRAFT::GetData(IL).Get() + Num(), GetNum(IL) - Num()); + Memory::CopyAssign(Impl.Pointer, NAMESPACE_REDCRAFT::GetData(IL), Num()); + Memory::CopyConstruct(Impl.Pointer + Num(), NAMESPACE_REDCRAFT::GetData(IL) + Num(), GetNum(IL) - Num()); } else check_no_entry(); @@ -869,8 +868,8 @@ public: } /** @return The pointer to the underlying element storage. */ - NODISCARD FORCEINLINE TObserverPtr< ElementType[]> GetData() { return TObserverPtr< ElementType[]>(Impl.Pointer); } - NODISCARD FORCEINLINE TObserverPtr GetData() const { return TObserverPtr(Impl.Pointer); } + NODISCARD FORCEINLINE ElementType* GetData() { return Impl.Pointer; } + NODISCARD FORCEINLINE const ElementType* GetData() const { return Impl.Pointer; } /** @return The iterator to the first or end element. */ NODISCARD FORCEINLINE Iterator Begin() { return Iterator(this, Impl.Pointer); } @@ -1003,8 +1002,8 @@ private: NODISCARD friend FORCEINLINE strong_ordering operator<=>(const TIteratorImpl& LHS, const TIteratorImpl& RHS) { return LHS.Pointer <=> RHS.Pointer; } - NODISCARD FORCEINLINE U& operator*() const { CheckThis(true); return *Pointer; } - NODISCARD FORCEINLINE U* operator->() const { CheckThis(true); return Pointer; } + NODISCARD FORCEINLINE U& operator*() const { CheckThis(true ); return *Pointer; } + NODISCARD FORCEINLINE U* operator->() const { CheckThis(false); return Pointer; } NODISCARD FORCEINLINE U& operator[](ptrdiff Index) const { TIteratorImpl Temp = *this + Index; return *Temp; } @@ -1024,8 +1023,6 @@ private: NODISCARD friend FORCEINLINE ptrdiff operator-(const TIteratorImpl& LHS, const TIteratorImpl& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; } - NODISCARD FORCEINLINE explicit operator TObserverPtr() const { CheckThis(); return TObserverPtr(Pointer); } - private: # if DO_CHECK diff --git a/Redcraft.Utility/Source/Public/Containers/ArrayView.h b/Redcraft.Utility/Source/Public/Containers/ArrayView.h index 9c4630c..dcb0789 100644 --- a/Redcraft.Utility/Source/Public/Containers/ArrayView.h +++ b/Redcraft.Utility/Source/Public/Containers/ArrayView.h @@ -11,7 +11,6 @@ #include "Containers/StaticArray.h" #include "TypeTraits/TypeTraits.h" #include "Miscellaneous/Compare.h" -#include "Memory/ObserverPointer.h" #include "Miscellaneous/AssertionMacros.h" NAMESPACE_REDCRAFT_BEGIN @@ -85,7 +84,6 @@ public: template requires (Extent == DynamicExtent || N == Extent) FORCEINLINE constexpr TArrayView(ElementType(&InArray)[N]) { - // @TODO: Refactor this to use the GetData() function. Impl.Pointer = InArray; if constexpr (Extent == DynamicExtent) @@ -96,19 +94,19 @@ public: /** Constructs an array view that is a view over the array 'InArray'. */ template requires (CConvertibleTo) - FORCEINLINE constexpr TArrayView(TStaticArray& InArray) : TArrayView(InArray.GetData().Get(), InArray.Num()) { } + FORCEINLINE constexpr TArrayView(TStaticArray& InArray) : TArrayView(InArray.GetData(), InArray.Num()) { } /** Constructs an array view that is a view over the array 'InArray'. */ template requires (CConvertibleTo) - FORCEINLINE constexpr TArrayView(const TStaticArray& InArray) : TArrayView(InArray.GetData().Get(), InArray.Num()) { } + FORCEINLINE constexpr TArrayView(const TStaticArray& InArray) : TArrayView(InArray.GetData(), InArray.Num()) { } /** Constructs an array view that is a view over the array 'InArray'. */ template requires (CConvertibleTo) - FORCEINLINE constexpr TArrayView(TArray& InArray) : TArrayView(InArray.GetData().Get(), InArray.Num()) { } + FORCEINLINE constexpr TArrayView(TArray& InArray) : TArrayView(InArray.GetData(), InArray.Num()) { } /** Constructs an array view that is a view over the array 'InArray'. */ template requires (CConvertibleTo) - FORCEINLINE constexpr TArrayView(const TArray& InArray) : TArrayView(InArray.GetData().Get(), InArray.Num()) { } + FORCEINLINE constexpr TArrayView(const TArray& InArray) : TArrayView(InArray.GetData(), InArray.Num()) { } /** Converting constructor from another array view 'InValue'. */ template requires ((Extent == DynamicExtent || N == DynamicExtent || N == Extent) && CConvertibleTo) @@ -116,8 +114,7 @@ public: { checkf(Extent == DynamicExtent || Extent == InValue.Num(), TEXT("Illegal view extent. Please check InValue.Num().")); - // @TODO: Refactor this to use the GetData() function. - Impl.Pointer = InValue.GetData().Get(); + Impl.Pointer = InValue.GetData(); if constexpr (Extent == DynamicExtent) { @@ -231,11 +228,11 @@ public: if constexpr (!CConst) { - return TArrayView(reinterpret_cast(GetData().Get()), NumBytes()); + return TArrayView(reinterpret_cast(GetData()), NumBytes()); } else { - return TArrayView(reinterpret_cast(GetData().Get()), NumBytes()); + return TArrayView(reinterpret_cast(GetData()), NumBytes()); } } @@ -244,11 +241,11 @@ public: { constexpr size_t BytesExtent = Extent != DynamicExtent ? sizeof(ElementType) * Extent : DynamicExtent; - return TArrayView(reinterpret_cast(GetData().Get()), NumBytes()); + return TArrayView(reinterpret_cast(GetData()), NumBytes()); } /** @return The pointer to the underlying element storage. */ - NODISCARD FORCEINLINE constexpr TObserverPtr GetData() const { return TObserverPtr(Impl.Pointer); } + NODISCARD FORCEINLINE constexpr ElementType* GetData() const { return Impl.Pointer; } /** @return The iterator to the first or end element. */ NODISCARD FORCEINLINE constexpr Iterator Begin() const { return Iterator(this, Impl.Pointer); } @@ -318,8 +315,8 @@ public: NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const Iterator& LHS, const Iterator& RHS) { return LHS.Pointer <=> RHS.Pointer; } - NODISCARD FORCEINLINE constexpr T& operator*() const { CheckThis(true); return *Pointer; } - NODISCARD FORCEINLINE constexpr T* operator->() const { CheckThis(true); return Pointer; } + NODISCARD FORCEINLINE constexpr T& operator*() const { CheckThis(true ); return *Pointer; } + NODISCARD FORCEINLINE constexpr T* operator->() const { CheckThis(false); return Pointer; } NODISCARD FORCEINLINE constexpr T& operator[](ptrdiff Index) const { Iterator Temp = *this + Index; return *Temp; } @@ -339,8 +336,6 @@ public: 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() const { CheckThis(); return TObserverPtr(Pointer); } - private: # if DO_CHECK diff --git a/Redcraft.Utility/Source/Public/Containers/Bitset.h b/Redcraft.Utility/Source/Public/Containers/Bitset.h index 5487396..5a33ac3 100644 --- a/Redcraft.Utility/Source/Public/Containers/Bitset.h +++ b/Redcraft.Utility/Source/Public/Containers/Bitset.h @@ -785,8 +785,8 @@ public: } /** @return The pointer to the underlying element storage. */ - NODISCARD FORCEINLINE TObserverPtr< BlockType[]> GetData() { return TObserverPtr< BlockType[]>(Impl.Pointer); } - NODISCARD FORCEINLINE TObserverPtr GetData() const { return TObserverPtr(Impl.Pointer); } + NODISCARD FORCEINLINE BlockType* GetData() { return Impl.Pointer; } + NODISCARD FORCEINLINE const BlockType* GetData() const { return Impl.Pointer; } /** @return The iterator to the first or end bit. */ NODISCARD FORCEINLINE Iterator Begin() { return Iterator(this, Impl.Pointer, 0); } diff --git a/Redcraft.Utility/Source/Public/Containers/Iterator.h b/Redcraft.Utility/Source/Public/Containers/Iterator.h index 9e9f08a..e4eb918 100644 --- a/Redcraft.Utility/Source/Public/Containers/Iterator.h +++ b/Redcraft.Utility/Source/Public/Containers/Iterator.h @@ -7,7 +7,6 @@ #include "Templates/Noncopyable.h" #include "TypeTraits/TypeTraits.h" #include "Miscellaneous/Compare.h" -#include "Memory/ObserverPointer.h" #include "Miscellaneous/AssertionMacros.h" NAMESPACE_REDCRAFT_BEGIN @@ -403,7 +402,7 @@ public: NODISCARD FORCEINLINE constexpr TIteratorReferenceType operator*() const requires (CDereferenceable) { CheckThis(true); return *Current; } - NODISCARD FORCEINLINE constexpr TIteratorPointerType operator->() const requires (CContiguousIterator) { CheckThis(true); return ToAddress(Current); } + NODISCARD FORCEINLINE constexpr TIteratorPointerType operator->() const requires (CContiguousIterator) { CheckThis(false); return ToAddress(Current); } NODISCARD FORCEINLINE constexpr TIteratorReferenceType operator[](ptrdiff Index) const requires (CRandomAccessIterator) { TCountedIterator Temp = *this + Index; return *Temp; } @@ -428,8 +427,6 @@ public: NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const TCountedIterator& LHS, FDefaultSentinel) { LHS.CheckThis(); return -LHS.Num(); } NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(FDefaultSentinel, const TCountedIterator& RHS) { RHS.CheckThis(); return RHS.Num(); } - NODISCARD FORCEINLINE constexpr explicit operator TObserverPtr() const requires (CContiguousIterator) { CheckThis(); return TObserverPtr(Current); } - NODISCARD FORCEINLINE constexpr const IteratorType& GetBase() const& { CheckThis(); return Current; } NODISCARD FORCEINLINE constexpr IteratorType GetBase() && { CheckThis(); return Current; } NODISCARD FORCEINLINE constexpr ptrdiff Num() const { CheckThis(); return Length; } diff --git a/Redcraft.Utility/Source/Public/Containers/List.h b/Redcraft.Utility/Source/Public/Containers/List.h index dd11bd8..7cc5acc 100644 --- a/Redcraft.Utility/Source/Public/Containers/List.h +++ b/Redcraft.Utility/Source/Public/Containers/List.h @@ -9,7 +9,6 @@ #include "TypeTraits/TypeTraits.h" #include "Miscellaneous/Compare.h" #include "Memory/MemoryOperator.h" -#include "Memory/ObserverPointer.h" #include "Miscellaneous/AssertionMacros.h" #include "Miscellaneous/ConstantIterator.h" diff --git a/Redcraft.Utility/Source/Public/Containers/StaticArray.h b/Redcraft.Utility/Source/Public/Containers/StaticArray.h index 52f38c8..a6bf507 100644 --- a/Redcraft.Utility/Source/Public/Containers/StaticArray.h +++ b/Redcraft.Utility/Source/Public/Containers/StaticArray.h @@ -8,7 +8,6 @@ #include "Containers/Iterator.h" #include "TypeTraits/TypeTraits.h" #include "Miscellaneous/Compare.h" -#include "Memory/ObserverPointer.h" #include "Miscellaneous/AssertionMacros.h" NAMESPACE_REDCRAFT_BEGIN @@ -67,8 +66,8 @@ public: } /** @return The pointer to the underlying element storage. */ - NODISCARD FORCEINLINE constexpr TObserverPtr< ElementType[]> GetData() { return TObserverPtr< ElementType[]>(_); } - NODISCARD FORCEINLINE constexpr TObserverPtr GetData() const { return TObserverPtr(_); } + NODISCARD FORCEINLINE constexpr ElementType* GetData() { return _; } + NODISCARD FORCEINLINE constexpr const ElementType* GetData() const { return _; } /** @return The iterator to the first or end element. */ NODISCARD FORCEINLINE constexpr Iterator Begin() { return Iterator(this, _); } @@ -151,8 +150,8 @@ private: NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const TIteratorImpl& LHS, const TIteratorImpl& RHS) { return LHS.Pointer <=> RHS.Pointer; } - NODISCARD FORCEINLINE constexpr U& operator*() const { CheckThis(true); return *Pointer; } - NODISCARD FORCEINLINE constexpr U* operator->() const { CheckThis(true); return Pointer; } + NODISCARD FORCEINLINE constexpr U& operator*() const { CheckThis(true ); return *Pointer; } + NODISCARD FORCEINLINE constexpr U* operator->() const { CheckThis(false); return Pointer; } NODISCARD FORCEINLINE constexpr U& operator[](ptrdiff Index) const { TIteratorImpl Temp = *this + Index; return *Temp; } @@ -172,8 +171,6 @@ private: NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const TIteratorImpl& LHS, const TIteratorImpl& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; } - NODISCARD FORCEINLINE constexpr explicit operator TObserverPtr() const { CheckThis(); return TObserverPtr(Pointer); } - private: # if DO_CHECK diff --git a/Redcraft.Utility/Source/Public/Containers/StaticBitset.h b/Redcraft.Utility/Source/Public/Containers/StaticBitset.h index 39c8da8..2858ab5 100644 --- a/Redcraft.Utility/Source/Public/Containers/StaticBitset.h +++ b/Redcraft.Utility/Source/Public/Containers/StaticBitset.h @@ -435,8 +435,8 @@ public: } /** @return The pointer to the underlying element storage. */ - NODISCARD FORCEINLINE constexpr TObserverPtr< BlockType[]> GetData() { return TObserverPtr< BlockType[]>(Impl); } - NODISCARD FORCEINLINE constexpr TObserverPtr GetData() const { return TObserverPtr(Impl); } + NODISCARD FORCEINLINE constexpr BlockType* GetData() { return Impl; } + NODISCARD FORCEINLINE constexpr const BlockType* GetData() const { return Impl; } /** @return The iterator to the first or end bit. */ NODISCARD FORCEINLINE constexpr Iterator Begin() { return Iterator(this, Impl, 0); } diff --git a/Redcraft.Utility/Source/Public/Memory/ObserverPointer.h b/Redcraft.Utility/Source/Public/Memory/ObserverPointer.h deleted file mode 100644 index a1e0b35..0000000 --- a/Redcraft.Utility/Source/Public/Memory/ObserverPointer.h +++ /dev/null @@ -1,155 +0,0 @@ -#pragma once - -#include "CoreTypes.h" -#include "Templates/Utility.h" -#include "Templates/TypeHash.h" -#include "Memory/PointerTraits.h" -#include "TypeTraits/TypeTraits.h" -#include "Miscellaneous/Compare.h" - -NAMESPACE_REDCRAFT_BEGIN -NAMESPACE_MODULE_BEGIN(Redcraft) -NAMESPACE_MODULE_BEGIN(Utility) - -template requires (CObject && !CBoundedArray) -class TObserverPtr; - -NAMESPACE_PRIVATE_BEGIN - -template struct TIsTObserverPtr : FFalse { }; -template struct TIsTObserverPtr> : FTrue { }; - -NAMESPACE_PRIVATE_END - -template -concept CTObserverPtr = NAMESPACE_PRIVATE::TIsTObserverPtr>::Value; - -template requires (CObject && !CBoundedArray) -class TObserverPtr -{ -public: - - using ElementType = T; - - FORCEINLINE constexpr TObserverPtr() : Pointer(nullptr) { } - - FORCEINLINE constexpr TObserverPtr(nullptr_t) : TObserverPtr() { } - - FORCEINLINE constexpr explicit TObserverPtr(T* InPtr) : Pointer(InPtr) { } - - FORCEINLINE constexpr TObserverPtr(const TObserverPtr&) = default; - FORCEINLINE constexpr TObserverPtr(TObserverPtr&&) = default; - - template requires (CConvertibleTo && !CArray) - FORCEINLINE constexpr TObserverPtr(TObserverPtr InValue) : Pointer(InValue.Pointer) { } - - FORCEINLINE constexpr ~TObserverPtr() = default; - - FORCEINLINE constexpr TObserverPtr& operator=(const TObserverPtr&) = default; - FORCEINLINE constexpr TObserverPtr& operator=(TObserverPtr&&) = default; - - NODISCARD friend FORCEINLINE constexpr bool operator==(const TObserverPtr& LHS, const TObserverPtr& RHS) { return LHS.Get() == RHS.Get(); } - - NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const TObserverPtr& LHS, const TObserverPtr& RHS) { return LHS.Get() <=> RHS.Get(); } - - NODISCARD FORCEINLINE constexpr bool operator==(T* InPtr) const& { return Get() == InPtr; } - - NODISCARD FORCEINLINE constexpr strong_ordering operator<=>(T* InPtr) const& { return Get() <=> InPtr; } - - NODISCARD FORCEINLINE constexpr T* Release() { return Exchange(Pointer, nullptr); } - - FORCEINLINE constexpr void Reset(T* InPtr = nullptr) { Pointer = InPtr; } - - NODISCARD FORCEINLINE constexpr T* Get() const { return Pointer; } - - NODISCARD FORCEINLINE constexpr bool IsValid() const { return Get() != nullptr; } - NODISCARD FORCEINLINE constexpr explicit operator bool() const { return Get() != nullptr; } - - NODISCARD FORCEINLINE constexpr T& operator*() const { checkf(IsValid(), TEXT("Read access violation. Please check IsValid().")); return *Get(); } - NODISCARD FORCEINLINE constexpr T* operator->() const { checkf(IsValid(), TEXT("Read access violation. Please check IsValid().")); return Get(); } - - NODISCARD FORCEINLINE constexpr explicit operator ElementType*() const { return Get(); } - - NODISCARD friend FORCEINLINE constexpr size_t GetTypeHash(const TObserverPtr& A) { return GetTypeHash(A.Get()); } - - friend FORCEINLINE constexpr void Swap(TObserverPtr& A, TObserverPtr& B) { Swap(A.Pointer, B.Pointer); } - -private: - - T* Pointer; - - template requires (CObject && !CBoundedArray) - friend class TObserverPtr; - -}; - -template -class TObserverPtr -{ -public: - - using ElementType = T; - - FORCEINLINE constexpr TObserverPtr() : Pointer(nullptr) { } - - FORCEINLINE constexpr TObserverPtr(nullptr_t) : TObserverPtr() { } - - template requires (CPointer && CConvertibleTo(*)[], T(*)[]>) - FORCEINLINE constexpr explicit TObserverPtr(U InPtr) : Pointer(InPtr) { } - - FORCEINLINE constexpr TObserverPtr(const TObserverPtr&) = default; - FORCEINLINE constexpr TObserverPtr(TObserverPtr&&) = default; - - template requires (CConvertibleTo(*)[], T(*)[]> && CArray) - FORCEINLINE constexpr TObserverPtr(TObserverPtr InValue) : Pointer(InValue.Pointer) { } - - FORCEINLINE constexpr ~TObserverPtr() = default; - - FORCEINLINE constexpr TObserverPtr& operator=(const TObserverPtr&) = default; - FORCEINLINE constexpr TObserverPtr& operator=(TObserverPtr&&) = default; - - NODISCARD friend FORCEINLINE constexpr bool operator==(const TObserverPtr& LHS, const TObserverPtr& RHS) { return LHS.Get() == RHS.Get(); } - - NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const TObserverPtr& LHS, const TObserverPtr& RHS) { return LHS.Get() <=> RHS.Get(); } - - template requires (CNullPointer || (CPointer && CConvertibleTo(*)[], T(*)[]>)) - NODISCARD FORCEINLINE constexpr bool operator==(U InPtr) const& { return Get() == InPtr; } - - template requires (CNullPointer || (CPointer && CConvertibleTo(*)[], T(*)[]>)) - NODISCARD FORCEINLINE constexpr strong_ordering operator<=>(U InPtr) const& { return Get() <=> InPtr; } - - NODISCARD FORCEINLINE constexpr T* Release() { return Exchange(Pointer, nullptr); } - - template requires (CNullPointer || (CPointer && CConvertibleTo(*)[], T(*)[]>)) - FORCEINLINE constexpr void Reset(U InPtr = nullptr) { Pointer = InPtr; } - - NODISCARD FORCEINLINE constexpr T* Get() const { return Pointer; } - - NODISCARD FORCEINLINE constexpr bool IsValid() const { return Get() != nullptr; } - NODISCARD FORCEINLINE constexpr explicit operator bool() const { return Get() != nullptr; } - - NODISCARD FORCEINLINE constexpr T& operator[](size_t Index) const { checkf(IsValid(), TEXT("Read access violation. Please check IsValid().")); return Get()[Index]; } - - NODISCARD FORCEINLINE constexpr explicit operator ElementType*() const { return Get(); } - - NODISCARD friend FORCEINLINE constexpr size_t GetTypeHash(const TObserverPtr& A) { return GetTypeHash(A.Get()); } - - friend FORCEINLINE constexpr void Swap(TObserverPtr& A, TObserverPtr& B) { Swap(A.Pointer, B.Pointer); } - -private: - - T* Pointer; - - template requires (CObject && !CBoundedArray) - friend class TObserverPtr; - -}; - -template requires (CObject && !CBoundedArray) -NODISCARD FORCEINLINE constexpr TObserverPtr MakeObserver(TRemoveExtent* InPtr) { return TObserverPtr(InPtr); } - -DEFINE_TPointerTraits(TObserverPtr); - -NAMESPACE_MODULE_END(Utility) -NAMESPACE_MODULE_END(Redcraft) -NAMESPACE_REDCRAFT_END diff --git a/Redcraft.Utility/Source/Public/Miscellaneous/ConstantIterator.h b/Redcraft.Utility/Source/Public/Miscellaneous/ConstantIterator.h index 40d0066..4cd9b2b 100644 --- a/Redcraft.Utility/Source/Public/Miscellaneous/ConstantIterator.h +++ b/Redcraft.Utility/Source/Public/Miscellaneous/ConstantIterator.h @@ -157,8 +157,8 @@ public: NODISCARD FORCEINLINE constexpr strong_ordering operator<=>(FDefaultSentinel) const& { return static_cast(0) <=> Length; } - NODISCARD FORCEINLINE constexpr const TRemoveReference& operator*() const { CheckThis(true); return *Current; } - NODISCARD FORCEINLINE constexpr const TRemoveReference* operator->() const { CheckThis(true); return ToAddress(Current); } + NODISCARD FORCEINLINE constexpr const TRemoveReference& operator*() const { CheckThis(true ); return *Current; } + NODISCARD FORCEINLINE constexpr const TRemoveReference* operator->() const { CheckThis(false); return ToAddress(Current); } NODISCARD FORCEINLINE constexpr const TRemoveReference& operator[](ptrdiff) const { return *this; } diff --git a/Redcraft.Utility/Source/Public/String/String.h b/Redcraft.Utility/Source/Public/String/String.h index 30c4bce..20bab62 100644 --- a/Redcraft.Utility/Source/Public/String/String.h +++ b/Redcraft.Utility/Source/Public/String/String.h @@ -12,7 +12,6 @@ #include "TypeTraits/TypeTraits.h" #include "Miscellaneous/Compare.h" #include "Memory/MemoryOperator.h" -#include "Memory/ObserverPointer.h" #include "Miscellaneous/AssertionMacros.h" NAMESPACE_REDCRAFT_BEGIN @@ -129,9 +128,9 @@ public: NODISCARD friend FORCEINLINE auto operator<=>(const TString& LHS, const TString& RHS) { return TStringView(LHS) <=> TStringView(RHS); } /** Compares the contents of 'LHS' and 'RHS' lexicographically. */ - NODISCARD friend FORCEINLINE auto operator<=>(const TString& LHS, ElementType RHS) { return TStringView(LHS) <=> RHS; } + NODISCARD friend FORCEINLINE auto operator<=>(const TString& LHS, ElementType RHS) { return TStringView(LHS) <=> RHS; } NODISCARD friend FORCEINLINE auto operator<=>(const TString& LHS, const ElementType* RHS) { return TStringView(LHS) <=> RHS; } - NODISCARD friend FORCEINLINE auto operator<=>(ElementType LHS, const TString& RHS) { return LHS <=> TStringView(RHS); } + NODISCARD friend FORCEINLINE auto operator<=>( ElementType LHS, const TString& RHS) { return LHS <=> TStringView(RHS); } NODISCARD friend FORCEINLINE auto operator<=>(const ElementType* LHS, const TString& RHS) { return LHS <=> TStringView(RHS); } /** Inserts 'InValue' before 'Index' in the string. */ @@ -663,8 +662,8 @@ public: NAMESPACE_STD::mbstate_t State = NAMESPACE_STD::mbstate_t(); - const char* BeginFrom = View.GetData().Get(); - const char* EndFrom = BeginFrom + View.Num(); + const char* BeginFrom = ToAddress(View.Begin()); + const char* EndFrom = ToAddress(View.End()); wchar Buffer[FWChar::MaxCodeUnitLength]; @@ -711,8 +710,8 @@ public: NAMESPACE_STD::mbstate_t State = NAMESPACE_STD::mbstate_t(); - const wchar* BeginFrom = View.GetData().Get(); - const wchar* EndFrom = BeginFrom + View.Num(); + const wchar* BeginFrom = ToAddress(View.Begin()); + const wchar* EndFrom = ToAddress(View.End()); char Buffer[FChar::MaxCodeUnitLength]; @@ -836,7 +835,7 @@ public: // wchar -> unicodechar -> ... for Linux else if constexpr (PLATFORM_LINUX && CSameAs) { - return Self(Self, TStringView(reinterpret_cast(View.GetData().Get()), View.Num())); + return Self(Self, TStringView(reinterpret_cast(View.GetData()), View.Num())); } // unicodechar u32char -> u8char @@ -930,7 +929,7 @@ public: if constexpr (PLATFORM_LINUX && (CSameAs || CSameAs)) { - return Self(Self, TStringView(reinterpret_cast(View.GetData().Get()), View.Num())); + return Self(Self, TStringView(reinterpret_cast(View.GetData()), View.Num())); } else NativeData.Insert(NativeData.End(), View.Begin(), View.End()); @@ -967,7 +966,7 @@ public: } /** @return The non-modifiable standard C character string version of the string. */ - NODISCARD FORCEINLINE const ElementType* ToCString() const { return NativeData.GetData().Get(); } + NODISCARD FORCEINLINE const ElementType* ToCString() const { return GetData(); } /** @return The target-encoded string from the T-encoded string. */ NODISCARD FORCEINLINE auto ToString() const { return EncodeTo(); } @@ -990,8 +989,8 @@ public: FORCEINLINE void Shrink() { NativeData.Shrink(); } /** @return The pointer to the underlying character storage. */ - NODISCARD FORCEINLINE TObserverPtr< ElementType[]> GetData() { return NativeData.GetData(); } - NODISCARD FORCEINLINE TObserverPtr GetData() const { return NativeData.GetData(); } + NODISCARD FORCEINLINE ElementType* GetData() { return NativeData.GetData(); } + NODISCARD FORCEINLINE const ElementType* GetData() const { return NativeData.GetData(); } /** @return The iterator to the first or end character. */ NODISCARD FORCEINLINE Iterator Begin() { return NativeData.Begin(); } @@ -1068,7 +1067,7 @@ using FU32String = TString; using FUnicodeString = TString; template template constexpr TStringView::TStringView(const TString& InString) - : TStringView(InString.GetData().Get(), InString.Num()) { } + : TStringView(InString.GetData(), InString.Num()) { } NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) diff --git a/Redcraft.Utility/Source/Public/String/StringView.h b/Redcraft.Utility/Source/Public/String/StringView.h index 6d33abe..9d4c7d9 100644 --- a/Redcraft.Utility/Source/Public/String/StringView.h +++ b/Redcraft.Utility/Source/Public/String/StringView.h @@ -11,7 +11,6 @@ #include "TypeTraits/TypeTraits.h" #include "Miscellaneous/Compare.h" #include "Memory/MemoryOperator.h" -#include "Memory/ObserverPointer.h" #include "Miscellaneous/AssertionMacros.h" #include "Miscellaneous/ConstantIterator.h" @@ -155,7 +154,7 @@ public: Count = Num() - Offset; } - Memory::CopyAssign(Dest, GetData().Get() + Offset, Count); + Memory::CopyAssign(Dest, GetData() + Offset, Count); return Count; } @@ -405,7 +404,7 @@ public: } /** @return The pointer to the underlying element storage. */ - NODISCARD FORCEINLINE constexpr TObserverPtr GetData() const { return NativeData.GetData(); } + NODISCARD FORCEINLINE constexpr const ElementType* GetData() const { return NativeData.GetData(); } /** @return The iterator to the first or end element. */ NODISCARD FORCEINLINE constexpr Iterator Begin() const { return NativeData.Begin(); } diff --git a/Redcraft.Utility/Source/Public/Templates/Container.h b/Redcraft.Utility/Source/Public/Templates/Container.h index a34a70d..6730c8e 100644 --- a/Redcraft.Utility/Source/Public/Templates/Container.h +++ b/Redcraft.Utility/Source/Public/Templates/Container.h @@ -3,44 +3,43 @@ #include "CoreTypes.h" #include "Templates/TypeHash.h" #include "TypeTraits/Swappable.h" -#include "Memory/ObserverPointer.h" NAMESPACE_REDCRAFT_BEGIN NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Utility) /** @return The pointer to the container element storage. */ -template requires (requires(T&& Container) { { Container.GetData() } -> CTObserverPtr; }) -FORCEINLINE constexpr decltype(auto) GetData(T&& Container) +template requires (requires(T&& Container) { { Container.GetData() } -> CPointer; }) +FORCEINLINE constexpr auto GetData(T&& Container) { return Container.GetData(); } /** Overloads the GetData algorithm for arrays. */ -template FORCEINLINE constexpr TObserverPtr< T[]> GetData( T(& Container)[N]) { return TObserverPtr< T[]>(Container); } -template FORCEINLINE constexpr TObserverPtr< T[]> GetData( T(&& Container)[N]) { return TObserverPtr< T[]>(Container); } -template FORCEINLINE constexpr TObserverPtr GetData(const T(& Container)[N]) { return TObserverPtr(Container); } -template FORCEINLINE constexpr TObserverPtr GetData(const T(&& Container)[N]) { return TObserverPtr(Container); } +template FORCEINLINE constexpr T* GetData( T(& Container)[N]) { return Container; } +template FORCEINLINE constexpr T* GetData( T(&& Container)[N]) { return Container; } +template FORCEINLINE constexpr const T* GetData(const T(& Container)[N]) { return Container; } +template FORCEINLINE constexpr const T* GetData(const T(&& Container)[N]) { return Container; } /** Overloads the GetData algorithm for initializer_list. */ template -FORCEINLINE constexpr TObserverPtr GetData(initializer_list Container) +FORCEINLINE constexpr const T* GetData(initializer_list Container) { - return TObserverPtr(Container.begin()); + return Container.begin(); } /** @return The number of elements in the container. */ template requires (requires(T&& Container) { { Container.Num() } -> CConvertibleTo; }) -FORCEINLINE constexpr decltype(auto) GetNum(T&& Container) +FORCEINLINE constexpr auto GetNum(T&& Container) { return Container.Num(); } /** Overloads the GetNum algorithm for arrays. */ -template FORCEINLINE constexpr size_t GetNum( T(& Container)[N]) { return N; } -template FORCEINLINE constexpr size_t GetNum( T(&& Container)[N]) { return N; } -template FORCEINLINE constexpr size_t GetNum(const T(& Container)[N]) { return N; } -template FORCEINLINE constexpr size_t GetNum(const T(&& Container)[N]) { return N; } +template FORCEINLINE constexpr size_t GetNum( T(& )[N]) { return N; } +template FORCEINLINE constexpr size_t GetNum( T(&&)[N]) { return N; } +template FORCEINLINE constexpr size_t GetNum(const T(& )[N]) { return N; } +template FORCEINLINE constexpr size_t GetNum(const T(&&)[N]) { return N; } /** Overloads the GetNum algorithm for initializer_list. */ template @@ -69,7 +68,7 @@ FORCEINLINE constexpr size_t GetTypeHash(T(&A)[N]) { Result = HashCombine(Result, GetTypeHash(A[Index])); } - + return Result; }