Redcraft/Redcraft.Utility/Source/Public/Templates/Optional.h

290 lines
7.7 KiB
C
Raw Normal View History

#pragma once
#include "CoreTypes.h"
#include "Templates/Compare.h"
#include "Concepts/Comparable.h"
#include "TypeTraits/TypeTraits.h"
#include "Miscellaneous/AssertionMacros.h"
NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
template <typename OptionalType>
struct TOptional
{
public:
using Type = OptionalType;
constexpr TOptional() : bIsValid(false) { }
constexpr TOptional(EInvalid) : TOptional() { }
template <typename... Types> requires TIsConstructible<OptionalType, Types...>::Value
constexpr explicit TOptional(EInPlace, Types&&... Args)
: bIsValid(true)
{
new(&Value) OptionalType(Forward<Types>(Args)...);
}
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))
{ }
constexpr TOptional(const TOptional& InValue)
: bIsValid(InValue.IsValid())
{
if (InValue.IsValid()) new(&Value) OptionalType(InValue.GetValue());
}
constexpr TOptional(TOptional&& InValue)
: bIsValid(InValue.IsValid())
{
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()
{
Reset();
}
constexpr TOptional& operator=(const TOptional& InValue)
{
if (&InValue == this) return *this;
if (!InValue.IsValid())
{
Reset();
return *this;
}
if (IsValid()) GetValue() = InValue.GetValue();
else
{
new(&Value) OptionalType(InValue.GetValue());
bIsValid = true;
}
return *this;
}
constexpr TOptional& operator=(TOptional&& InValue)
{
if (&InValue == this) return *this;
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, const T&>::Value
constexpr TOptional& operator=(const TOptional<T>& InValue)
{
if (!InValue.IsValid())
{
Reset();
return *this;
}
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 OptionalType& Emplace(ArgsType&&... Args)
{
Reset();
OptionalType* Result = new(&Value) OptionalType(Forward<ArgsType>(Args)...);
bIsValid = true;
return *Result;
}
constexpr bool IsValid() const { return bIsValid; }
constexpr explicit operator bool() const { return bIsValid; }
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 OptionalType* operator->() const { return &GetValue(); }
constexpr OptionalType* operator->() { 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 T = OptionalType>
constexpr OptionalType Get(T&& DefaultValue) && { return IsValid() ? GetValue() : DefaultValue; }
template <typename T = OptionalType>
constexpr OptionalType Get(T&& DefaultValue) const& { return IsValid() ? GetValue() : DefaultValue; }
constexpr void Reset()
{
if (bIsValid)
{
bIsValid = false;
typedef OptionalType DestructOptionalType;
((OptionalType*)&Value)->DestructOptionalType::~DestructOptionalType();
}
}
private:
TAlignedStorage<sizeof(OptionalType), alignof(OptionalType)>::Type Value;
bool bIsValid;
};
template <typename T>
TOptional(T) ->TOptional<T>;
template <typename T, typename U>
constexpr bool operator==(const TOptional<T>& LHS, const TOptional<U>& RHS)
{
if (LHS.IsValid() != RHS.IsValid()) return false;
if (LHS.IsValid() == false) return true;
return *LHS == *RHS;
}
template <typename T, typename U>
constexpr bool operator!=(const TOptional<T>& LHS, const TOptional<U>& RHS)
{
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>& LHS, const U& RHS)
{
return LHS.IsValid() ? *LHS == RHS : false;
}
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)
{
return TOptional<typename TDecay<T>::Type>(Forward<T>(InValue));
}
template <typename T, typename... Types>
constexpr TOptional<T> MakeOptional(Types&&... Args)
{
return TOptional<T>(InPlace, Forward<T>(Args)...);
}
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 = MoveTemp(A);
A.Reset();
return;
}
if (B && !A)
{
A = MoveTemp(B);
B.Reset();
return;
}
Swap(*A, *B);
}
NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft)
NAMESPACE_REDCRAFT_END