feat(templates): add TOptional and the corresponding testing

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

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;
Reset();
if (InValue.bIsValid)
if (!InValue.IsValid())
{
new(&Value) T(InValue.GetValue());
Reset();
return *this;
}
if (IsValid()) GetValue() = InValue.GetValue();
else
{
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;
Reset();
if (InValue.bIsValid)
if (!InValue.IsValid())
{
new(&Value) T(MoveTempIfPossible(InValue).GetValue());
Reset();
return *this;
}
if (IsValid()) GetValue() = MoveTemp(InValue.GetValue());
else
{
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)
{
Reset();
if (!InValue.IsValid())
{
Reset();
return *this;
}
new(&Value) T(MoveTempIfPossible(InValue));
bIsValid = true;
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;
}