feat(templates): add TPointerTraits and the corresponding testing
This commit is contained in:
parent
96e6fb22cd
commit
52cd65dbad
@ -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)
|
||||
|
64
Redcraft.Utility/Source/Public/Templates/PointerTraits.h
Normal file
64
Redcraft.Utility/Source/Public/Templates/PointerTraits.h
Normal 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
|
@ -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
|
||||
|
@ -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"
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user