#pragma once #include "CoreTypes.h" #include "Templates/Utility.h" #include "Templates/TypeHash.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 requires TIsObject::Value && (!TIsArray::Value) && TIsDestructible::Value struct TOptional { private: template struct TAllowUnwrapping : TBoolConstant& >::Value || TIsConstructible& >::Value || TIsConstructible&&>::Value || TIsConstructible&&>::Value || TIsConvertible< TOptional&, OptionalType>::Value || TIsConvertible&, OptionalType>::Value || TIsConvertible< TOptional&&, OptionalType>::Value || TIsConvertible&&, OptionalType>::Value )> { }; public: using ValueType = 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) requires TIsCopyConstructible::Value : bIsValid(InValue.IsValid()) { if (InValue.IsValid()) new(&Value) OptionalType(InValue.GetValue()); } constexpr TOptional(TOptional&& InValue) requires TIsMoveConstructible::Value : bIsValid(InValue.IsValid()) { if (InValue.IsValid()) new(&Value) OptionalType(MoveTemp(InValue.GetValue())); } template requires TIsConstructible::Value && TAllowUnwrapping::Value constexpr explicit (!TIsConvertible::Value) TOptional(const TOptional& InValue) : bIsValid(InValue.IsValid()) { if (InValue.IsValid()) new(&Value) OptionalType(InValue.GetValue()); } template requires TIsConstructible::Value && TAllowUnwrapping::Value constexpr explicit (!TIsConvertible::Value) TOptional(TOptional&& InValue) : bIsValid(InValue.IsValid()) { if (InValue.IsValid()) new(&Value) OptionalType(MoveTemp(InValue.GetValue())); } constexpr ~TOptional() { if constexpr (!TIsTriviallyDestructible::Value) Reset(); } constexpr TOptional& operator=(const TOptional& InValue) requires TIsCopyConstructible::Value && TIsCopyAssignable::Value { 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) requires TIsMoveConstructible::Value && TIsMoveAssignable::Value { 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 && TIsAssignable::Value && TAllowUnwrapping::Value 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; } template requires TIsConstructible::Value && TIsAssignable::Value && TAllowUnwrapping::Value 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 && TIsAssignable::Value constexpr TOptional& operator=(T&& InValue) { if (IsValid()) GetValue() = Forward(InValue); else { new(&Value) OptionalType(Forward(InValue)); bIsValid = true; } return *this; } template requires TIsConstructible::Value constexpr OptionalType& Emplace(ArgTypes&&... 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 void* GetData() { return &Value; } constexpr const void* GetData() const { return &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 *reinterpret_cast< 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(*reinterpret_cast< 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 *reinterpret_cast(&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(*reinterpret_cast(&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(); } constexpr OptionalType& Get( OptionalType& DefaultValue) & { return IsValid() ? GetValue() : DefaultValue; } constexpr const OptionalType& Get(const OptionalType& DefaultValue) const& { return IsValid() ? GetValue() : DefaultValue; } constexpr void Reset() { if (bIsValid) { bIsValid = false; typedef OptionalType DestructOptionalType; ((OptionalType*)&Value)->DestructOptionalType::~DestructOptionalType(); } } constexpr size_t GetTypeHash() const requires CHashable { if (!IsValid()) return 2824517378; return NAMESPACE_REDCRAFT::GetTypeHash(GetValue()); } template requires TIsMoveConstructible::Value && TIsSwappable::Value 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::Type Value; bool bIsValid; }; template TOptional(T) -> TOptional; template requires CWeaklyEqualityComparableWith 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 CWeaklyEqualityComparableWith constexpr bool operator==(const TOptional& LHS, const U& RHS) { return LHS.IsValid() ? *LHS == RHS : false; } template constexpr bool operator==(const TOptional& LHS, FInvalid) { return !LHS.IsValid(); } template requires (TIsObject::Value && !TIsArray::Value && TIsDestructible::Value) constexpr TOptional::Type> MakeOptional(FInvalid) { return TOptional::Type>(Invalid); } template requires (TIsObject::Value && !TIsArray::Value && TIsDestructible::Value) constexpr TOptional::Type> MakeOptional(T&& InValue) { return TOptional::Type>(Forward(InValue)); } template requires (TIsObject::Value && !TIsArray::Value && TIsDestructible::Value) constexpr TOptional MakeOptional(Types&&... Args) { return TOptional(InPlace, Forward(Args)...); } template struct TIsTOptional : FFalse { }; template struct TIsTOptional> : FTrue { }; NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END