2022-03-09 15:17:54 +00:00
# pragma once
# include "CoreTypes.h"
2022-03-26 11:31:45 +00:00
# include "Templates/Utility.h"
2022-03-31 08:40:31 +00:00
# include "Templates/TypeHash.h"
2022-03-09 15:17:54 +00:00
# include "TypeTraits/TypeTraits.h"
2022-05-03 10:22:41 +00:00
# include "Miscellaneous/Compare.h"
2022-03-09 15:17:54 +00:00
# include "Miscellaneous/AssertionMacros.h"
NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN ( Redcraft )
NAMESPACE_MODULE_BEGIN ( Utility )
2022-12-16 11:30:55 +00:00
template < typename OptionalType > requires ( CDestructible < OptionalType > )
class TOptional ;
NAMESPACE_PRIVATE_BEGIN
2022-12-18 11:11:53 +00:00
template < typename T > struct TIsTOptional : FFalse { } ;
template < typename T > struct TIsTOptional < TOptional < T > > : FTrue { } ;
2022-12-16 11:30:55 +00:00
template < typename T , typename U >
concept CTOptionalAllowUnwrappable =
! ( CConstructibleFrom < U , TOptional < T > & >
| | CConstructibleFrom < U , const TOptional < T > & >
| | CConstructibleFrom < U , TOptional < T > & & >
| | CConstructibleFrom < U , const TOptional < T > & & >
| | CConvertibleTo < TOptional < T > & , U >
| | CConvertibleTo < const TOptional < T > & , U >
| | CConvertibleTo < TOptional < T > & & , U >
| | CConvertibleTo < const TOptional < T > & & , U >
| | CAssignableFrom < U & , TOptional < T > & >
| | CAssignableFrom < U & , const TOptional < T > & >
| | CAssignableFrom < U & , TOptional < T > & & >
| | CAssignableFrom < U & , const TOptional < T > & & > ) ;
NAMESPACE_PRIVATE_END
2022-12-18 11:11:53 +00:00
template < typename T > concept CTOptional = NAMESPACE_PRIVATE : : TIsTOptional < TRemoveCV < T > > : : Value ;
2022-11-16 14:03:54 +00:00
template < typename OptionalType > requires ( CDestructible < OptionalType > )
2022-11-15 11:28:43 +00:00
class TOptional
2022-03-09 15:17:54 +00:00
{
public :
2022-03-23 09:49:30 +00:00
using ValueType = OptionalType ;
2022-03-09 15:17:54 +00:00
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr TOptional ( ) : bIsValid ( false ) { }
2022-03-09 15:17:54 +00:00
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr TOptional ( FInvalid ) : TOptional ( ) { }
2022-03-13 15:18:07 +00:00
2022-11-16 14:03:54 +00:00
template < typename . . . Ts > requires ( CConstructibleFrom < OptionalType , Ts . . . > )
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr explicit TOptional ( FInPlace , Ts & & . . . Args )
2022-03-09 15:17:54 +00:00
: bIsValid ( true )
{
2022-12-12 14:07:12 +00:00
new ( & Value ) OptionalType ( Forward < Ts > ( Args ) . . . ) ;
2022-03-09 15:17:54 +00:00
}
2022-11-16 14:03:54 +00:00
template < typename T = OptionalType > requires ( CConstructibleFrom < OptionalType , T & & > )
2022-12-19 10:00:52 +00:00
& & ( ! CSameAs < TRemoveCVRef < T > , FInPlace > ) & & ( ! CSameAs < TOptional , TRemoveCVRef < T > > )
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr explicit ( ! CConvertibleTo < T & & , OptionalType > ) TOptional ( T & & InValue )
2022-03-13 15:18:07 +00:00
: TOptional ( InPlace , Forward < T > ( InValue ) )
2022-03-09 15:17:54 +00:00
{ }
2022-03-13 15:18:07 +00:00
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr TOptional ( const TOptional & InValue ) requires ( CTriviallyCopyConstructible < OptionalType > ) = default ;
2022-11-17 15:17:50 +00:00
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr TOptional ( const TOptional & InValue ) requires ( CCopyConstructible < OptionalType > & & ! CTriviallyCopyConstructible < OptionalType > )
2022-03-13 15:18:07 +00:00
: bIsValid ( InValue . IsValid ( ) )
{
2022-12-12 14:07:12 +00:00
if ( InValue . IsValid ( ) ) new ( & Value ) OptionalType ( InValue . GetValue ( ) ) ;
2022-03-13 15:18:07 +00:00
}
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr TOptional ( TOptional & & InValue ) requires ( CTriviallyMoveConstructible < OptionalType > ) = default ;
2022-11-17 15:17:50 +00:00
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr TOptional ( TOptional & & InValue ) requires ( CMoveConstructible < OptionalType > & & ! CTriviallyMoveConstructible < OptionalType > )
2022-03-13 15:18:07 +00:00
: bIsValid ( InValue . IsValid ( ) )
{
2022-12-12 14:07:12 +00:00
if ( InValue . IsValid ( ) ) new ( & Value ) OptionalType ( MoveTemp ( InValue . GetValue ( ) ) ) ;
2022-03-13 15:18:07 +00:00
}
2022-03-09 15:17:54 +00:00
2022-12-16 11:30:55 +00:00
template < typename T = OptionalType > requires ( CConstructibleFrom < OptionalType , const T & > & & NAMESPACE_PRIVATE : : CTOptionalAllowUnwrappable < T , OptionalType > )
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr explicit ( ! CConvertibleTo < const T & , OptionalType > ) TOptional ( const TOptional < T > & InValue )
2022-03-13 15:18:07 +00:00
: bIsValid ( InValue . IsValid ( ) )
2022-03-09 15:17:54 +00:00
{
2022-12-12 14:07:12 +00:00
if ( InValue . IsValid ( ) ) new ( & Value ) OptionalType ( InValue . GetValue ( ) ) ;
2022-03-09 15:17:54 +00:00
}
2022-12-16 11:30:55 +00:00
template < typename T = OptionalType > requires ( CConstructibleFrom < OptionalType , T & & > & & NAMESPACE_PRIVATE : : CTOptionalAllowUnwrappable < T , OptionalType > )
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr explicit ( ! CConvertibleTo < T & & , OptionalType > ) TOptional ( TOptional < T > & & InValue )
2022-03-13 15:18:07 +00:00
: bIsValid ( InValue . IsValid ( ) )
2022-03-09 15:17:54 +00:00
{
2022-12-12 14:07:12 +00:00
if ( InValue . IsValid ( ) ) new ( & Value ) OptionalType ( MoveTemp ( InValue . GetValue ( ) ) ) ;
2022-03-09 15:17:54 +00:00
}
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr ~ TOptional ( ) requires ( CTriviallyDestructible < OptionalType > ) = default ;
2022-11-17 15:17:50 +00:00
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr ~ TOptional ( ) requires ( ! CTriviallyDestructible < OptionalType > )
2022-03-09 15:17:54 +00:00
{
2022-11-17 15:17:50 +00:00
Reset ( ) ;
2022-03-09 15:17:54 +00:00
}
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr TOptional & operator = ( const TOptional & InValue ) requires ( CTriviallyCopyConstructible < OptionalType > & & CTriviallyCopyAssignable < OptionalType > ) = default ;
2022-11-17 15:17:50 +00:00
2022-12-16 15:23:05 +00:00
constexpr TOptional & operator = ( const TOptional & InValue ) requires ( CCopyConstructible < OptionalType > & & CCopyAssignable < OptionalType >
2022-11-17 15:17:50 +00:00
& & ! CTriviallyCopyConstructible < OptionalType > & & ! CTriviallyCopyAssignable < OptionalType > )
2022-03-09 15:17:54 +00:00
{
2022-03-13 15:18:07 +00:00
if ( & InValue = = this ) return * this ;
2022-03-09 15:17:54 +00:00
2022-03-13 15:18:07 +00:00
if ( ! InValue . IsValid ( ) )
{
Reset ( ) ;
return * this ;
}
2022-03-09 15:17:54 +00:00
2022-03-13 15:18:07 +00:00
if ( IsValid ( ) ) GetValue ( ) = InValue . GetValue ( ) ;
else
2022-03-09 15:17:54 +00:00
{
2022-12-12 14:07:12 +00:00
new ( & Value ) OptionalType ( InValue . GetValue ( ) ) ;
2022-03-09 15:17:54 +00:00
bIsValid = true ;
}
return * this ;
}
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr TOptional & operator = ( TOptional & & InValue ) requires ( CTriviallyMoveConstructible < OptionalType > & & CTriviallyMoveAssignable < OptionalType > ) = default ;
2022-11-17 15:17:50 +00:00
2022-12-16 15:23:05 +00:00
constexpr TOptional & operator = ( TOptional & & InValue ) requires ( CMoveConstructible < OptionalType > & & CMoveAssignable < OptionalType >
2022-11-17 15:17:50 +00:00
& & ! CTriviallyMoveConstructible < OptionalType > & & ! CTriviallyMoveAssignable < OptionalType > )
2022-03-09 15:17:54 +00:00
{
2022-03-13 15:18:07 +00:00
if ( & InValue = = this ) return * this ;
2022-03-09 15:17:54 +00:00
2022-03-13 15:18:07 +00:00
if ( ! InValue . IsValid ( ) )
{
Reset ( ) ;
return * this ;
}
2022-03-09 15:17:54 +00:00
2022-03-13 15:18:07 +00:00
if ( IsValid ( ) ) GetValue ( ) = MoveTemp ( InValue . GetValue ( ) ) ;
else
2022-03-09 15:17:54 +00:00
{
2022-12-12 14:07:12 +00:00
new ( & Value ) OptionalType ( MoveTemp ( InValue . GetValue ( ) ) ) ;
2022-03-09 15:17:54 +00:00
bIsValid = true ;
}
return * this ;
}
2022-11-16 14:03:54 +00:00
template < typename T = OptionalType > requires ( CConstructibleFrom < OptionalType , const T & >
2022-12-16 11:30:55 +00:00
& & CAssignableFrom < OptionalType & , const T & > & & NAMESPACE_PRIVATE : : CTOptionalAllowUnwrappable < T , OptionalType > )
2022-12-16 15:23:05 +00:00
constexpr TOptional & operator = ( const TOptional < T > & InValue )
2022-03-09 15:17:54 +00:00
{
2022-03-13 15:18:07 +00:00
if ( ! InValue . IsValid ( ) )
{
Reset ( ) ;
return * this ;
}
2022-03-09 15:17:54 +00:00
2022-03-13 15:18:07 +00:00
if ( IsValid ( ) ) GetValue ( ) = InValue . GetValue ( ) ;
else
{
2022-12-12 14:07:12 +00:00
new ( & Value ) OptionalType ( InValue . GetValue ( ) ) ;
2022-03-13 15:18:07 +00:00
bIsValid = true ;
}
2022-03-09 15:17:54 +00:00
return * this ;
}
2022-11-16 14:03:54 +00:00
template < typename T = OptionalType > requires ( CConstructibleFrom < OptionalType , T & & >
2022-12-16 11:30:55 +00:00
& & CAssignableFrom < OptionalType & , T & & > & & NAMESPACE_PRIVATE : : CTOptionalAllowUnwrappable < T , OptionalType > )
2022-12-16 15:23:05 +00:00
constexpr TOptional & operator = ( TOptional < T > & & InValue )
2022-03-13 15:18:07 +00:00
{
if ( ! InValue . IsValid ( ) )
{
Reset ( ) ;
return * this ;
}
if ( IsValid ( ) ) GetValue ( ) = MoveTemp ( InValue . GetValue ( ) ) ;
else
{
2022-12-12 14:07:12 +00:00
new ( & Value ) OptionalType ( MoveTemp ( InValue . GetValue ( ) ) ) ;
2022-03-13 15:18:07 +00:00
bIsValid = true ;
}
return * this ;
}
2022-11-16 14:03:54 +00:00
template < typename T = OptionalType > requires ( CConstructibleFrom < OptionalType , T & & > & & CAssignableFrom < OptionalType & , T & & > )
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr TOptional & operator = ( T & & InValue )
2022-03-13 15:18:07 +00:00
{
if ( IsValid ( ) ) GetValue ( ) = Forward < T > ( InValue ) ;
else
{
2022-12-12 14:07:12 +00:00
new ( & Value ) OptionalType ( Forward < T > ( InValue ) ) ;
2022-03-13 15:18:07 +00:00
bIsValid = true ;
}
return * this ;
}
2022-12-18 11:11:53 +00:00
template < typename T = OptionalType > requires ( CWeaklyEqualityComparable < OptionalType , T > )
friend FORCEINLINE constexpr bool operator = = ( const TOptional & LHS , const TOptional < T > & RHS )
{
if ( LHS . IsValid ( ) ! = RHS . IsValid ( ) ) return false ;
if ( LHS . IsValid ( ) = = false ) return true ;
return * LHS = = * RHS ;
}
template < typename T = OptionalType > requires ( CSynthThreeWayComparable < OptionalType , T > )
friend FORCEINLINE constexpr partial_ordering operator < = > ( const TOptional & LHS , const TOptional < T > & RHS )
{
if ( LHS . IsValid ( ) ! = RHS . IsValid ( ) ) return partial_ordering : : unordered ;
if ( LHS . IsValid ( ) = = false ) return partial_ordering : : equivalent ;
return SynthThreeWayCompare ( * LHS , * RHS ) ;
}
template < typename T = OptionalType > requires ( ! CTOptional < T > & & CWeaklyEqualityComparable < OptionalType , T > )
FORCEINLINE constexpr bool operator = = ( const T & InValue ) const &
{
return IsValid ( ) ? GetValue ( ) = = InValue : false ;
}
template < typename T = OptionalType > requires ( ! CTOptional < T > & & CSynthThreeWayComparable < OptionalType , T > )
FORCEINLINE constexpr partial_ordering operator < = > ( const T & InValue ) const &
{
return IsValid ( ) ? SynthThreeWayCompare ( GetValue ( ) , InValue ) : partial_ordering : : unordered ;
}
FORCEINLINE constexpr bool operator = = ( FInvalid ) const & { return ! IsValid ( ) ; }
2022-03-09 15:17:54 +00:00
2022-11-16 14:03:54 +00:00
template < typename . . . ArgTypes > requires ( CConstructibleFrom < OptionalType , ArgTypes . . . > )
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr OptionalType & Emplace ( ArgTypes & & . . . Args )
2022-03-09 15:17:54 +00:00
{
Reset ( ) ;
2022-12-12 14:07:12 +00:00
OptionalType * Result = new ( & Value ) OptionalType ( Forward < ArgTypes > ( Args ) . . . ) ;
2022-03-09 15:17:54 +00:00
bIsValid = true ;
return * Result ;
}
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr bool IsValid ( ) const { return bIsValid ; }
FORCEINLINE constexpr explicit operator bool ( ) const { return bIsValid ; }
2022-03-23 09:49:30 +00:00
2022-12-13 14:02:39 +00:00
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 < const 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 MoveTemp ( * reinterpret_cast < const OptionalType * > ( & Value ) ) ; }
2022-03-09 15:17:54 +00:00
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr const OptionalType * operator - > ( ) const { return & GetValue ( ) ; }
FORCEINLINE constexpr OptionalType * operator - > ( ) { return & GetValue ( ) ; }
2022-03-09 15:17:54 +00:00
2022-12-13 14:02:39 +00:00
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 ( ) ; }
2022-03-09 15:17:54 +00:00
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr OptionalType & Get ( OptionalType & DefaultValue ) & { return IsValid ( ) ? GetValue ( ) : DefaultValue ; }
FORCEINLINE constexpr const OptionalType & Get ( const OptionalType & DefaultValue ) const & { return IsValid ( ) ? GetValue ( ) : DefaultValue ; }
2022-03-09 15:17:54 +00:00
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr void Reset ( )
2022-03-09 15:17:54 +00:00
{
if ( bIsValid )
{
bIsValid = false ;
2022-03-13 15:18:07 +00:00
typedef OptionalType DestructOptionalType ;
( ( OptionalType * ) & Value ) - > DestructOptionalType : : ~ DestructOptionalType ( ) ;
2022-03-09 15:17:54 +00:00
}
}
2022-12-19 10:00:52 +00:00
friend FORCEINLINE constexpr size_t GetTypeHash ( const TOptional & A ) requires ( CHashable < OptionalType > )
2022-04-22 14:28:44 +00:00
{
2022-12-19 10:00:52 +00:00
if ( ! A . IsValid ( ) ) return 2824517378 ;
return GetTypeHash ( A . GetValue ( ) ) ;
2022-04-22 14:28:44 +00:00
}
2022-11-16 14:03:54 +00:00
template < typename T > requires ( CMoveConstructible < OptionalType > & & CSwappable < OptionalType > )
2022-12-19 10:00:52 +00:00
friend constexpr void Swap ( TOptional & A , TOptional & B )
2022-04-14 14:41:22 +00:00
{
2022-12-19 10:00:52 +00:00
if ( ! A . IsValid ( ) & & ! B . IsValid ( ) ) return ;
2022-04-14 14:41:22 +00:00
2022-12-19 10:00:52 +00:00
if ( A . IsValid ( ) & & ! B . IsValid ( ) )
2022-04-14 14:41:22 +00:00
{
2022-12-19 10:00:52 +00:00
B = MoveTemp ( A ) ;
A . Reset ( ) ;
2022-04-14 14:41:22 +00:00
}
2022-12-19 10:00:52 +00:00
else if ( ! A . IsValid ( ) & & B . IsValid ( ) )
2022-04-14 14:41:22 +00:00
{
2022-12-19 10:00:52 +00:00
A = MoveTemp ( B ) ;
B . Reset ( ) ;
}
else
{
Swap ( A . GetValue ( ) , B . GetValue ( ) ) ;
2022-04-14 14:41:22 +00:00
}
}
2022-03-09 15:17:54 +00:00
private :
2022-05-22 14:52:47 +00:00
TAlignedStorage < sizeof ( OptionalType ) , alignof ( OptionalType ) > Value ;
2022-03-09 15:17:54 +00:00
bool bIsValid ;
} ;
template < typename T >
2022-04-13 14:35:30 +00:00
TOptional ( T ) - > TOptional < T > ;
2022-03-09 15:17:54 +00:00
2022-11-16 14:03:54 +00:00
template < typename T > requires ( CDestructible < T > )
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr TOptional < TDecay < T > > MakeOptional ( FInvalid )
2022-03-14 10:03:57 +00:00
{
2022-05-22 14:52:47 +00:00
return TOptional < TDecay < T > > ( Invalid ) ;
2022-03-14 10:03:57 +00:00
}
2022-11-16 14:03:54 +00:00
template < typename T > requires ( CDestructible < T > & & CConstructibleFrom < T , T & & > )
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr TOptional < T > MakeOptional ( T & & InValue )
2022-03-09 15:17:54 +00:00
{
2022-05-15 14:56:53 +00:00
return TOptional < T > ( Forward < T > ( InValue ) ) ;
2022-03-09 15:17:54 +00:00
}
2022-11-16 14:03:54 +00:00
template < typename T , typename . . . Ts > requires ( CDestructible < T > & & CConstructibleFrom < T , Ts . . . > )
2022-12-13 14:02:39 +00:00
FORCEINLINE constexpr TOptional < T > MakeOptional ( Ts & & . . . Args )
2022-03-09 15:17:54 +00:00
{
return TOptional < T > ( InPlace , Forward < T > ( Args ) . . . ) ;
}
NAMESPACE_MODULE_END ( Utility )
NAMESPACE_MODULE_END ( Redcraft )
NAMESPACE_REDCRAFT_END