From 95b492851aa49b172649cf1cc69c7c7cf88ed59e Mon Sep 17 00:00:00 2001 From: _Redstone_c_ Date: Mon, 2 Jan 2023 22:03:40 +0800 Subject: [PATCH] fix(templates): fix the implementation of the Swap algorithm for arrays --- .../Source/Public/Templates/Container.h | 19 +++++++++++++++++++ .../Source/Public/Templates/Utility.h | 12 ------------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Redcraft.Utility/Source/Public/Templates/Container.h b/Redcraft.Utility/Source/Public/Templates/Container.h index 083e4e2..976b4d1 100644 --- a/Redcraft.Utility/Source/Public/Templates/Container.h +++ b/Redcraft.Utility/Source/Public/Templates/Container.h @@ -1,57 +1,76 @@ #pragma once #include "CoreTypes.h" +#include "TypeTraits/Swappable.h" NAMESPACE_REDCRAFT_BEGIN NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Utility) +/** @return The pointer or iterator to the container element storage. */ template requires (requires(T&& Container) { Container.GetData(); }) FORCEINLINE constexpr decltype(auto) GetData(T&& Container) { return Container.GetData(); } +/** Overloads the GetData algorithm for arrays. */ 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 T::data(). */ template requires (requires(T&& Container) { Container.data(); }) FORCEINLINE constexpr decltype(auto) GetData(T&& Container) { return Container.data(); } +/** Overloads the GetData algorithm for initializer_list. */ template FORCEINLINE constexpr decltype(auto) GetData(initializer_list Container) { return Container.begin(); } +/** @return The number of elements in the container. */ template requires (requires(T&& Container) { Container.Num(); }) FORCEINLINE constexpr decltype(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; } +/** Overloads the GetNum algorithm for T::size(). */ template requires (requires(T&& Container) { Container.size(); }) FORCEINLINE constexpr decltype(auto) GetNum(T&& Container) { return Container.size(); } +/** Overloads the GetNum algorithm for initializer_list. */ template FORCEINLINE constexpr decltype(auto) GetNum(initializer_list Container) { return Container.size(); } +/** Overloads the Swap algorithm for arrays. */ +template requires (CSwappable>) +FORCEINLINE constexpr void Swap(T(&A)[N], T(&B)[N]) +{ + for (size_t Index = 0; Index < N; ++Index) + { + Swap(A[Index], B[Index]); + } +} + NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END diff --git a/Redcraft.Utility/Source/Public/Templates/Utility.h b/Redcraft.Utility/Source/Public/Templates/Utility.h index b66dbc5..793bab5 100644 --- a/Redcraft.Utility/Source/Public/Templates/Utility.h +++ b/Redcraft.Utility/Source/Public/Templates/Utility.h @@ -73,18 +73,6 @@ FORCEINLINE constexpr void Swap(T& A, T& B) B = MoveTemp(Temp); } -/** Overloads the Swap algorithm for array. */ -template requires (CMoveConstructible && CMoveAssignable) -FORCEINLINE constexpr void Swap(T(&A)[N], T(&B)[N]) -{ - for (size_t Index = 0; Index < N; ++Index) - { - T Temp = MoveTemp(A[Index]); - A[Index] = MoveTemp(B[Index]); - B[Index] = MoveTemp(Temp); - } -} - /** Replaces the value of 'A' with 'B' and returns the old value of 'A'. */ template requires (CMoveConstructible && CAssignableFrom) FORCEINLINE constexpr T Exchange(T& A, U&& B)