feat(templates): add TOptional and the corresponding testing

This commit is contained in:
_Redstone_c_ 2022-03-13 23:18:07 +08:00
parent c2aecef3dd
commit 8a3b089648
7 changed files with 240 additions and 102 deletions

View File

@ -11,6 +11,7 @@ void TestTemplates()
TestInvoke(); TestInvoke();
TestReferenceWrapper(); TestReferenceWrapper();
TestCompare(); TestCompare();
TestOptional();
TestMiscellaneous(); TestMiscellaneous();
} }
@ -195,6 +196,79 @@ void TestCompare()
} }
void TestOptional()
{
TOptional<int32> TempA;
TOptional<int32> TempB(Invalid);
TOptional<int32> TempC(InPlace, 0);
TOptional<int32> TempD(0);
TOptional<int32> TempE(0l);
TOptional<int32> TempF(0.0);
TOptional<int32> TempG(TempA);
TOptional<int32> TempH(TempD);
TOptional<int32> TempI(MakeOptional<int32>(0));
TOptional<int32> TempJ(MakeOptional<int32>(Invalid));
TOptional<int32> TempK, TempL, TempM, TempN;
TempK = TempA;
TempL = TempD;
TempM = MakeOptional<int32>(0);
TempN = MakeOptional<int32>(Invalid);
*TempL = 303;
*TempM = 404;
TOptional<int32> TempO;
TempO.Emplace(404);
always_check(TempO);
always_check(TempO.IsValid());
always_check(*TempO == 404);
always_check(TempO.GetValue() == 404);
always_check(TempO.Get(500) == 404);
TempO.Reset();
always_check(TempO == TempO);
always_check(TempO.Get(500) == 500);
int32 TempP = 200;
TempO = TempP;
TempO = 300;
always_check(TempO != TempA);
always_check(TempO != TempD);
always_check(TempO == TempO);
always_check(TempO == 300);
always_check(300 == TempO);
int16 TempQ = 1024;
TOptional<int16> TempR = TempQ;
TOptional<int32> TempS(InPlace, TempQ);
TOptional<int32> TempT(TempQ);
TOptional<int32> TempU(TempR);
TOptional<int32> TempV(MakeOptional<int16>(2048));
TOptional<int32> TempW, TempX, TempY;
TempW = TempQ;
TempX = TempR;
TempY = MakeOptional<int16>(2048);
struct FTracker
{
FTracker() { }
FTracker(const FTracker& InValue) { always_check_no_entry(); }
FTracker(FTracker&& InValue) { }
FTracker& operator=(const FTracker& InValue) { always_check_no_entry(); return *this; }
FTracker& operator=(FTracker&& InValue) { return *this; }
};
TOptional<FTracker> TempZ(MakeOptional<FTracker>());
TempZ = MakeOptional<FTracker>();
TempZ = FTracker();
}
NAMESPACE_UNNAMED_BEGIN NAMESPACE_UNNAMED_BEGIN
template <typename T> template <typename T>

View File

@ -41,13 +41,9 @@ NAMESPACE_MODULE_BEGIN(Utility)
enum { INDEX_NONE = -1 }; enum { INDEX_NONE = -1 };
enum { UNICODE_BOM = 0xfeff }; enum { UNICODE_BOM = 0xfeff };
enum EForceInit enum EForceInit { ForceInit };
{
ForceInit,
ForceInitToZero
};
enum ENoInit { NoInit }; enum ENoInit { NoInit };
enum EInvalid { Invalid };
enum EInPlace { InPlace }; enum EInPlace { InPlace };
NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Utility)

View File

