feat(templates): add TPointerTraits and the corresponding testing

This commit is contained in:
_Redstone_c_ 2023-01-16 19:27:39 +08:00
parent 96e6fb22cd
commit 52cd65dbad
5 changed files with 96 additions and 0 deletions

View File

@ -2297,6 +2297,29 @@ void TestMiscTemplates()
always_check(TestFunctionB(&ObjectA) == 1);
always_check(TestFunctionB(AddressOf(ObjectA)) == 0);
always_check(AddressOf(TestMiscTemplates) == &TestMiscTemplates);
always_check(!TPointerTraits<int64>::bIsPointer);
always_check(TPointerTraits<int64*>::bIsPointer);
always_check((CSameAs<TPointerTraits<int64*>::PointerType, int64*>));
always_check((CSameAs<TPointerTraits<int64*>::ElementType, int64>));
always_check(TPointerTraits<int64*>::ToAddress(nullptr) == nullptr);
always_check(TPointerTraits<int64(*)[]>::bIsPointer);
always_check((CSameAs<TPointerTraits<int64(*)[]>::PointerType, int64(*)[]>));
always_check((CSameAs<TPointerTraits<int64(*)[]>::ElementType, int64>));
always_check(TPointerTraits<int64*>::ToAddress(nullptr) == nullptr);
always_check(TPointerTraits<TSharedPtr<int64>>::bIsPointer);
always_check((CSameAs<TPointerTraits<TSharedPtr<int64>>::PointerType, TSharedPtr<int64>>));
always_check((CSameAs<TPointerTraits<TSharedPtr<int64>>::ElementType, int64>));
always_check(TPointerTraits<TSharedPtr<int64>>::ToAddress(nullptr) == nullptr);
always_check(TPointerTraits<TSharedPtr<int64[]>>::bIsPointer);
always_check((CSameAs<TPointerTraits<TSharedPtr<int64[]>>::PointerType, TSharedPtr<int64[]>>));
always_check((CSameAs<TPointerTraits<TSharedPtr<int64[]>>::ElementType, int64>));
always_check(TPointerTraits<TSharedPtr<int64[]>>::ToAddress(nullptr) == nullptr);
}
NAMESPACE_END(Testing)

View File

@ -0,0 +1,64 @@
#pragma once
#include "CoreTypes.h"
NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
/** The class template provides the standardized way to access certain properties of pointer-like types. */
template <typename T>
struct TPointerTraits
{
static constexpr bool bIsPointer = false;
};
/** A specialization of TPointerTraits is provided for pointer types T*. */
template <typename T>
struct TPointerTraits<T*>
{
static constexpr bool bIsPointer = true;
using PointerType = T*;
using ElementType = T;
static FORCEINLINE constexpr ElementType* ToAddress(PointerType InPtr)
{
return InPtr;
}
};
/** A specialization of TPointerTraits is provided for array pointer types T(*)[]. */
template <typename T>
struct TPointerTraits<T(*)[]>
{
static constexpr bool bIsPointer = true;
using PointerType = T(*)[];
using ElementType = T;
static FORCEINLINE constexpr ElementType* ToAddress(PointerType InPtr)
{
return InPtr;
}
};
/** A specialization of TPointerTraits is provided for pointer-like type. */
#define DEFINE_TPointerTraits(TPtr) \
template <typename T> \
struct TPointerTraits<TPtr<T>> \
{ \
static constexpr bool bIsPointer = true; \
\
using PointerType = TPtr<T>; \
using ElementType = TPtr<T>::ElementType; \
\
static FORCEINLINE constexpr ElementType* ToAddress(const PointerType& InPtr) \
{ \
return InPtr.Get(); \
} \
};
NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft)
NAMESPACE_REDCRAFT_END

View File

@ -8,6 +8,7 @@
#include "Memory/MemoryOperator.h"
#include "Templates/Noncopyable.h"
#include "TypeTraits/PrimaryType.h"
#include "Templates/PointerTraits.h"
#include "Templates/UniquePointer.h"
#include "TypeTraits/Miscellaneous.h"
#include "TypeTraits/TypeProperties.h"
@ -1896,6 +1897,9 @@ NODISCARD FORCEINLINE TSharedPtr<T> ReinterpretCast(TSharedPtr<U>&& InValue)
return TSharedPtr<T>(MoveTemp(InValue), reinterpret_cast<T*>(InValue.Get()));
}
DEFINE_TPointerTraits(TSharedRef);
DEFINE_TPointerTraits(TSharedPtr);
NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft)
NAMESPACE_REDCRAFT_END

View File

@ -15,5 +15,6 @@
#include "Templates/Function.h"
#include "Templates/Atomic.h"
#include "Templates/ScopeHelper.h"
#include "Templates/PointerTraits.h"
#include "Templates/UniquePointer.h"
#include "Templates/SharedPointer.h"

View File

@ -5,6 +5,7 @@
#include "Templates/Utility.h"
#include "Templates/Noncopyable.h"
#include "TypeTraits/PrimaryType.h"
#include "Templates/PointerTraits.h"
#include "TypeTraits/Miscellaneous.h"
#include "TypeTraits/TypeProperties.h"
#include "TypeTraits/SupportedOperations.h"
@ -682,6 +683,9 @@ void MakeUnique(Ts&&...) = delete;
static_assert(sizeof(TUniqueRef<int32>) == sizeof(int32*), "The byte size of TUniqueRef is unexpected");
static_assert(sizeof(TUniquePtr<int32>) == sizeof(int32*), "The byte size of TUniquePtr is unexpected");
DEFINE_TPointerTraits(TUniqueRef);
DEFINE_TPointerTraits(TUniquePtr);
NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft)
NAMESPACE_REDCRAFT_END