#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) /** The class template manages an optional contained value or reference, i.e. a value or reference that may or may not be present. */ template > requires (CDestructible>) class TOptional; NAMESPACE_PRIVATE_BEGIN template struct TIsTOptional : FFalse { }; template struct TIsTOptional> : FTrue { }; 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 concept CTOptional = NAMESPACE_PRIVATE::TIsTOptional>::Value; /** The class template manages an optional contained value, i.e. a value that may or may not be present. */ template requires (!CReference) class TOptional final { public: using FValueType = T; static_assert(!CReference); /** Constructs an object that does not contain a value. */ FORCEINLINE constexpr TOptional() : bIsValid(false) { } /** Constructs an object that does not contain a value. */ FORCEINLINE constexpr TOptional(FInvalid) : TOptional() { } /** Constructs an object with initial content an object, direct-initialized from Forward(InValue). */ template requires (CConstructibleFrom && !CSameAs, FInPlace> && !CSameAs>) FORCEINLINE constexpr explicit (!CConvertibleTo) TOptional(U&& InValue) : TOptional(InPlace, Forward(InValue)) { } /** Constructs an object with initial content an object, direct-non-list-initialized from Forward(Args).... */ template requires (CConstructibleFrom) FORCEINLINE constexpr explicit TOptional(FInPlace, Ts&&... Args) : bIsValid(true) { new (&Value) FValueType(Forward(Args)...); } /** Constructs an object with initial content an object, direct-non-list-initialized from IL, Forward(Args).... */ template requires (CConstructibleFrom, Ts...>) FORCEINLINE constexpr explicit TOptional(FInPlace, initializer_list IL, Ts&&... Args) : bIsValid(true) { new (&Value) FValueType(IL, Forward(Args)...); } /** Copies content of other into a new instance. */ FORCEINLINE constexpr TOptional(const TOptional& InValue) requires (CTriviallyCopyConstructible) = default; /** Copies content of other into a new instance. */ FORCEINLINE constexpr TOptional(const TOptional& InValue) requires (CCopyConstructible && !CTriviallyCopyConstructible) : bIsValid(InValue.IsValid()) { if (InValue.IsValid()) new (&Value) FValueType(InValue.GetValue()); } /** Moves content of other into a new instance. */ FORCEINLINE constexpr TOptional(TOptional&& InValue) requires (CTriviallyMoveConstructible) = default; /** Moves content of other into a new instance. */ FORCEINLINE constexpr TOptional(TOptional&& InValue) requires (CMoveConstructible && !CTriviallyMoveConstructible) : bIsValid(InValue.IsValid()) { if (InValue.IsValid()) new (&Value) FValueType(MoveTemp(InValue.GetValue())); } /** Converting copy constructor. */ template requires (CConstructibleFrom && NAMESPACE_PRIVATE::CTOptionalAllowUnwrappable) FORCEINLINE constexpr explicit (!CConvertibleTo) TOptional(const TOptional& InValue) : bIsValid(InValue.IsValid()) { if (InValue.IsValid()) new (&Value) FValueType(InValue.GetValue()); } /** Converting move constructor. */ template requires (CConstructibleFrom && NAMESPACE_PRIVATE::CTOptionalAllowUnwrappable) FORCEINLINE constexpr explicit (!CConvertibleTo) TOptional(TOptional&& InValue) : bIsValid(InValue.IsValid()) { if (InValue.IsValid()) new (&Value) FValueType(MoveTemp(InValue.GetValue())); } /** Destroys the contained object, if any, as if by a call to Reset(). */ FORCEINLINE constexpr ~TOptional() requires (CTriviallyDestructible) = default; /** Destroys the contained object, if any, as if by a call to Reset(). */ FORCEINLINE constexpr ~TOptional() requires (!CTriviallyDestructible) { Reset(); } /** Assigns by copying the state of 'InValue'. */ FORCEINLINE constexpr TOptional& operator=(const TOptional& InValue) requires (CTriviallyCopyConstructible && CTriviallyCopyAssignable) = default; /** Assigns by copying the state of 'InValue'. */ 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) FValueType(InValue.GetValue()); bIsValid = true; } return *this; } /** Assigns by moving the state of 'InValue'. */ FORCEINLINE constexpr TOptional& operator=(TOptional&& InValue) requires (CTriviallyMoveConstructible && CTriviallyMoveAssignable) = default; /** Assigns by moving the state of 'InValue'. */ 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) FValueType(MoveTemp(InValue.GetValue())); bIsValid = true; } return *this; } /** Assigns by copying the state of 'InValue'. */ 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) FValueType(InValue.GetValue()); bIsValid = true; } return *this; } /** Assigns by moving the state of 'InValue'. */ 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) FValueType(MoveTemp(InValue.GetValue())); bIsValid = true; } return *this; } /** Assigns the value of 'InValue'. */ template requires (CConstructibleFrom && CAssignableFrom) FORCEINLINE constexpr TOptional& operator=(U&& InValue) { if (IsValid()) GetValue() = Forward(InValue); else { new (&Value) FValueType(Forward(InValue)); bIsValid = true; } return *this; } /** Check if the two optional are equivalent. */ template requires (CWeaklyEqualityComparable) NODISCARD friend 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; } /** Check the order relationship between two optional. */ template requires (CSynthThreeWayComparable) NODISCARD friend 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); } /** Check if the optional value is equivalent to 'InValue'. */ template requires (!CTOptional && CWeaklyEqualityComparable) NODISCARD FORCEINLINE constexpr bool operator==(const U& InValue) const& { return IsValid() ? GetValue() == InValue : false; } /** Check that the optional value is in ordered relationship with 'InValue'. */ template requires (!CTOptional && CSynthThreeWayComparable) NODISCARD FORCEINLINE constexpr partial_ordering operator<=>(const U& InValue) const& { return IsValid() ? SynthThreeWayCompare(GetValue(), InValue) : partial_ordering::unordered; } /** @return true if instance does not contain a value, otherwise false. */ NODISCARD FORCEINLINE constexpr bool operator==(FInvalid) const& { return !IsValid(); } /** * Changes the contained object to one constructed from the arguments. * First destroys the current contained object (if any) by Reset(), * then constructs an object, direct-non-list-initialized from Forward(Args)..., as the contained object. * * @param Args - The arguments to be passed to the constructor of the object. * * @return A reference to the new object. */ template requires (CConstructibleFrom) FORCEINLINE constexpr T& Emplace(Ts&&... Args) { Reset(); T* Result = new (&Value) FValueType(Forward(Args)...); bIsValid = true; return *Result; } /** * Changes the contained object to one constructed from the arguments. * First destroys the current contained object (if any) by Reset(), * then constructs an object, direct-non-list-initialized from IL, Forward(Args)..., as the contained object. * * @param IL, Args - The arguments to be passed to the constructor of the object. * * @return A reference to the new object. */ template requires (CConstructibleFrom, Ts...>) FORCEINLINE constexpr T& Emplace(initializer_list IL, Ts&&... Args) { Reset(); T* Result = new (&Value) FValueType(IL, Forward(Args)...); bIsValid = true; return *Result; } /** @return true if instance contains a value, otherwise false. */ NODISCARD FORCEINLINE constexpr bool IsValid() const { return bIsValid; } NODISCARD FORCEINLINE constexpr explicit operator bool() const { return bIsValid; } /** @return The contained object. */ NODISCARD FORCEINLINE constexpr T& 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< T*>(&Value); } NODISCARD FORCEINLINE constexpr T&& 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< T*>(&Value)); } NODISCARD FORCEINLINE constexpr const T& 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); } NODISCARD FORCEINLINE constexpr const T&& 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)); } /** @return The contained object. */ NODISCARD FORCEINLINE constexpr T& operator*() & { 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< T*>(&Value); } NODISCARD FORCEINLINE constexpr T&& operator*() && { 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< T*>(&Value)); } NODISCARD FORCEINLINE constexpr const T& operator*() 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); } NODISCARD FORCEINLINE constexpr const T&& operator*() 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)); } /** @return The pointer to the contained object. */ NODISCARD FORCEINLINE constexpr const T* operator->() const { return &GetValue(); } NODISCARD FORCEINLINE constexpr T* operator->() { return &GetValue(); } /** @return The contained object when IsValid() returns true, 'DefaultValue' otherwise. */ NODISCARD FORCEINLINE constexpr T& Get( T& DefaultValue) & { return IsValid() ? GetValue() : DefaultValue; } NODISCARD FORCEINLINE constexpr const T& Get(const T& DefaultValue) const& { return IsValid() ? GetValue() : DefaultValue; } /** If not empty, destroys the contained object. */ FORCEINLINE constexpr void Reset() { if (bIsValid) { bIsValid = false; reinterpret_cast(&Value)->~FValueType(); } } /** Overloads the GetTypeHash algorithm for TOptional. */ NODISCARD friend FORCEINLINE constexpr size_t GetTypeHash(const TOptional& A) requires (CHashable) { if (!A.IsValid()) return 2824517378; return GetTypeHash(A.GetValue()); } /** Overloads the Swap algorithm for TOptional. */ friend constexpr void Swap(TOptional& A, TOptional& B) requires (CMoveConstructible && CSwappable) { if (!A.IsValid() && !B.IsValid()) return; if (A.IsValid() && !B.IsValid()) { B = MoveTemp(A); A.Reset(); } else if (!A.IsValid() && B.IsValid()) { A = MoveTemp(B); B.Reset(); } else { Swap(A.GetValue(), B.GetValue()); } } private: TAlignedStorage Value; bool bIsValid; }; /** The class template manages an optional contained reference, i.e. a reference that may or may not be present. */ template requires (CLValueReference) class TOptional final { public: using FValueType = TRemoveReference; static_assert(!CReference); /** Constructs an object that does not contain a reference. */ FORCEINLINE constexpr TOptional() : Ptr(nullptr) { } /** Constructs an object that does not contain a reference. */ FORCEINLINE constexpr TOptional(FInvalid) : TOptional() { } /** Constructs an object with initial content an object, direct-initialized from Forward(InValue). */ template requires (CConstructibleFrom) && (!CSameAs, FInPlace>) && (!CSameAs>) FORCEINLINE constexpr explicit (!CConvertibleTo) TOptional(U&& InValue) : Ptr(AddressOf(static_cast(Forward(InValue)))) { } /** Copies content of other into a new instance. */ FORCEINLINE constexpr TOptional(const TOptional&) = default; FORCEINLINE constexpr TOptional(TOptional&&) = default; /** Converting constructor. */ template requires (CConstructibleFrom && NAMESPACE_PRIVATE::CTOptionalAllowUnwrappable) FORCEINLINE constexpr explicit (!CConvertibleTo) TOptional(TOptional InValue) : Ptr(InValue.IsValid() ? AddressOf(static_cast(InValue.GetValue())) : nullptr) { } /** Converting constructor. */ template requires (!CConst && CConstructibleFrom && NAMESPACE_PRIVATE::CTOptionalAllowUnwrappable) FORCEINLINE constexpr explicit (!CConvertibleTo) TOptional(TOptional& InValue) : Ptr(InValue.IsValid() ? AddressOf(static_cast(InValue.GetValue())) : nullptr) { } /** Converting constructor. */ template requires (CConst && CConstructibleFrom && NAMESPACE_PRIVATE::CTOptionalAllowUnwrappable) FORCEINLINE constexpr explicit (!CConvertibleTo) TOptional(const TOptional& InValue) : Ptr(InValue.IsValid() ? AddressOf(static_cast(InValue.GetValue())) : nullptr) { } /** Assigns by copying the state of 'InValue'. */ FORCEINLINE constexpr TOptional& operator=(const TOptional&) = default; FORCEINLINE constexpr TOptional& operator=(TOptional&&) = default; /** Destructor. */ FORCEINLINE constexpr ~TOptional() = default; /** Check if the two optional are equivalent. */ template requires (CWeaklyEqualityComparable) NODISCARD friend FORCEINLINE constexpr bool operator==(TOptional LHS, TOptional RHS) { if (LHS.IsValid() != RHS.IsValid()) return false; if (LHS.IsValid() == false) return true; return *LHS == *RHS; } /** Check the order relationship between two optional. */ template requires (CSynthThreeWayComparable) NODISCARD friend FORCEINLINE constexpr partial_ordering operator<=>(TOptional LHS, TOptional RHS) { if (LHS.IsValid() != RHS.IsValid()) return partial_ordering::unordered; if (LHS.IsValid() == false) return partial_ordering::equivalent; return SynthThreeWayCompare(*LHS, *RHS); } /** Check if the optional reference is equivalent to 'InValue'. */ template requires (!CTOptional && CWeaklyEqualityComparable) NODISCARD FORCEINLINE constexpr bool operator==(const U& InValue) const& { return IsValid() ? GetValue() == InValue : false; } /** Check that the optional reference is in ordered relationship with 'InValue'. */ template requires (!CTOptional && CSynthThreeWayComparable) NODISCARD FORCEINLINE constexpr partial_ordering operator<=>(const U& InValue) const& { return IsValid() ? SynthThreeWayCompare(GetValue(), InValue) : partial_ordering::unordered; } /** @return true if instance does not contain a reference, otherwise false. */ NODISCARD FORCEINLINE constexpr bool operator==(FInvalid) const& { return !IsValid(); } /** @return true if instance contains a reference, otherwise false. */ NODISCARD FORCEINLINE constexpr bool IsValid() const { return Ptr != nullptr; } NODISCARD FORCEINLINE constexpr explicit operator bool() const { return Ptr != nullptr; } /** @return The contained object. */ NODISCARD FORCEINLINE constexpr T 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 *Ptr; } /** @return The pointer to the contained object. */ NODISCARD FORCEINLINE constexpr auto operator->() const { return Ptr; } /** @return The contained object. */ NODISCARD FORCEINLINE constexpr T operator*() const { return GetValue(); } /** @return The contained object when IsValid() returns true, 'DefaultValue' otherwise. */ NODISCARD FORCEINLINE constexpr T Get(T DefaultValue) const { return IsValid() ? GetValue() : DefaultValue; } /** If not empty, destroys the contained object. */ FORCEINLINE constexpr void Reset() { Ptr = nullptr; } /** Overloads the GetTypeHash algorithm for TOptional. */ NODISCARD friend FORCEINLINE constexpr size_t GetTypeHash(const TOptional& A) requires (CHashable) { if (!A.IsValid()) return 2824517378; return GetTypeHash(A.GetValue()); } private: FValueType* Ptr; }; template TOptional(T) -> TOptional; /** Creates an optional object from value. */ template requires (CDestructible> && CConstructibleFrom, T>) NODISCARD FORCEINLINE constexpr TOptional> MakeOptional(T&& InValue) { return TOptional>(Forward(InValue)); } /** Creates an optional object constructed in-place from Args.... */ template requires (CDestructible && CConstructibleFrom) NODISCARD FORCEINLINE constexpr TOptional MakeOptional(Ts&&... Args) { return TOptional(InPlace, Forward(Args)...); } /** Creates an optional object constructed in-place from IL, Args.... */ template requires (CDestructible && CConstructibleFrom, Ts...>) NODISCARD FORCEINLINE constexpr TOptional MakeOptional(initializer_list IL, Ts&&... Args) { return TOptional(InPlace, IL, Forward(Args)...); } NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END