@ -1,6 +1,8 @@
#pragma once #pragma once
#include "CoreTypes.h" #include "CoreTypes.h"
#include "Templates/Compare.h"
#include "Concepts/Comparable.h"
#include "TypeTraits/TypeTraits.h" #include "TypeTraits/TypeTraits.h"
#include "Miscellaneous/AssertionMacros.h" #include "Miscellaneous/AssertionMacros.h"
@ -8,39 +10,54 @@ NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility) NAMESPACE_MODULE_BEGIN(Utility)
template <typename T> template <typename OptionalType>
struct TOptional struct TOptional
{ {
public: public:
using Type = T; using Type = OptionalType;
constexpr TOptional() : bIsValid(false) { } constexpr TOptional() : bIsValid(false) { }
template <typename... Types> requires TIsConstructible<T, Types...>::Value constexpr TOptional(EInvalid) : TOptional() { }
template <typename... Types> requires TIsConstructible<OptionalType, Types...>::Value
constexpr explicit TOptional(EInPlace, Types&&... Args) constexpr explicit TOptional(EInPlace, Types&&... Args)
: bIsValid(true) : bIsValid(true)
{ {
new(&Value) T(Forward<Types>(Args)...); new(&Value) OptionalType(Forward<Types>(Args)...);
} }
template <typename U = T> requires TIsConstructible<T, U&&>::Value && !TIsSame<typename TRemoveCVRef<U>::Type, EInPlace>::Value && !TIsSame<typename TRemoveCVRef<U>::Type, TOptional>::Value template <typename T = OptionalType> requires TIsConstructible<OptionalType, T&&>::Value
constexpr explicit(!TIsConvertible<U&&, T>::Value) TOptional(U&& InValue) && (!TIsSame<typename TRemoveCVRef<T>::Type, EInPlace>::Value) && (!TIsSame<typename TRemoveCVRef<T>::Type, TOptional>::Value)
: TOptional(InPlace, Forward<U>(InValue)) constexpr explicit(!TIsConvertible<T&&, OptionalType>::Value) TOptional(T&& InValue)
: TOptional(InPlace, Forward<T>(InValue))
{ } { }
template <typename U = T> requires TIsConstructible<T, const U&>::Value constexpr TOptional(const TOptional& InValue)
constexpr explicit(!TIsConvertible<const U&, T>::Value) TOptional(const TOptional<U>& InValue) : bIsValid(InValue.IsValid())
: bIsValid(InValue.bIsValid)
{ {
if (InValue.bIsValid) new(&Value) T(InValue.GetValue()); if (InValue.IsValid()) new(&Value) OptionalType(InValue.GetValue());
} }
template <typename U = T> requires TIsConstructible<T, U&&>::Value constexpr TOptional(TOptional&& InValue)
constexpr explicit(!TIsConvertible<U&&, T>::Value) TOptional(TOptional<U>&& InValue) : bIsValid(InValue.IsValid())
: bIsValid(InValue.bIsValid)
{ {
if (InValue.bIsValid) new(&Value) T(MoveTempIfPossible(InValue).GetValue()); if (InValue.IsValid()) new(&Value) OptionalType(MoveTemp(InValue.GetValue()));
}
template <typename T = OptionalType> requires TIsConstructible<OptionalType, const T&>::Value
constexpr explicit(!TIsConvertible<const T&, OptionalType>::Value) TOptional(const TOptional<T>& InValue)
: bIsValid(InValue.IsValid())
{
if (InValue.IsValid()) new(&Value) OptionalType(InValue.GetValue());
}
template <typename T = OptionalType> requires TIsConstructible<OptionalType, T&&>::Value
constexpr explicit(!TIsConvertible<T&&, OptionalType>::Value) TOptional(TOptional<T>&& InValue)
: bIsValid(InValue.IsValid())
{
if (InValue.IsValid()) new(&Value) OptionalType(MoveTemp(InValue.GetValue()));
} }
constexpr ~TOptional() constexpr ~TOptional()
@ -48,56 +65,103 @@ public:
Reset(); Reset();
} }
template <typename U = T> requires TIsConstructible<T, const U&>::Value constexpr TOptional& operator=(const TOptional& InValue)
constexpr TOptional& operator=(const TOptional<U>& InValue)
{ {
if (InValue == this) return *this; if (&InValue == this) return *this;
if (!InValue.IsValid())
{
Reset(); Reset();
return *this;
}
if (InValue.bIsValid) if (IsValid()) GetValue() = InValue.GetValue();
else
{ {
new(&Value) T(InValue.GetValue()); new(&Value) OptionalType(InValue.GetValue());
bIsValid = true; bIsValid = true;
} }
return *this; return *this;
} }
template <typename U = T> requires TIsConstructible<T, U&&>::Value constexpr TOptional& operator=(TOptional&& InValue)
constexpr TOptional& operator=(TOptional<U>&& InValue)
{ {
if (InValue == this) return *this; if (&InValue == this) return *this;
if (!InValue.IsValid())
{
Reset(); Reset();
return *this;
}
if (InValue.bIsValid) if (IsValid()) GetValue() = MoveTemp(InValue.GetValue());
else
{ {
new(&Value) T(MoveTempIfPossible(InValue).GetValue()); new(&Value) OptionalType(MoveTemp(InValue.GetValue()));
bIsValid = true; bIsValid = true;
} }
return *this; return *this;
} }
template <typename U = T> requires TIsConstructible<T, U&&>::Value template <typename T = OptionalType> requires TIsConstructible<OptionalType, const T&>::Value
constexpr TOptional& operator=(U&& InValue) constexpr TOptional& operator=(const TOptional<T>& InValue)
{
if (!InValue.IsValid())
{ {
Reset(); Reset();
return *this;
}
new(&Value) T(MoveTempIfPossible(InValue)); if (IsValid()) GetValue() = InValue.GetValue();
else
{
new(&Value) OptionalType(InValue.GetValue());
bIsValid = true; bIsValid = true;
}
return *this; return *this;
} }
template <typename T = OptionalType> requires TIsConstructible<OptionalType, T&&>::Value
constexpr TOptional& operator=(TOptional<T>&& InValue)
{
if (!InValue.IsValid())
{
Reset();
return *this;
}
if (IsValid()) GetValue() = MoveTemp(InValue.GetValue());
else
{
new(&Value) OptionalType(MoveTemp(InValue.GetValue()));
bIsValid = true;
}
return *this;
}
template <typename T = OptionalType> requires TIsConstructible<OptionalType, T&&>::Value
constexpr TOptional& operator=(T&& InValue)
{
if (IsValid()) GetValue() = Forward<T>(InValue);
else
{
new(&Value) OptionalType(Forward<T>(InValue));
bIsValid = true;
}
return *this;
}
template <typename... ArgsType> template <typename... ArgsType>
constexpr T& Emplace(ArgsType&&... Args) constexpr OptionalType& Emplace(ArgsType&&... Args)
{ {
Reset(); Reset();
T* Result = new(&Value) T(Forward<ArgsType>(Args)...); OptionalType* Result = new(&Value) OptionalType(Forward<ArgsType>(Args)...);
bIsValid = true; bIsValid = true;
return *Result; return *Result;
@ -106,24 +170,24 @@ public:
constexpr bool IsValid() const { return bIsValid; } constexpr bool IsValid() const { return bIsValid; }
constexpr explicit operator bool() const { return bIsValid; } constexpr explicit operator bool() const { return bIsValid; }
constexpr T& GetValue() & { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsSet() or use Get(DefaultValue) instead.")); return *(T*)&Value; } constexpr OptionalType& GetValue() & { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsValid() or use Get(DefaultValue) instead.")); return *(OptionalType*)&Value; }
constexpr T&& GetValue() && { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsSet() or use Get(DefaultValue) instead.")); return *(T*)&Value; } constexpr OptionalType&& GetValue() && { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsValid() or use Get(DefaultValue) instead.")); return MoveTemp(*(OptionalType*)&Value); }
constexpr const T& GetValue() const& { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsSet() or use Get(DefaultValue) instead.")); return *(T*)&Value; } constexpr const OptionalType& GetValue() const& { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsValid() or use Get(DefaultValue) instead.")); return *(OptionalType*)&Value; }
constexpr const T&& GetValue() const&& { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsSet() or use Get(DefaultValue) instead.")); return *(T*)&Value; } constexpr const OptionalType&& GetValue() const&& { checkf(IsValid(), TEXT("It is an error to call GetValue() on an unset TOptional. Please either check IsValid() or use Get(DefaultValue) instead.")); return MoveTemp(*(OptionalType*)&Value); }
constexpr const T* operator->() const { return &GetValue(); } constexpr const OptionalType* operator->() const { return &GetValue(); }
constexpr T* operator->() { return &GetValue(); } constexpr OptionalType* operator->() { return &GetValue(); }
constexpr T& operator*() & { return GetValue(); } constexpr OptionalType& operator*() & { return GetValue(); }
constexpr T&& operator*() && { return GetValue(); } constexpr OptionalType&& operator*() && { return GetValue(); }
constexpr const T& operator*() const& { return GetValue(); } constexpr const OptionalType& operator*() const& { return GetValue(); }
constexpr const T&& operator*() const&& { return GetValue(); } constexpr const OptionalType&& operator*() const&& { return GetValue(); }
template <typename U = T> template <typename T = OptionalType>
constexpr T Get(U&& DefaultValue) && { return IsValid() ? GetValue() : DefaultValue; } constexpr OptionalType Get(T&& DefaultValue) && { return IsValid() ? GetValue() : DefaultValue; }
template <typename U = T> template <typename T = OptionalType>
constexpr T Get(U&& DefaultValue) const& { return IsValid() ? GetValue() : DefaultValue; } constexpr OptionalType Get(T&& DefaultValue) const& { return IsValid() ? GetValue() : DefaultValue; }
constexpr void Reset() constexpr void Reset()
{ {
@ -131,14 +195,14 @@ public:
{ {
bIsValid = false; bIsValid = false;
typedef T DestructOptionalType; typedef OptionalType DestructOptionalType;
((T*)&Value)->DestructOptionalType::~DestructOptionalType(); ((OptionalType*)&Value)->DestructOptionalType::~DestructOptionalType();
} }
} }
private: private:
TAlignedStorage<sizeof(T), alignof(T)>::Type Value; TAlignedStorage<sizeof(OptionalType), alignof(OptionalType)>::Type Value;
bool bIsValid; bool bIsValid;
}; };
@ -149,7 +213,7 @@ TOptional(T) ->TOptional<T>;
template <typename T, typename U> template <typename T, typename U>
constexpr bool operator==(const TOptional<T>& LHS, const TOptional<U>& RHS) constexpr bool operator==(const TOptional<T>& LHS, const TOptional<U>& RHS)
{ {
if (LHS.IsValid() != LHS.IsValid()) return false; if (LHS.IsValid() != RHS.IsValid()) return false;
if (LHS.IsValid() == false) return true; if (LHS.IsValid() == false) return true;
return *LHS == *RHS; return *LHS == *RHS;
} }
@ -157,32 +221,34 @@ constexpr bool operator==(const TOptional<T>& LHS, const TOptional<U>& RHS)
template <typename T, typename U> template <typename T, typename U>
constexpr bool operator!=(const TOptional<T>& LHS, const TOptional<U>& RHS) constexpr bool operator!=(const TOptional<T>& LHS, const TOptional<U>& RHS)
{ {
if (LHS.IsValid() != LHS.IsValid()) return true; if (LHS.IsValid() != RHS.IsValid()) return true;
if (LHS.IsValid() == false) return false; if (LHS.IsValid() == false) return true;
return *LHS != *RHS; return *LHS != *RHS;
} }
//template <typename T, typename U> template <typename T, typename U>
//constexpr bool operator<(const TOptional<T>&, const TOptional<U>&); constexpr bool operator==(const TOptional<T>& LHS, const U& RHS)
//template <typename T, typename U> {
//constexpr bool operator>(const TOptional<T>&, const TOptional<U>&); return LHS.IsValid() ? *LHS == RHS : false;
//template <typename T, typename U> }
//constexpr bool operator<=(const TOptional<T>&, const TOptional<U>&);
//template <typename T, typename U>
//constexpr bool operator>=(const TOptional<T>&, const TOptional<U>&);
//template <typename T, typename U> constexpr bool operator==(const TOptional<T>&, const U&); template <typename T, typename U>
//template <typename T, typename U> constexpr bool operator==(const T&, const TOptional<U>&); constexpr bool operator!=(const TOptional<T>& LHS, const U& RHS)
//template <typename T, typename U> constexpr bool operator!=(const TOptional<T>&, const U&); {
//template <typename T, typename U> constexpr bool operator!=(const T&, const TOptional<U>&); return LHS.IsValid() ? *LHS != RHS : true;
//template <typename T, typename U> constexpr bool operator<(const TOptional<T>&, const U&); }
//template <typename T, typename U> constexpr bool operator<(const T&, const TOptional<U>&);
//template <typename T, typename U> constexpr bool operator>(const TOptional<T>&, const U&); template <typename T, typename U>
//template <typename T, typename U> constexpr bool operator>(const T&, const TOptional<U>&); constexpr bool operator==(const T& LHS, const TOptional<U>& RHS)
//template <typename T, typename U> constexpr bool operator<=(const TOptional<T>&, const U&); {
//template <typename T, typename U> constexpr bool operator<=(const T&, const TOptional<U>&); return RHS.IsValid() ? LHS == *RHS : false;
//template <typename T, typename U> constexpr bool operator>=(const TOptional<T>&, const U&); }
//template <typename T, typename U> constexpr bool operator>=(const T&, const TOptional<U>&);
template <typename T, typename U>
constexpr bool operator!=(const T& LHS, const TOptional<U>& RHS)
{
return RHS.IsValid() ? LHS != *RHS : true;
}
template <typename T> template <typename T>
constexpr TOptional<typename TDecay<T>::Type> MakeOptional(T&& InValue) constexpr TOptional<typename TDecay<T>::Type> MakeOptional(T&& InValue)
@ -196,21 +262,21 @@ constexpr TOptional<T> MakeOptional(Types&&... Args)
return TOptional<T>(InPlace, Forward<T>(Args)...); return TOptional<T>(InPlace, Forward<T>(Args)...);
} }
template <typename T> template <typename T> requires TIsMoveConstructible<T>::Value&& TIsSwappable<T>::Value
constexpr void Swap(TOptional<T>& A, TOptional<T>& B) constexpr void Swap(TOptional<T>& A, TOptional<T>& B)
{ {
if (!A && !B) return; if (!A && !B) return;
if (A && !B) if (A && !B)
{ {
B = A; B = MoveTemp(A);
A.Reset(); A.Reset();
return; return;
} }
if (B && !A) if (B && !A)
{ {
A = B; A = MoveTemp(B);
B.Reset(); B.Reset();
return; return;
} }

View File

@ -7,3 +7,4 @@
#include "Templates/Invoke.h" #include "Templates/Invoke.h"
#include "Templates/ReferenceWrapper.h" #include "Templates/ReferenceWrapper.h"
#include "Templates/Compare.h" #include "Templates/Compare.h"
#include "Templates/Optional.h"

View File

@ -23,7 +23,7 @@ constexpr const T(&AsConst(T(&Array)[N]))[N]
} }
template <typename T> template <typename T>
FORCEINLINE typename TRemoveReference<T>::Type&& MoveTemp(T&& Obj) constexpr typename TRemoveReference<T>::Type&& MoveTemp(T&& Obj)
{ {
typedef typename TRemoveReference<T>::Type CastType; typedef typename TRemoveReference<T>::Type CastType;
@ -34,31 +34,31 @@ FORCEINLINE typename TRemoveReference<T>::Type&& MoveTemp(T&& Obj)
} }
template <typename T> template <typename T>
FORCEINLINE T CopyTemp(T& Val) constexpr T CopyTemp(T& Val)
{ {
return const_cast<const T&>(Val); return const_cast<const T&>(Val);
} }
template <typename T> template <typename T>
FORCEINLINE T CopyTemp(const T& Val) constexpr T CopyTemp(const T& Val)
{ {
return Val; return Val;
} }
template <typename T> template <typename T>
FORCEINLINE T&& CopyTemp(T&& Val) constexpr T&& CopyTemp(T&& Val)
{ {
return MoveTemp(Val); return MoveTemp(Val);
} }
template <typename T> template <typename T>
FORCEINLINE T&& Forward(typename TRemoveReference<T>::Type& Obj) constexpr T&& Forward(typename TRemoveReference<T>::Type& Obj)
{ {
return (T&&)Obj; return (T&&)Obj;
} }
template <typename T> template <typename T>
FORCEINLINE T&& Forward(typename TRemoveReference<T>::Type&& Obj) constexpr T&& Forward(typename TRemoveReference<T>::Type&& Obj)
{ {
return (T&&)Obj; return (T&&)Obj;
} }
@ -80,7 +80,7 @@ constexpr T Exchange(T& A, U&& B)
} }
template <typename T> template <typename T>
T&& DeclVal(); constexpr T&& DeclVal();
template <typename T> requires TIsObject<T>::Value template <typename T> requires TIsObject<T>::Value
constexpr T* AddressOf(T& Object) constexpr T* AddressOf(T& Object)
@ -88,7 +88,7 @@ constexpr T* AddressOf(T& Object)
return reinterpret_cast<T*>(&const_cast<char&>(reinterpret_cast<const volatile char&>(Object))); return reinterpret_cast<T*>(&const_cast<char&>(reinterpret_cast<const volatile char&>(Object)));
} }
template <typename T> requires !TIsObject<T>::Value template <typename T> requires (!TIsObject<T>::Value)
constexpr T* AddressOf(T& Object) constexpr T* AddressOf(T& Object)
{ {
return &Object; return &Object;

View File

@ -10,6 +10,7 @@ void REDCRAFTUTILITY_API TestTemplates();
void REDCRAFTUTILITY_API TestInvoke(); void REDCRAFTUTILITY_API TestInvoke();
void REDCRAFTUTILITY_API TestReferenceWrapper(); void REDCRAFTUTILITY_API TestReferenceWrapper();
void REDCRAFTUTILITY_API TestCompare(); void REDCRAFTUTILITY_API TestCompare();
void REDCRAFTUTILITY_API TestOptional();
void REDCRAFTUTILITY_API TestMiscellaneous(); void REDCRAFTUTILITY_API TestMiscellaneous();
NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Utility)