refactor(templates): trivializes TOptional and TVariant operations for types that use trivially copy/move operations

This commit is contained in:
_Redstone_c_ 2022-11-17 23:17:50 +08:00
parent f96c000b9e
commit a4ac19673f
2 changed files with 36 additions and 12 deletions

View File

@ -53,13 +53,17 @@ public:
: TOptional(InPlace, Forward<T>(InValue)) : TOptional(InPlace, Forward<T>(InValue))
{ } { }
constexpr TOptional(const TOptional& InValue) requires (CCopyConstructible<OptionalType>) constexpr TOptional(const TOptional& InValue) requires (CTriviallyCopyConstructible<OptionalType>) = default;
constexpr TOptional(const TOptional& InValue) requires (CCopyConstructible<OptionalType> && !CTriviallyCopyConstructible<OptionalType>)
: bIsValid(InValue.IsValid()) : bIsValid(InValue.IsValid())
{ {
if (InValue.IsValid()) new(&Value) OptionalType(InValue.GetValue()); if (InValue.IsValid()) new(&Value) OptionalType(InValue.GetValue());
} }
constexpr TOptional(TOptional&& InValue) requires (CMoveConstructible<OptionalType>) constexpr TOptional(TOptional&& InValue) requires (CTriviallyMoveConstructible<OptionalType>) = default;
constexpr TOptional(TOptional&& InValue) requires (CMoveConstructible<OptionalType> && !CTriviallyMoveConstructible<OptionalType>)
: bIsValid(InValue.IsValid()) : bIsValid(InValue.IsValid())
{ {
if (InValue.IsValid()) new(&Value) OptionalType(MoveTemp(InValue.GetValue())); if (InValue.IsValid()) new(&Value) OptionalType(MoveTemp(InValue.GetValue()));
@ -79,12 +83,17 @@ public:
if (InValue.IsValid()) new(&Value) OptionalType(MoveTemp(InValue.GetValue())); if (InValue.IsValid()) new(&Value) OptionalType(MoveTemp(InValue.GetValue()));
} }
constexpr ~TOptional() constexpr ~TOptional() requires (CTriviallyDestructible<OptionalType>) = default;
constexpr ~TOptional() requires (!CTriviallyDestructible<OptionalType>)
{ {
if constexpr (!CTriviallyDestructible<OptionalType>) Reset(); Reset();
} }
constexpr TOptional& operator=(const TOptional& InValue) requires (CCopyConstructible<OptionalType> && CCopyAssignable<OptionalType>) constexpr TOptional& operator=(const TOptional& InValue) requires (CTriviallyCopyConstructible<OptionalType> && CTriviallyCopyAssignable<OptionalType>) = default;
constexpr TOptional& operator=(const TOptional& InValue) requires (CCopyConstructible<OptionalType> && CCopyAssignable<OptionalType>
&& !CTriviallyCopyConstructible<OptionalType> && !CTriviallyCopyAssignable<OptionalType>)
{ {
if (&InValue == this) return *this; if (&InValue == this) return *this;
@ -104,7 +113,10 @@ public:
return *this; return *this;
} }
constexpr TOptional& operator=(TOptional&& InValue) requires (CMoveConstructible<OptionalType> && CMoveAssignable<OptionalType>) constexpr TOptional& operator=(TOptional&& InValue) requires (CTriviallyMoveConstructible<OptionalType> && CTriviallyMoveAssignable<OptionalType>) = default;
constexpr TOptional& operator=(TOptional&& InValue) requires (CMoveConstructible<OptionalType> && CMoveAssignable<OptionalType>
&& !CTriviallyMoveConstructible<OptionalType> && !CTriviallyMoveAssignable<OptionalType>)
{ {
if (&InValue == this) return *this; if (&InValue == this) return *this;

View File

@ -157,13 +157,17 @@ public:
constexpr TVariant(FInvalid) : TVariant() { }; constexpr TVariant(FInvalid) : TVariant() { };
constexpr TVariant(const TVariant& InValue) requires (true && ... && CCopyConstructible<Ts>) constexpr TVariant(const TVariant& InValue) requires (true && ... && CTriviallyCopyConstructible<Ts>) = default;
constexpr TVariant(const TVariant& InValue) requires ((true && ... && CCopyConstructible<Ts>) && !(true && ... && CTriviallyCopyConstructible<Ts>))
: TypeIndex(static_cast<uint8>(InValue.GetIndex())) : TypeIndex(static_cast<uint8>(InValue.GetIndex()))
{ {
if (IsValid()) CopyConstructImpl[InValue.GetIndex()](&Value, &InValue.Value); if (IsValid()) CopyConstructImpl[InValue.GetIndex()](&Value, &InValue.Value);
} }
constexpr TVariant(TVariant&& InValue) requires (true && ... && CMoveConstructible<Ts>) constexpr TVariant(TVariant&& InValue) requires (true && ... && CTriviallyMoveConstructible<Ts>) = default;
constexpr TVariant(TVariant&& InValue) requires ((true && ... && CMoveConstructible<Ts>) && !(true && ... && CTriviallyMoveConstructible<Ts>))
: TypeIndex(static_cast<uint8>(InValue.GetIndex())) : TypeIndex(static_cast<uint8>(InValue.GetIndex()))
{ {
if (IsValid()) MoveConstructImpl[InValue.GetIndex()](&Value, &InValue.Value); if (IsValid()) MoveConstructImpl[InValue.GetIndex()](&Value, &InValue.Value);
@ -189,12 +193,17 @@ public:
constexpr TVariant(T&& InValue) : TVariant(InPlaceType<typename NAMESPACE_PRIVATE::TVariantSelectedType<TRemoveReference<T>, Ts...>::Type>, Forward<T>(InValue)) constexpr TVariant(T&& InValue) : TVariant(InPlaceType<typename NAMESPACE_PRIVATE::TVariantSelectedType<TRemoveReference<T>, Ts...>::Type>, Forward<T>(InValue))
{ } { }
constexpr ~TVariant() constexpr ~TVariant() requires (true && ... && CTriviallyDestructible<Ts>) = default;
constexpr ~TVariant() requires (!(true && ... && CTriviallyDestructible<Ts>))
{ {
if constexpr (!(true && ... && CTriviallyDestructible<Ts>)) Reset(); Reset();
} }
constexpr TVariant& operator=(const TVariant& InValue) requires (true && ... && (CCopyConstructible<Ts> && CCopyAssignable<Ts>)) constexpr TVariant& operator=(const TVariant& InValue) requires (true && ... && (CTriviallyCopyConstructible<Ts> && CTriviallyCopyAssignable<Ts>)) = default;
constexpr TVariant& operator=(const TVariant& InValue) requires ((true && ... && (CCopyConstructible<Ts> && CCopyAssignable<Ts>))
&& !(true && ... && (CTriviallyCopyConstructible<Ts> && CTriviallyCopyAssignable<Ts>)))
{ {
if (&InValue == this) return *this; if (&InValue == this) return *this;
@ -215,7 +224,10 @@ public:
return *this; return *this;
} }
constexpr TVariant& operator=(TVariant&& InValue) requires (true && ... && (CMoveConstructible<Ts> && CMoveAssignable<Ts>)) constexpr TVariant& operator=(TVariant&& InValue) requires (true && ... && (CTriviallyMoveConstructible<Ts> && CTriviallyMoveAssignable<Ts>)) = default;
constexpr TVariant& operator=(TVariant&& InValue) requires ((true && ... && (CMoveConstructible<Ts> && CMoveAssignable<Ts>))
&& !(true && ... && (CTriviallyMoveConstructible<Ts> && CTriviallyMoveAssignable<Ts>)))
{ {
if (&InValue == this) return *this; if (&InValue == this) return *this;