From 0b4cb1a9e0eb5ab65822596ed4b2eb2a82222613 Mon Sep 17 00:00:00 2001 From: _Redstone_c_ Date: Mon, 14 Mar 2022 18:03:57 +0800 Subject: [PATCH] style(templates): redefine placeholders such as ForceInit, InPlace, etc. --- .../Source/Public/Miscellaneous/CoreDefines.h | 5 ---- .../Source/Public/Templates/Optional.h | 13 +++++++-- .../Source/Public/Templates/Placeholders.h | 29 +++++++++++++++++++ .../Source/Public/Templates/Templates.h | 1 + 4 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 Redcraft.Utility/Source/Public/Templates/Placeholders.h diff --git a/Redcraft.Utility/Source/Public/Miscellaneous/CoreDefines.h b/Redcraft.Utility/Source/Public/Miscellaneous/CoreDefines.h index fdd1f40..67858b8 100644 --- a/Redcraft.Utility/Source/Public/Miscellaneous/CoreDefines.h +++ b/Redcraft.Utility/Source/Public/Miscellaneous/CoreDefines.h @@ -41,11 +41,6 @@ NAMESPACE_MODULE_BEGIN(Utility) enum { INDEX_NONE = -1 }; enum { UNICODE_BOM = 0xfeff }; -enum EForceInit { ForceInit }; -enum ENoInit { NoInit }; -enum EInvalid { Invalid }; -enum EInPlace { InPlace }; - 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 ee33843..bb94f3a 100644 --- a/Redcraft.Utility/Source/Public/Templates/Optional.h +++ b/Redcraft.Utility/Source/Public/Templates/Optional.h @@ -2,6 +2,7 @@ #include "CoreTypes.h" #include "Templates/Compare.h" +#include "Templates/Placeholders.h" #include "Concepts/Comparable.h" #include "TypeTraits/TypeTraits.h" #include "Miscellaneous/AssertionMacros.h" @@ -19,17 +20,17 @@ public: constexpr TOptional() : bIsValid(false) { } - constexpr TOptional(EInvalid) : TOptional() { } + constexpr TOptional(FInvalid) : TOptional() { } template requires TIsConstructible::Value - constexpr explicit TOptional(EInPlace, Types&&... Args) + constexpr explicit TOptional(FInPlace, Types&&... Args) : bIsValid(true) { new(&Value) OptionalType(Forward(Args)...); } template requires TIsConstructible::Value - && (!TIsSame::Type, EInPlace>::Value) && (!TIsSame::Type, TOptional>::Value) + && (!TIsSame::Type, FInPlace>::Value) && (!TIsSame::Type, TOptional>::Value) constexpr explicit(!TIsConvertible::Value) TOptional(T&& InValue) : TOptional(InPlace, Forward(InValue)) { } @@ -250,6 +251,12 @@ constexpr bool operator!=(const T& LHS, const TOptional& RHS) return RHS.IsValid() ? LHS != *RHS : true; } +template +constexpr TOptional::Type> MakeOptional(FInvalid) +{ + return TOptional::Type>(Invalid); +} + template constexpr TOptional::Type> MakeOptional(T&& InValue) { diff --git a/Redcraft.Utility/Source/Public/Templates/Placeholders.h b/Redcraft.Utility/Source/Public/Templates/Placeholders.h new file mode 100644 index 0000000..d1449af --- /dev/null +++ b/Redcraft.Utility/Source/Public/Templates/Placeholders.h @@ -0,0 +1,29 @@ +#pragma once + +#include "CoreTypes.h" + +NAMESPACE_REDCRAFT_BEGIN +NAMESPACE_MODULE_BEGIN(Redcraft) +NAMESPACE_MODULE_BEGIN(Utility) + +struct FForceInit { explicit FForceInit() = default; }; +inline constexpr FForceInit ForceInit{}; + +struct FNoInit { explicit FNoInit() = default; }; +inline constexpr FNoInit NoInit{}; + +struct FInvalid { explicit FInvalid() = default; }; +inline constexpr FInvalid Invalid{}; + +struct FInPlace { explicit FInPlace() = default; }; +inline constexpr FInPlace InPlace{}; + +template struct TInPlaceType { explicit TInPlaceType() = default; }; +template inline constexpr TInPlaceType InPlaceType{}; + +template struct TInPlaceIndex { explicit TInPlaceIndex() = default; }; +template inline constexpr TInPlaceIndex InPlaceIndex{}; + +NAMESPACE_MODULE_END(Utility) +NAMESPACE_MODULE_END(Redcraft) +NAMESPACE_REDCRAFT_END diff --git a/Redcraft.Utility/Source/Public/Templates/Templates.h b/Redcraft.Utility/Source/Public/Templates/Templates.h index 7624233..6b1b495 100644 --- a/Redcraft.Utility/Source/Public/Templates/Templates.h +++ b/Redcraft.Utility/Source/Public/Templates/Templates.h @@ -2,6 +2,7 @@ #include "CoreTypes.h" #include "Templates/Utility.h" +#include "Templates/Placeholders.h" #include "Templates/Container.h" #include "Templates/Noncopyable.h" #include "Templates/Invoke.h"