feat(templates): improve Swap functions and peripheral tools

This commit is contained in:
_Redstone_c_ 2022-02-04 16:34:41 +08:00
parent bafcadc68f
commit b0c170f803
6 changed files with 54 additions and 10 deletions

View File

@ -179,7 +179,7 @@ void TestConcepts()
always_check(CSwappable<int32>);
always_check(CSwappable<FTestStructG>);
always_check(CSwappable<FTestStructN>);
// always_check(!CSwappable<FSingleton>);
always_check(!CSwappable<FSingleton>);
always_check((CSwappableWith<int32&, int32&>));

View File

@ -1,6 +1,7 @@
#include "Testing/TypeTraitsTesting.h"
#include "Misc/AssertionMacros.h"
#include "TypeTraits/TypeTraits.h"
#include "Templates/Templates.h"
NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
@ -435,6 +436,15 @@ void TestTypeTraits()
always_check((TIsSame<int64, TCommonReference<int8, int32, int64>::Type>::Value));
always_check((TIsSame<double, TCommonReference<float, double>::Type>::Value));
// Swappable.h
always_check(TIsSwappable<int32>::Value);
always_check(TIsSwappable<FTestStructG>::Value);
always_check(TIsSwappable<FTestStructN>::Value);
always_check(!TIsSwappable<FSingleton>::Value);
always_check((TIsSwappableWith<int32&, int32&>::Value));
}
NAMESPACE_MODULE_END(Utility)

View File

@ -63,18 +63,20 @@ FORCEINLINE T&& Forward(typename TRemoveReference<T>::Type&& Obj)
return (T&&)Obj;
}
template <typename T>
FORCEINLINE void Swap(T& A, T& B)
template <typename T> requires TIsMoveConstructible<T>::Value && TIsMoveAssignable<T>::Value
constexpr void Swap(T& A, T& B)
{
T Temp = MoveTemp(A);
A = MoveTemp(B);
B = MoveTemp(Temp);
}
template <typename T>
FORCEINLINE void Exchange(T& A, T& B)
template <typename T, typename U = T> requires TIsMoveConstructible<T>::Value && TIsAssignable<T&, U>::Value
constexpr T Exchange(T& A, U&& B)
{
Swap(A, B);
T Temp = MoveTemp(A);
A = Forward<U>(B);
return Temp;
}
template <typename T>

View File

@ -23,21 +23,17 @@ template <typename T> struct TIsTriviallyMoveAssignable : TBoolConstant<NA
template <typename T> struct TIsTriviallyDestructible : TBoolConstant<NAMESPACE_STD::is_trivially_destructible_v<T>> { };
template <typename T> struct THasVirtualDestructor : TBoolConstant<NAMESPACE_STD::has_virtual_destructor_v<T>> { };
//template <typename T> struct TIsSwappable;
//template <typename T> struct TIsNothrowDefaultConstructible;
//template <typename T> struct TIsNothrowCopyConstructible;
//template <typename T> struct TIsNothrowMoveConstructible;
//template <typename T> struct TIsNothrowCopyAssignable;
//template <typename T> struct TIsNothrowMoveAssignable;
//template <typename T> struct TIsNothrowSwappable;
//template <typename T> struct TIsNothrowDestructible;
template <typename T, typename U> struct TIsAssignable : TBoolConstant<NAMESPACE_STD::is_assignable_v<T, U>> { };
template <typename T, typename U> struct TIsTriviallyAssignable : TBoolConstant<NAMESPACE_STD::is_trivially_assignable_v<T, U>> { };
//template <typename T, typename U> struct TIsSwappableWith;
//template <typename T, typename U> struct TIsNothrowAssignable;
//template <typename T, typename U> struct TIsNothrowSwappableWith;
template <typename T, typename... Args> struct TIsConstructible : TBoolConstant<NAMESPACE_STD::is_constructible_v<T, Args...>> { };
template <typename T, typename... Args> struct TIsTriviallyConstructible : TBoolConstant<NAMESPACE_STD::is_trivially_constructible_v<T, Args...>> { };

View File

@ -0,0 +1,35 @@
#pragma once
#include "CoreTypes.h"
#include "Templates/Utility.h"
#include "TypeTraits/HelperClasses.h"
NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
NAMESPACE_PRIVATE_BEGIN
// This is a copy of a declaration to prevent circular references, originally defined in Templates/Utility.h
template <typename T> requires TIsMoveConstructible<T>::Value&& TIsMoveAssignable<T>::Value
constexpr void Swap(T& A, T& B);
template <typename T, typename U>
concept CSwappableWith =
requires(T&& A, U&& B)
{
Swap(A, B);
Swap(B, A);
};
NAMESPACE_PRIVATE_END
template <typename T> struct TIsSwappable : TBoolConstant<NAMESPACE_PRIVATE::CSwappableWith<T&, T&>> { };
template <typename T, typename U> struct TIsSwappableWith : TBoolConstant<NAMESPACE_PRIVATE::CSwappableWith<T , U >> { };
//template <typename T> struct TIsNothrowSwappable;
//template <typename T, typename U> struct TIsNothrowSwappableWith;
NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft)
NAMESPACE_REDCRAFT_END

View File

@ -7,4 +7,5 @@
#include "TypeTraits/TypeProperties.h"
#include "TypeTraits/SupportedOperations.h"
#include "TypeTraits/Miscellaneous.h"
#include "TypeTraits/Swappable.h"
#include "TypeTraits/Common.h"