#pragma once #include "CoreTypes.h" #include "Templates/Utility.h" #include "Templates/TypeHash.h" #include "TypeTraits/TypeTraits.h" #include "Miscellaneous/Compare.h" #include "Miscellaneous/AssertionMacros.h" NAMESPACE_REDCRAFT_BEGIN NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Utility) template requires (CDestructible) class TOptional; NAMESPACE_PRIVATE_BEGIN template concept CTOptionalAllowUnwrappable = !(CConstructibleFrom& > || CConstructibleFrom& > || CConstructibleFrom&&> || CConstructibleFrom&&> || CConvertibleTo< TOptional&, U> || CConvertibleTo&, U> || CConvertibleTo< TOptional&&, U> || CConvertibleTo&&, U> || CAssignableFrom& > || CAssignableFrom& > || CAssignableFrom&&> || CAssignableFrom&&>); NAMESPACE_PRIVATE_END template requires (CDestructible) class TOptional { public: using ValueType = OptionalType; FORCEINLINE constexpr TOptional() : bIsValid(false) { } FORCEINLINE constexpr TOptional(FInvalid) : TOptional() { } template requires (CConstructibleFrom) FORCEINLINE constexpr explicit TOptional(FInPlace, Ts&&... Args) : bIsValid(true) { new (&Value) OptionalType(Forward(Args)...); } template requires (CConstructibleFrom) && (!CSameAs, FInPlace>) && (!CBaseOf>) FORCEINLINE constexpr explicit (!CConvertibleTo) TOptional(T&& InValue) : TOptional(InPlace, Forward(InValue)) { } FORCEINLINE constexpr TOptional(const TOptional& InValue) requires (CTriviallyCopyConstructible) = default; FORCEINLINE constexpr TOptional(const TOptional& InValue) requires (CCopyConstructible && !CTriviallyCopyConstructible) : bIsValid(InValue.IsValid()) { if (InValue.IsValid()) new (&Value) OptionalType(InValue.GetValue()); } FORCEINLINE constexpr TOptional(TOptional&& InValue) requires (CTriviallyMoveConstructible) = default; FORCEINLINE constexpr TOptional(TOptional&& InValue) requires (CMoveConstructible && !CTriviallyMoveConstructible) : bIsValid(InValue.IsValid()) { if (InValue.IsValid()) new (&Value) OptionalType(MoveTemp(InValue.GetValue())); } template requires (CConstructibleFrom && NAMESPACE_PRIVATE::CTOptionalAllowUnwrappable) FORCEINLINE constexpr explicit (!CConvertibleTo) TOptional(const TOptional& InValue) : bIsValid(InValue.IsValid()) { if (InValue.IsValid()) new (&Value) OptionalType(InValue.GetValue()); } template requires (CConstructibleFrom && NAMESPACE_PRIVATE::CTOptionalAllowUnwrappable) FORCEINLINE constexpr explicit (!CConvertibleTo) TOptional(TOptional&& InValue) : bIsValid(InValue.IsValid()) { if (InValue.IsValid()) new (&Value) OptionalType(MoveTemp(InValue.GetValue())); } FORCEINLINE constexpr ~TOptional() requires (CTriviallyDestructible) = default; FORCEINLINE constexpr ~TOptional() requires (!CTriviallyDestructible) { Reset(); } FORCEINLINE constexpr TOptional& operator=(const TOptional& InValue) requires (CTriviallyCopyConstructible && CTriviallyCopyAssignable) = default; constexpr TOptional& operator=(const TOptional& InValue) requires (CCopyConstructible && CCopyAssignable && !CTriviallyCopyConstructible && !CTriviallyCopyAssignable) { 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; } FORCEINLINE constexpr TOptional& operator=(TOptional&& InValue) requires (CTriviallyMoveConstructible && CTriviallyMoveAssignable) = default; constexpr TOptional& operator=(TOptional&& InValue) requires (CMoveConstructible && CMoveAssignable && !CTriviallyMoveConstructible && !CTriviallyMoveAssignable) { 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 (CConstructibleFrom && CAssignableFrom && NAMESPACE_PRIVATE::CTOptionalAllowUnwrappable) 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 (CConstructibleFrom && CAssignableFrom && NAMESPACE_PRIVATE::CTOptionalAllowUnwrappable) 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 (CConstructibleFrom && CAssignableFrom) FORCEINLINE constexpr TOptional& operator=(T&& InValue) { if (IsValid()) GetValue() = Forward(InValue); else { new (&Value) OptionalType(Forward(InValue)); bIsValid = true; } return *this; } template requires (CConstructibleFrom) FORCEINLINE constexpr OptionalType& Emplace(ArgTypes&&... Args) { Reset(); OptionalType* Result = new (&Value) OptionalType(Forward(Args)...); bIsValid = true; return *Result; } FORCEINLINE constexpr bool IsValid() const { return bIsValid; } FORCEINLINE constexpr explicit operator bool() const { return bIsValid; } FORCEINLINE 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 *reinterpret_cast< OptionalType*>(&Value); } FORCEINLINE 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(*reinterpret_cast< OptionalType*>(&Value)); } FORCEINLINE 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 *reinterpret_cast(&Value); } FORCEINLINE 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(*reinterpret_cast(&Value)); } FORCEINLINE constexpr const OptionalType* operator->() const { return &GetValue(); } FORCEINLINE constexpr OptionalType* operator->() { return &GetValue(); } FORCEINLINE constexpr OptionalType& operator*() & { return GetValue(); } FORCEINLINE constexpr OptionalType&& operator*() && { return GetValue(); } FORCEINLINE constexpr const OptionalType& operator*() const& { return GetValue(); } FORCEINLINE constexpr const OptionalType&& operator*() const&& { return GetValue(); } FORCEINLINE constexpr OptionalType& Get( OptionalType& DefaultValue) & { return IsValid() ? GetValue() : DefaultValue; } FORCEINLINE constexpr const OptionalType& Get(const OptionalType& DefaultValue) const& { return IsValid() ? GetValue() : DefaultValue; } FORCEINLINE constexpr void Reset() { if (bIsValid) { bIsValid = false; typedef OptionalType DestructOptionalType; ((OptionalType*)&Value)->DestructOptionalType::~DestructOptionalType(); } } FORCEINLINE constexpr size_t GetTypeHash() const requires (CHashable) { if (!IsValid()) return 2824517378; return NAMESPACE_REDCRAFT::GetTypeHash(GetValue()); } template requires (CMoveConstructible && CSwappable) constexpr void Swap(TOptional& InValue) { if (!IsValid() && !InValue.IsValid()) return; if (IsValid() && !InValue.IsValid()) { InValue = MoveTemp(*this); Reset(); return; } if (InValue.IsValid() && !IsValid()) { *this = MoveTemp(InValue); InValue.Reset(); return; } NAMESPACE_REDCRAFT::Swap(GetValue(), InValue.GetValue()); } private: TAlignedStorage Value; bool bIsValid; }; template TOptional(T) -> TOptional; template requires (CWeaklyEqualityComparable) FORCEINLINE 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 requires (CSynthThreeWayComparable) FORCEINLINE constexpr partial_ordering operator<=>(const TOptional& LHS, const TOptional& RHS) { if (LHS.IsValid() != RHS.IsValid()) return partial_ordering::unordered; if (LHS.IsValid() == false) return partial_ordering::equivalent; return SynthThreeWayCompare(*LHS, *RHS); } template requires (CWeaklyEqualityComparable) FORCEINLINE constexpr bool operator==(const TOptional& LHS, const U& RHS) { return LHS.IsValid() ? *LHS == RHS : false; } template FORCEINLINE constexpr bool operator==(const TOptional& LHS, FInvalid) { return !LHS.IsValid(); } template requires (CDestructible) FORCEINLINE constexpr TOptional> MakeOptional(FInvalid) { return TOptional>(Invalid); } template requires (CDestructible && CConstructibleFrom) FORCEINLINE constexpr TOptional MakeOptional(T&& InValue) { return TOptional(Forward(InValue)); } template requires (CDestructible && CConstructibleFrom) FORCEINLINE constexpr TOptional MakeOptional(Ts&&... Args) { return TOptional(InPlace, Forward(Args)...); } NAMESPACE_PRIVATE_BEGIN template struct TIsTOptional : FFalse { }; template struct TIsTOptional> : FTrue { }; NAMESPACE_PRIVATE_END template concept CTOptional = NAMESPACE_PRIVATE::TIsTOptional>::Value; NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END