From 02cdce39e2ca9f8ea34a8960f765d823b88fdd85 Mon Sep 17 00:00:00 2001 From: _Redstone_c_ Date: Mon, 20 Feb 2023 18:53:59 +0800 Subject: [PATCH] feat(templates): add MakeAny() and fix MakeOptional() --- .../Source/Public/Templates/Any.h | 28 +++++++++++++++++++ .../Source/Public/Templates/Optional.h | 19 +++++++++---- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/Redcraft.Utility/Source/Public/Templates/Any.h b/Redcraft.Utility/Source/Public/Templates/Any.h index 93abb01..25929f3 100644 --- a/Redcraft.Utility/Source/Public/Templates/Any.h +++ b/Redcraft.Utility/Source/Public/Templates/Any.h @@ -599,6 +599,34 @@ static_assert(sizeof(FAny) == 64, "The byte size of FAny is unexpected"); static_assert(alignof(FAny) == 16, "The byte alignment of FAny is unexpected"); +/** Creates an any object that does not contain a value. */ +template requires (NAMESPACE_PRIVATE::CFAnyPlaceable) +NODISCARD FORCEINLINE constexpr FAny MakeAny(FInvalid) +{ + return FAny(Invalid); +} + +/** Creates an any object from value. */ +template requires (NAMESPACE_PRIVATE::CFAnyPlaceable && CConstructibleFrom, T>) +NODISCARD FORCEINLINE constexpr FAny MakeAny(T&& InValue) +{ + return FAny(Forward(InValue)); +} + +/** Creates an any object constructed in-place from Args.... */ +template requires (NAMESPACE_PRIVATE::CFAnyPlaceable && CConstructibleFrom) +NODISCARD FORCEINLINE constexpr FAny MakeAny(Ts&&... Args) +{ + return FAny(InPlaceType>, Forward(Args)...); +} + +/** Creates an any object constructed in-place from IL, Args.... */ +template requires (NAMESPACE_PRIVATE::CFAnyPlaceable && CConstructibleFrom, Ts...>) +NODISCARD FORCEINLINE constexpr FAny MakeAny(initializer_list IL, Ts&&... Args) +{ + return FAny(InPlaceType>, IL, Forward(Args)...); +} + NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END diff --git a/Redcraft.Utility/Source/Public/Templates/Optional.h b/Redcraft.Utility/Source/Public/Templates/Optional.h index 87b472d..0f5b38f 100644 --- a/Redcraft.Utility/Source/Public/Templates/Optional.h +++ b/Redcraft.Utility/Source/Public/Templates/Optional.h @@ -377,25 +377,32 @@ TOptional(T) -> TOptional; /** Creates an optional object that does not contain a value. */ template requires (CDestructible) -NODISCARD FORCEINLINE constexpr TOptional> MakeOptional(FInvalid) +NODISCARD FORCEINLINE constexpr TOptional MakeOptional(FInvalid) { - return TOptional>(Invalid); + return TOptional(Invalid); } /** Creates an optional object from value. */ -template requires (CDestructible && CConstructibleFrom) -NODISCARD FORCEINLINE constexpr TOptional MakeOptional(T&& InValue) +template requires (CDestructible> && CConstructibleFrom, T>) +NODISCARD FORCEINLINE constexpr TOptional> MakeOptional(T&& InValue) { - return TOptional(Forward(InValue)); + return TOptional>(Forward(InValue)); } -/** Creates an optional object constructed in-place from args.... */ +/** Creates an optional object constructed in-place from Args.... */ template requires (CDestructible && CConstructibleFrom) NODISCARD FORCEINLINE constexpr TOptional MakeOptional(Ts&&... Args) { return TOptional(InPlace, Forward(Args)...); } +/** Creates an optional object constructed in-place from IL, Args.... */ +template requires (CDestructible && CConstructibleFrom, Ts...>) +NODISCARD FORCEINLINE constexpr TOptional MakeOptional(initializer_list IL, Ts&&... Args) +{ + return TOptional(InPlace, IL, Forward(Args)...); +} + NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END