#pragma once #include "CoreTypes.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 struct TOptional { public: using Type = OptionalType; constexpr TOptional() : bIsValid(false) { } constexpr TOptional(FInvalid) : TOptional() { } template requires TIsConstructible::Value constexpr explicit TOptional(FInPlace, Types&&... Args) : bIsValid(true) { new(&Value) OptionalType(Forward(Args)...); } template requires TIsConstructible::Value && (!TIsSame::Type, FInPlace>::Value) && (!TIsSame::Type, TOptional>::Value) constexpr explicit(!TIsConvertible::Value) TOptional(T&& InValue) : TOptional(InPlace, Forward(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 requires TIsConstructible::Value constexpr explicit(!TIsConvertible::Value) TOptional(const TOptional& InValue) : bIsValid(InValue.IsValid()) { if (InValue.IsValid()) new(&Value) OptionalType(InValue.GetValue()); } template requires TIsConstructible::Value constexpr explicit(!TIsConvertible::Value) TOptional(TOptional&& 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 requires TIsConstructible::Value constexpr TOptional& operator=(const TOptional& InValue) { if (!InValue.IsValid()) { Reset(); return *this; } if (IsValid()) GetValue() = InValue.GetValue(); else { new(&Value) OptionalType(InValue.GetValue()); bIsValid = true; } return *this; } template requires TIsConstructible::Value constexpr TOptional& operator=(TOptional&& 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 requires TIsConstructible::Value constexpr TOptional& operator=(T&& InValue) { if (IsValid()) GetValue() = Forward(InValue); else { new(&Value) OptionalType(Forward(InValue)); bIsValid = true; } return *this; } template constexpr OptionalType& Emplace(ArgsType&&... Args) { Reset(); OptionalType* Result = new(&Value) OptionalType(Forward(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 constexpr OptionalType Get(T&& DefaultValue) && { return IsValid() ? GetValue() : DefaultValue; } template 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::Type Value; bool bIsValid; }; template TOptional(T) ->TOptional; template constexpr bool operator==(const TOptional& LHS, const TOptional& RHS) { if (LHS.IsValid() != RHS.IsValid()) return false; if (LHS.IsValid() == false) return true; return *LHS == *RHS; } template constexpr bool operator!=(const TOptional& LHS, const TOptional& RHS) { if (LHS.IsValid() != RHS.IsValid()) return true; if (LHS.IsValid() == false) return false; return *LHS != *RHS; } template constexpr bool operator==(const TOptional& LHS, const U& RHS) { return LHS.IsValid() ? *LHS == RHS : false; } template constexpr bool operator!=(const TOptional& LHS, const U& RHS) { return LHS.IsValid() ? *LHS != RHS : true; } template constexpr bool operator==(const T& LHS, const TOptional& RHS) { return RHS.IsValid() ? LHS == *RHS : false; } template 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) { return TOptional::Type>(Forward(InValue)); } template constexpr TOptional MakeOptional(Types&&... Args) { return TOptional(InPlace, Forward(Args)...); } template requires TIsMoveConstructible::Value&& TIsSwappable::Value constexpr void Swap(TOptional& A, TOptional& 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