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();
TestReferenceWrapper();
TestCompare();
TestOptional();
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
template <typename T>

View File

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

View File

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

View File

@ -7,3 +7,4 @@
#include "Templates/Invoke.h"
#include "Templates/ReferenceWrapper.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>
FORCEINLINE typename TRemoveReference<T>::Type&& MoveTemp(T&& Obj)
constexpr typename TRemoveReference<T>::Type&& MoveTemp(T&& Obj)
{
typedef typename TRemoveReference<T>::Type CastType;
@ -34,31 +34,31 @@ FORCEINLINE typename TRemoveReference<T>::Type&& MoveTemp(T&& Obj)
}
template <typename T>
FORCEINLINE T CopyTemp(T& Val)
constexpr T CopyTemp(T& Val)
{
return const_cast<const T&>(Val);
}
template <typename T>
FORCEINLINE T CopyTemp(const T& Val)
constexpr T CopyTemp(const T& Val)
{
return Val;
}
template <typename T>
FORCEINLINE T&& CopyTemp(T&& Val)
constexpr T&& CopyTemp(T&& Val)
{
return MoveTemp(Val);
}
template <typename T>
FORCEINLINE T&& Forward(typename TRemoveReference<T>::Type& Obj)
constexpr T&& Forward(typename TRemoveReference<T>::Type& Obj)
{
return (T&&)Obj;
}
template <typename T>
FORCEINLINE T&& Forward(typename TRemoveReference<T>::Type&& Obj)
constexpr T&& Forward(typename TRemoveReference<T>::Type&& Obj)
{
return (T&&)Obj;
}
@ -80,7 +80,7 @@ constexpr T Exchange(T& A, U&& B)
}
template <typename T>
T&& DeclVal();
constexpr T&& DeclVal();
template <typename T> requires TIsObject<T>::Value
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)));
}
template <typename T> requires !TIsObject<T>::Value
template <typename T> requires (!TIsObject<T>::Value)
constexpr T* AddressOf(T& Object)
{
return &Object;

View File

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