2022-02-09 13:46:39 +00:00
# pragma once
# include "CoreTypes.h"
# include "Templates/Invoke.h"
# include "Templates/Utility.h"
2022-05-03 14:20:08 +00:00
# include "Templates/Optional.h"
2022-02-09 13:46:39 +00:00
# include "TypeTraits/TypeTraits.h"
NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN ( Redcraft )
NAMESPACE_MODULE_BEGIN ( Utility )
2022-05-15 15:10:02 +00:00
template < typename ReferencedType > requires ( CObject < ReferencedType > | | CFunction < ReferencedType > )
2022-11-15 11:28:43 +00:00
class TReferenceWrapper
2022-02-09 13:46:39 +00:00
{
public :
2022-05-03 14:20:08 +00:00
using Type = ReferencedType ;
2022-02-09 13:46:39 +00:00
2022-05-16 15:09:04 +00:00
template < typename T = ReferencedType > requires CConvertibleTo < T , ReferencedType & >
2022-05-03 14:20:08 +00:00
constexpr TReferenceWrapper ( T & & Object ) : Pointer ( AddressOf ( Forward < T > ( Object ) ) ) { }
2022-02-09 13:46:39 +00:00
TReferenceWrapper ( const TReferenceWrapper & ) = default ;
2022-05-03 14:20:08 +00:00
2022-05-16 15:09:04 +00:00
template < typename T = ReferencedType > requires CConvertibleTo < T & , ReferencedType & >
2022-05-03 14:20:08 +00:00
constexpr TReferenceWrapper ( const TReferenceWrapper < T > & InValue )
: Pointer ( InValue . Pointer )
{ }
2022-02-09 13:46:39 +00:00
2022-05-03 14:20:08 +00:00
TReferenceWrapper & operator = ( const TReferenceWrapper & ) = default ;
2022-05-16 15:09:04 +00:00
template < typename T = ReferencedType > requires CConvertibleTo < T & , ReferencedType & >
2022-05-03 14:20:08 +00:00
constexpr TReferenceWrapper & operator = ( const TReferenceWrapper < T > & InValue )
{
Pointer = InValue . Pointer ;
return * this ;
}
constexpr operator ReferencedType & ( ) const { return * Pointer ; }
constexpr ReferencedType & Get ( ) const { return * Pointer ; }
2022-02-09 13:46:39 +00:00
template < typename . . . Types >
2022-06-16 15:37:29 +00:00
constexpr TInvokeResult < ReferencedType & , Types . . . > operator ( ) ( Types & & . . . Args ) const
2022-02-09 13:46:39 +00:00
{
return Invoke ( Get ( ) , Forward < Types > ( Args ) . . . ) ;
}
2022-05-03 14:20:08 +00:00
constexpr size_t GetTypeHash ( ) const requires CHashable < ReferencedType >
{
return NAMESPACE_REDCRAFT : : GetTypeHash ( Get ( ) ) ;
}
2022-02-09 13:46:39 +00:00
2022-05-03 14:20:08 +00:00
constexpr void Swap ( TReferenceWrapper & InValue )
{
ReferencedType * Temp = Pointer ;
Pointer = InValue . Pointer ;
InValue . Pointer = Temp ;
}
2022-02-09 13:46:39 +00:00
private :
2022-05-03 14:20:08 +00:00
ReferencedType * Pointer ;
2022-11-15 11:28:43 +00:00
template < typename T > requires ( CObject < T > | | CFunction < T > ) friend class TReferenceWrapper ;
2022-05-03 14:20:08 +00:00
// Optimize TOptional with these hacking
constexpr TReferenceWrapper ( FInvalid ) : Pointer ( nullptr ) { } ;
2022-11-15 11:28:43 +00:00
template < typename T > requires CDestructible < T > friend class TOptional ;
2022-02-09 13:46:39 +00:00
} ;
template < typename T >
TReferenceWrapper ( T & ) - > TReferenceWrapper < T > ;
2022-04-24 15:08:00 +00:00
template < typename T >
void Ref ( const T & & ) = delete ;
template < typename T >
constexpr TReferenceWrapper < T > Ref ( T & InValue )
{
return TReferenceWrapper < T > ( InValue ) ;
}
template < typename T >
constexpr TReferenceWrapper < T > Ref ( TReferenceWrapper < T > InValue )
{
return Ref ( InValue . Get ( ) ) ;
}
template < typename T >
constexpr TReferenceWrapper < const T > Ref ( const T & InValue )
{
return TReferenceWrapper < const T > ( InValue ) ;
}
template < typename T >
constexpr TReferenceWrapper < const T > Ref ( TReferenceWrapper < T > InValue )
{
return Ref ( InValue . Get ( ) ) ;
}
2022-05-20 15:35:36 +00:00
NAMESPACE_PRIVATE_BEGIN
2022-06-16 15:37:29 +00:00
template < typename T > struct TIsTReferenceWrapperImpl : FFalse { } ;
template < typename T > struct TIsTReferenceWrapperImpl < TReferenceWrapper < T > > : FTrue { } ;
template < typename T > struct TUnwrapReferenceImpl { using Type = T ; } ;
template < typename T > struct TUnwrapReferenceImpl < TReferenceWrapper < T > > { using Type = T & ; } ;
template < typename T > struct TUnwrapRefDecayImpl { using Type = typename TUnwrapReferenceImpl < TDecay < T > > : : Type ; } ;
2022-03-31 09:36:48 +00:00
2022-05-20 15:35:36 +00:00
NAMESPACE_PRIVATE_END
2022-06-16 15:37:29 +00:00
template < typename T >
concept CTReferenceWrapper = NAMESPACE_PRIVATE : : TIsTReferenceWrapperImpl < T > : : Value ;
2022-05-20 15:35:36 +00:00
2022-06-16 15:37:29 +00:00
template < typename T >
using TUnwrapReference = typename NAMESPACE_PRIVATE : : TUnwrapReferenceImpl < T > : : Type ;
2022-03-26 11:33:28 +00:00
2022-06-16 15:37:29 +00:00
template < typename T >
using TUnwrapRefDecay = typename NAMESPACE_PRIVATE : : TUnwrapRefDecayImpl < T > : : Type ;
2022-03-26 11:33:28 +00:00
2022-05-03 14:20:08 +00:00
template < typename ReferencedType >
2022-11-15 11:28:43 +00:00
class TOptional < TReferenceWrapper < ReferencedType > >
2022-05-03 14:20:08 +00:00
{
private :
using OptionalType = TReferenceWrapper < ReferencedType > ;
template < typename T >
2022-05-20 15:35:36 +00:00
struct TAllowUnwrapping : TBoolConstant < ! (
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-05-03 14:20:08 +00:00
) > { } ;
public :
using ValueType = OptionalType ;
constexpr TOptional ( ) : Reference ( Invalid ) { }
constexpr TOptional ( FInvalid ) : TOptional ( ) { }
2022-05-20 15:35:36 +00:00
template < typename . . . Types > requires CConstructibleFrom < OptionalType , Types . . . >
2022-05-03 14:20:08 +00:00
constexpr explicit TOptional ( FInPlace , Types & & . . . Args )
: Reference ( Forward < Types > ( Args ) . . . )
{ }
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-05-03 14:20:08 +00:00
: TOptional ( InPlace , Forward < T > ( InValue ) )
{ }
TOptional ( const TOptional & InValue ) = default ;
TOptional ( TOptional & & InValue ) = default ;
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-05-03 14:20:08 +00:00
: Reference ( InValue . Reference )
{ }
~ TOptional ( ) = default ;
TOptional & operator = ( const TOptional & InValue ) = default ;
TOptional & operator = ( TOptional & & InValue ) = default ;
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-05-03 14:20:08 +00:00
constexpr TOptional & operator = ( const TOptional < T > & InValue )
{
Reference = InValue . Reference ;
return * this ;
}
2022-05-20 15:35:36 +00:00
template < typename T = OptionalType > requires CConstructibleFrom < OptionalType , T & & > & & CAssignableFrom < OptionalType & , T & & >
2022-05-03 14:20:08 +00:00
constexpr TOptional & operator = ( T & & InValue )
{
Reference = InValue ;
return * this ;
}
2022-05-20 15:35:36 +00:00
template < typename . . . ArgTypes > requires CConstructibleFrom < OptionalType , ArgTypes . . . >
2022-05-03 14:20:08 +00:00
constexpr OptionalType & Emplace ( ArgTypes & & . . . Args )
{
Reference = TReferenceWrapper < ReferencedType > ( Forward < ArgTypes > ( Args ) . . . ) ;
return Reference ;
}
constexpr bool IsValid ( ) const { return Reference . Pointer ! = nullptr ; }
constexpr explicit operator bool ( ) const { return Reference . Pointer ! = nullptr ; }
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 Reference ; }
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 Reference ; }
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 Reference ; }
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 Reference ; }
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 ( )
{
Reference = Invalid ;
}
constexpr size_t GetTypeHash ( ) const requires CHashable < ReferencedType >
{
if ( ! IsValid ( ) ) return 2824517378 ;
return Reference . GetTypeHash ( ) ;
}
constexpr void Swap ( TOptional & InValue )
{
Reference . Swap ( InValue . Reference ) ;
}
private :
TReferenceWrapper < ReferencedType > Reference ;
2022-11-15 11:28:43 +00:00
template < typename T > requires CDestructible < T > friend class TOptional ;
2022-05-03 14:20:08 +00:00
} ;
2022-02-09 13:46:39 +00:00
NAMESPACE_MODULE_END ( Utility )
NAMESPACE_MODULE_END ( Redcraft )
NAMESPACE_REDCRAFT_END