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-05-16 14:42:17 +00:00
template < typename OptionalType > requires CDestructible < OptionalType >
2022-03-09 15:17:54 +00:00
struct TOptional
{
2022-04-13 14:35:30 +00:00
private :
template < typename T >
struct TAllowUnwrapping : TBoolConstant < ! (
2022-05-20 15:35:36 +00:00
CConstructibleFrom < OptionalType , TOptional < T > & >
| | CConstructibleFrom < OptionalType , const TOptional < T > & >
| | CConstructibleFrom < OptionalType , TOptional < T > & & >
| | CConstructibleFrom < OptionalType , const TOptional < T > & & >
2022-05-16 15:09:04 +00:00
| | CConvertibleTo < TOptional < T > & , OptionalType >
| | CConvertibleTo < const TOptional < T > & , OptionalType >
| | CConvertibleTo < TOptional < T > & & , OptionalType >
| | CConvertibleTo < const TOptional < T > & & , OptionalType >
2022-05-20 15:35:36 +00:00
| | CAssignableFrom < OptionalType & , TOptional < T > & >
| | CAssignableFrom < OptionalType & , const TOptional < T > & >
| | CAssignableFrom < OptionalType & , TOptional < T > & & >
| | CAssignableFrom < OptionalType & , const TOptional < T > & & >
2022-04-13 14:35:30 +00:00
) > { } ;
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
constexpr TOptional ( ) : bIsValid ( false ) { }
2022-03-14 10:03:57 +00:00
constexpr TOptional ( FInvalid ) : TOptional ( ) { }
2022-03-13 15:18:07 +00:00
2022-05-20 15:35:36 +00:00
template < typename . . . Types > requires CConstructibleFrom < OptionalType , Types . . . >
2022-03-14 10:03:57 +00:00
constexpr explicit TOptional ( FInPlace , Types & & . . . Args )
2022-03-09 15:17:54 +00:00
: bIsValid ( true )
{
2022-03-13 15:18:07 +00:00
new ( & Value ) OptionalType ( Forward < Types > ( Args ) . . . ) ;
2022-03-09 15:17:54 +00:00
}
2022-05-20 15:35:36 +00:00
template < typename T = OptionalType > requires CConstructibleFrom < OptionalType , T & & >
2022-05-22 14:52:47 +00:00
& & ( ! CSameAs < TRemoveCVRef < T > , FInPlace > ) & & ( ! CSameAs < TRemoveCVRef < T > , TOptional > )
2022-05-16 15:09:04 +00:00
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-05-16 14:42:17 +00:00
constexpr TOptional ( const TOptional & InValue ) requires CCopyConstructible < OptionalType >
2022-03-13 15:18:07 +00:00
: bIsValid ( InValue . IsValid ( ) )
{
if ( InValue . IsValid ( ) ) new ( & Value ) OptionalType ( InValue . GetValue ( ) ) ;
}
2022-05-16 14:42:17 +00:00
constexpr TOptional ( TOptional & & InValue ) requires CMoveConstructible < OptionalType >
2022-03-13 15:18:07 +00:00
: bIsValid ( InValue . IsValid ( ) )
{
if ( InValue . IsValid ( ) ) new ( & Value ) OptionalType ( MoveTemp ( InValue . GetValue ( ) ) ) ;
}
2022-03-09 15:17:54 +00:00
2022-05-20 15:35:36 +00:00
template < typename T = OptionalType > requires CConstructibleFrom < OptionalType , const T & > & & TAllowUnwrapping < T > : : Value
2022-05-16 15:09:04 +00:00
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-03-13 15:18:07 +00:00
if ( InValue . IsValid ( ) ) new ( & Value ) OptionalType ( InValue . GetValue ( ) ) ;
2022-03-09 15:17:54 +00:00
}
2022-05-20 15:35:36 +00:00
template < typename T = OptionalType > requires CConstructibleFrom < OptionalType , T & & > & & TAllowUnwrapping < T > : : Value
2022-05-16 15:09:04 +00:00
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-03-13 15:18:07 +00:00
if ( InValue . IsValid ( ) ) new ( & Value ) OptionalType ( MoveTemp ( InValue . GetValue ( ) ) ) ;
2022-03-09 15:17:54 +00:00
}
constexpr ~ TOptional ( )
{
2022-05-16 14:42:17 +00:00
if constexpr ( ! CTriviallyDestructible < OptionalType > ) Reset ( ) ;
2022-03-09 15:17:54 +00:00
}
2022-05-16 14:42:17 +00:00
constexpr TOptional & operator = ( const TOptional & InValue ) requires CCopyConstructible < OptionalType > & & CCopyAssignable < 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-03-13 15:18:07 +00:00
new ( & Value ) OptionalType ( InValue . GetValue ( ) ) ;
2022-03-09 15:17:54 +00:00
bIsValid = true ;
}
return * this ;
}
2022-05-16 14:42:17 +00:00
constexpr TOptional & operator = ( TOptional & & InValue ) requires CMoveConstructible < OptionalType > & & CMoveAssignable < 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-03-13 15:18:07 +00:00
new ( & Value ) OptionalType ( MoveTemp ( InValue . GetValue ( ) ) ) ;
2022-03-09 15:17:54 +00:00
bIsValid = true ;
}
return * this ;
}
2022-06-16 15:37:29 +00:00
template < typename T = OptionalType > requires CConstructibleFrom < OptionalType , const T & >
& & CAssignableFrom < OptionalType & , const T & > & & TAllowUnwrapping < T > : : Value
2022-03-13 15:18:07 +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
{
new ( & Value ) OptionalType ( InValue . GetValue ( ) ) ;
bIsValid = true ;
}
2022-03-09 15:17:54 +00:00
return * this ;
}
2022-06-16 15:37:29 +00:00
template < typename T = OptionalType > requires CConstructibleFrom < OptionalType , T & & >
& & CAssignableFrom < OptionalType & , T & & > & & TAllowUnwrapping < T > : : Value
2022-03-13 15:18:07 +00:00
constexpr TOptional & operator = ( TOptional < T > & & InValue )
{
if ( ! InValue . IsValid ( ) )
{
Reset ( ) ;
return * this ;
}
if ( IsValid ( ) ) GetValue ( ) = MoveTemp ( InValue . GetValue ( ) ) ;
else
{
new ( & Value ) OptionalType ( MoveTemp ( InValue . GetValue ( ) ) ) ;
bIsValid = true ;
}
return * this ;
}
2022-05-20 15:35:36 +00:00
template < typename T = OptionalType > requires CConstructibleFrom < OptionalType , T & & > & & CAssignableFrom < OptionalType & , T & & >
2022-03-13 15:18:07 +00:00
constexpr TOptional & operator = ( T & & InValue )
{
if ( IsValid ( ) ) GetValue ( ) = Forward < T > ( InValue ) ;
else
{
new ( & Value ) OptionalType ( Forward < T > ( InValue ) ) ;
bIsValid = true ;
}
return * this ;
}
2022-03-09 15:17:54 +00:00
2022-05-20 15:35:36 +00:00
template < typename . . . ArgTypes > requires CConstructibleFrom < OptionalType , ArgTypes . . . >
2022-04-03 14:55:17 +00:00
constexpr OptionalType & Emplace ( ArgTypes & & . . . Args )
2022-03-09 15:17:54 +00:00
{
Reset ( ) ;
2022-04-03 14:55:17 +00:00
OptionalType * Result = new ( & Value ) OptionalType ( Forward < ArgTypes > ( Args ) . . . ) ;
2022-03-09 15:17:54 +00:00
bIsValid = true ;
return * Result ;
}
constexpr bool IsValid ( ) const { return bIsValid ; }
constexpr explicit operator bool ( ) const { return bIsValid ; }
2022-03-23 09:49:30 +00:00
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 < const 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 ( * reinterpret_cast < const OptionalType * > ( & Value ) ) ; }
2022-03-09 15:17:54 +00:00
2022-03-13 15:18:07 +00:00
constexpr const OptionalType * operator - > ( ) const { return & GetValue ( ) ; }
constexpr OptionalType * operator - > ( ) { return & GetValue ( ) ; }
2022-03-09 15:17:54 +00:00
2022-03-13 15:18:07 +00:00
constexpr OptionalType & operator * ( ) & { return GetValue ( ) ; }
constexpr OptionalType & & operator * ( ) & & { return GetValue ( ) ; }
constexpr const OptionalType & operator * ( ) const & { return GetValue ( ) ; }
constexpr const OptionalType & & operator * ( ) const & & { return GetValue ( ) ; }
2022-03-09 15:17:54 +00:00
2022-03-23 09:49:30 +00:00
constexpr OptionalType & Get ( OptionalType & DefaultValue ) & { return IsValid ( ) ? GetValue ( ) : DefaultValue ; }
constexpr const OptionalType & Get ( const OptionalType & DefaultValue ) const & { return IsValid ( ) ? GetValue ( ) : DefaultValue ; }
2022-03-09 15:17:54 +00:00
constexpr void Reset ( )
{
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-04-22 14:28:44 +00:00
constexpr size_t GetTypeHash ( ) const requires CHashable < OptionalType >
{
if ( ! IsValid ( ) ) return 2824517378 ;
return NAMESPACE_REDCRAFT : : GetTypeHash ( GetValue ( ) ) ;
}
2022-05-20 15:35:36 +00:00
template < typename T > requires CMoveConstructible < OptionalType > & & CSwappable < OptionalType >
2022-04-14 14:41:22 +00:00
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 ( ) ) ;
}
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-05-21 14:39:22 +00:00
template < typename T , typename U > requires CWeaklyEqualityComparable < T , U >
2022-03-09 15:17:54 +00:00
constexpr bool operator = = ( const TOptional < T > & LHS , const TOptional < U > & RHS )
{
2022-03-13 15:18:07 +00:00
if ( LHS . IsValid ( ) ! = RHS . IsValid ( ) ) return false ;
2022-03-09 15:17:54 +00:00
if ( LHS . IsValid ( ) = = false ) return true ;
return * LHS = = * RHS ;
}
2022-05-21 14:39:22 +00:00
template < typename T , typename U > requires CSynthThreeWayComparable < T , U >
2022-05-03 10:22:41 +00:00
constexpr partial_ordering operator < = > ( const TOptional < T > & LHS , const TOptional < U > & RHS )
{
if ( LHS . IsValid ( ) ! = RHS . IsValid ( ) ) return partial_ordering : : unordered ;
if ( LHS . IsValid ( ) = = false ) return partial_ordering : : equivalent ;
return SynthThreeWayCompare ( * LHS , * RHS ) ;
}
2022-05-21 14:39:22 +00:00
template < typename T , typename U > requires CWeaklyEqualityComparable < T , U >
2022-03-13 15:18:07 +00:00
constexpr bool operator = = ( const TOptional < T > & LHS , const U & RHS )
{
return LHS . IsValid ( ) ? * LHS = = RHS : false ;
}
2022-03-22 03:12:05 +00:00
template < typename T >
constexpr bool operator = = ( const TOptional < T > & LHS , FInvalid )
{
return ! LHS . IsValid ( ) ;
}
2022-05-16 14:42:17 +00:00
template < typename T > requires CDestructible < T >
2022-05-22 14:52:47 +00:00
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-05-20 15:35:36 +00:00
template < typename T > requires CDestructible < T > & & CConstructibleFrom < T , T & & >
2022-05-15 14:56:53 +00:00
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-05-20 15:35:36 +00:00
template < typename T , typename . . . Types > requires CDestructible < T > & & CConstructibleFrom < T , Types . . . >
2022-03-09 15:17:54 +00:00
constexpr TOptional < T > MakeOptional ( Types & & . . . Args )
{
return TOptional < T > ( InPlace , Forward < T > ( Args ) . . . ) ;
}
2022-05-20 15:35:36 +00:00
NAMESPACE_PRIVATE_BEGIN
2022-03-31 09:36:48 +00:00
template < typename T > struct TIsTOptional : FFalse { } ;
template < typename T > struct TIsTOptional < TOptional < T > > : FTrue { } ;
2022-05-20 15:35:36 +00:00
NAMESPACE_PRIVATE_END
template < typename T > concept CTOptional = NAMESPACE_PRIVATE : : TIsTOptional < T > : : Value ;
2022-03-09 15:17:54 +00:00
NAMESPACE_MODULE_END ( Utility )
NAMESPACE_MODULE_END ( Redcraft )
NAMESPACE_REDCRAFT_END