refactor(templates): refactor the return value of GetData() to TObserverPtr

This commit is contained in:
_Redstone_c_ 2023-02-26 19:01:32 +08:00
parent e9f780622f
commit 343ff8d240
3 changed files with 21 additions and 30 deletions

View File

@ -176,7 +176,7 @@ private:
NAMESPACE_PRIVATE_END 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. */ /** 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 <typename T, typename Allocator = FDefaultAllocator> requires (!CConst<T> && CDestructible<T> && CInstantiableAllocator<Allocator>) template <CObject T, typename Allocator = FDefaultAllocator> requires (!CConst<T> && CDestructible<T> && CInstantiableAllocator<Allocator>)
class TArray final class TArray final
{ {
public: public:
@ -412,20 +412,20 @@ public:
Storage.GetMax() = NumToAllocate; Storage.GetMax() = NumToAllocate;
Storage.GetPointer() = Storage.GetAllocator().Allocate(Max()); Storage.GetPointer() = Storage.GetAllocator().Allocate(Max());
Memory::CopyConstruct<ElementType>(Storage.GetPointer(), NAMESPACE_REDCRAFT::GetData(IL), Num()); Memory::CopyConstruct<ElementType>(Storage.GetPointer(), NAMESPACE_REDCRAFT::GetData(IL).Get(), Num());
return *this; return *this;
} }
if (GetNum(IL) <= Num()) if (GetNum(IL) <= Num())
{ {
Memory::CopyAssign(Storage.GetPointer(), NAMESPACE_REDCRAFT::GetData(IL), GetNum(IL)); Memory::CopyAssign(Storage.GetPointer(), NAMESPACE_REDCRAFT::GetData(IL).Get(), GetNum(IL));
Memory::Destruct(Storage.GetPointer() + GetNum(IL), Num() - GetNum(IL)); Memory::Destruct(Storage.GetPointer() + GetNum(IL), Num() - GetNum(IL));
} }
else if (GetNum(IL) <= Max()) else if (GetNum(IL) <= Max())
{ {
Memory::CopyAssign(Storage.GetPointer(), NAMESPACE_REDCRAFT::GetData(IL), Num()); Memory::CopyAssign(Storage.GetPointer(), NAMESPACE_REDCRAFT::GetData(IL).Get(), Num());
Memory::CopyConstruct<ElementType>(Storage.GetPointer() + Num(), NAMESPACE_REDCRAFT::GetData(IL) + Num(), GetNum(IL) - Num()); Memory::CopyConstruct<ElementType>(Storage.GetPointer() + Num(), NAMESPACE_REDCRAFT::GetData(IL).Get() + Num(), GetNum(IL) - Num());
} }
else check_no_entry(); else check_no_entry();
@ -1208,6 +1208,12 @@ private:
}; };
template <typename I, typename S>
TArray(I, S) -> TArray<TIteratorElementType<I>>;
template <typename T>
TArray(initializer_list<T>) -> TArray<T>;
NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft) NAMESPACE_MODULE_END(Redcraft)
NAMESPACE_REDCRAFT_END NAMESPACE_REDCRAFT_END

View File

@ -68,8 +68,7 @@ public:
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 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 operator ElementType*() { return Get(); } NODISCARD FORCEINLINE constexpr explicit operator ElementType*() const { return Get(); }
NODISCARD FORCEINLINE constexpr operator const ElementType*() const { return Get(); }
NODISCARD friend FORCEINLINE constexpr size_t GetTypeHash(const TObserverPtr& A) { return GetTypeHash(A.Get()); } NODISCARD friend FORCEINLINE constexpr size_t GetTypeHash(const TObserverPtr& A) { return GetTypeHash(A.Get()); }
@ -131,8 +130,7 @@ public:
NODISCARD FORCEINLINE constexpr T& operator[](size_t Index) const { checkf(IsValid(), TEXT("Read access violation. Please check IsValid().")); return Get()[Index]; } NODISCARD FORCEINLINE constexpr T& operator[](size_t Index) const { checkf(IsValid(), TEXT("Read access violation. Please check IsValid().")); return Get()[Index]; }
NODISCARD FORCEINLINE constexpr operator ElementType*() { return Get(); } NODISCARD FORCEINLINE constexpr explicit operator ElementType*() const { return Get(); }
NODISCARD FORCEINLINE constexpr operator const ElementType*() const { return Get(); }
NODISCARD friend FORCEINLINE constexpr size_t GetTypeHash(const TObserverPtr& A) { return GetTypeHash(A.Get()); } NODISCARD friend FORCEINLINE constexpr size_t GetTypeHash(const TObserverPtr& A) { return GetTypeHash(A.Get()); }

View File

@ -3,36 +3,30 @@
#include "CoreTypes.h" #include "CoreTypes.h"
#include "Templates/TypeHash.h" #include "Templates/TypeHash.h"
#include "TypeTraits/Swappable.h" #include "TypeTraits/Swappable.h"
#include "Memory/ObserverPointer.h"
NAMESPACE_REDCRAFT_BEGIN NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility) NAMESPACE_MODULE_BEGIN(Utility)
/** @return The pointer to the container element storage. */ /** @return The pointer to the container element storage. */
template <typename T> requires (requires(T&& Container) { { Container.GetData() } -> CPointer; }) template <typename T> requires (requires(T&& Container) { { Container.GetData() } -> CTObserverPtr; })
FORCEINLINE constexpr decltype(auto) GetData(T&& Container) FORCEINLINE constexpr decltype(auto) GetData(T&& Container)
{ {
return Container.GetData(); return Container.GetData();
} }
/** Overloads the GetData algorithm for arrays. */ /** Overloads the GetData algorithm for arrays. */
template <typename T, size_t N> FORCEINLINE constexpr T* GetData( T(& Container)[N]) { return Container; } template <typename T, size_t N> FORCEINLINE constexpr TObserverPtr< T[]> GetData( T(& Container)[N]) { return TObserverPtr< T[]>(Container); }
template <typename T, size_t N> FORCEINLINE constexpr T* GetData( T(&& Container)[N]) { return Container; } template <typename T, size_t N> FORCEINLINE constexpr TObserverPtr< T[]> GetData( T(&& Container)[N]) { return TObserverPtr< T[]>(Container); }
template <typename T, size_t N> FORCEINLINE constexpr const T* GetData(const T(& Container)[N]) { return Container; } template <typename T, size_t N> FORCEINLINE constexpr TObserverPtr<const T[]> GetData(const T(& Container)[N]) { return TObserverPtr<const T[]>(Container); }
template <typename T, size_t N> FORCEINLINE constexpr const T* GetData(const T(&& Container)[N]) { return Container; } template <typename T, size_t N> FORCEINLINE constexpr TObserverPtr<const T[]> GetData(const T(&& Container)[N]) { return TObserverPtr<const T[]>(Container); }
/** Overloads the GetData algorithm for T::data(). */
template <typename T> requires (requires(T&& Container) { { Container.data() } -> CPointer; })
FORCEINLINE constexpr decltype(auto) GetData(T&& Container)
{
return Container.data();
}
/** Overloads the GetData algorithm for initializer_list. */ /** Overloads the GetData algorithm for initializer_list. */
template <typename T> template <typename T>
FORCEINLINE constexpr const T* GetData(initializer_list<T> Container) FORCEINLINE constexpr TObserverPtr<const T[]> GetData(initializer_list<T> Container)
{ {
return Container.begin(); return TObserverPtr<const T[]>(Container.begin());
} }
/** @return The number of elements in the container. */ /** @return The number of elements in the container. */
@ -48,13 +42,6 @@ template <typename T, size_t N> FORCEINLINE constexpr size_t GetNum( T(&& C
template <typename T, size_t N> FORCEINLINE constexpr size_t GetNum(const T(& Container)[N]) { return N; } template <typename T, size_t N> FORCEINLINE constexpr size_t GetNum(const T(& Container)[N]) { return N; }
template <typename T, size_t N> FORCEINLINE constexpr size_t GetNum(const T(&& Container)[N]) { return N; } template <typename T, size_t N> FORCEINLINE constexpr size_t GetNum(const T(&& Container)[N]) { return N; }
/** Overloads the GetNum algorithm for T::size(). */
template <typename T> requires (requires(T&& Container) { { Container.size() } -> CConvertibleTo<size_t>; })
FORCEINLINE constexpr decltype(auto) GetNum(T&& Container)
{
return Container.size();
}
/** Overloads the GetNum algorithm for initializer_list. */ /** Overloads the GetNum algorithm for initializer_list. */
template <typename T> template <typename T>
FORCEINLINE constexpr size_t GetNum(initializer_list<T> Container) FORCEINLINE constexpr size_t GetNum(initializer_list<T> Container)