feat(templates): add initializer_list overloads to the in place constructor and emplace
This commit is contained in:
parent
b0e7c01f13
commit
38ec8f6571
@ -173,6 +173,13 @@ void TestOptional()
|
||||
always_check(TempB <= TempC);
|
||||
always_check(TempA <=> TempB == partial_ordering::unordered);
|
||||
}
|
||||
|
||||
{
|
||||
struct FTest { FTest(initializer_list<int32>, int32) { } };
|
||||
|
||||
TOptional<FTest> Temp(InPlace, { 0, 1, 2 }, 3);
|
||||
Temp.Emplace({ 0, 1, 2 }, 3);
|
||||
}
|
||||
}
|
||||
|
||||
void TestVariant()
|
||||
@ -186,8 +193,8 @@ void TestVariant()
|
||||
// TVariant<int32> TempF(0.0);
|
||||
TVariant<int32> TempG(TempA);
|
||||
TVariant<int32> TempH(TempD);
|
||||
TVariant<int32> TempI(TVariant<int32>(0));
|
||||
TVariant<int32> TempJ(TVariant<int32>(Invalid));
|
||||
TVariant<int32> TempI = TVariant<int32>(0);
|
||||
TVariant<int32> TempJ = TVariant<int32>(Invalid);
|
||||
|
||||
TVariant<int32> TempK, TempL, TempM, TempN;
|
||||
TempK = TempA;
|
||||
@ -423,6 +430,16 @@ void TestVariant()
|
||||
always_check(TempD >= TempC);
|
||||
always_check(TempA <=> TempB == partial_ordering::unordered);
|
||||
}
|
||||
|
||||
{
|
||||
struct FTest { FTest(initializer_list<int32>, int32) { } };
|
||||
|
||||
TVariant<FTest> TempA(InPlaceIndex<0>, { 0, 1, 2 }, 3);
|
||||
TempA.Emplace<0>({ 0, 1, 2 }, 3);
|
||||
|
||||
TVariant<FTest> TempB(InPlaceType<FTest>, { 0, 1, 2 }, 3);
|
||||
TempB.Emplace<FTest>({ 0, 1, 2 }, 3);
|
||||
}
|
||||
}
|
||||
|
||||
void TestAny()
|
||||
@ -630,6 +647,12 @@ void TestAny()
|
||||
TempZ = FTracker();
|
||||
}
|
||||
|
||||
{
|
||||
struct FTest { FTest(initializer_list<int32>, int32) { } };
|
||||
|
||||
FAny Temp(InPlaceType<FTest>, { 0, 1, 2 }, 3);
|
||||
Temp.Emplace<FTest>({ 0, 1, 2 }, 3);
|
||||
}
|
||||
}
|
||||
|
||||
void TestTuple()
|
||||
@ -1235,6 +1258,19 @@ void TestFunction()
|
||||
always_check(NotIdentity(false));
|
||||
}
|
||||
|
||||
{
|
||||
struct FTest
|
||||
{
|
||||
FTest(initializer_list<int32>, int32) { }
|
||||
void operator()() { }
|
||||
};
|
||||
|
||||
TFunction<void()> TempA(InPlaceType<FTest>, { 0, 1, 2 }, 3);
|
||||
TempA.Emplace<FTest>({ 0, 1, 2 }, 3);
|
||||
|
||||
TUniqueFunction<void()> TempB(InPlaceType<FTest>, { 0, 1, 2 }, 3);
|
||||
TempB.Emplace<FTest>({ 0, 1, 2 }, 3);
|
||||
}
|
||||
}
|
||||
|
||||
void TestAtomic()
|
||||
|
@ -106,6 +106,13 @@ public:
|
||||
{
|
||||
EmplaceImpl<T>(Forward<Ts>(Args)...);
|
||||
}
|
||||
|
||||
/** Constructs an object with initial content an object of type TDecay<T>, direct-non-list-initialized from IL, Forward<Ts>(Args).... */
|
||||
template <typename T, typename U, typename... Ts> requires (NAMESPACE_PRIVATE::CFAnyPlaceable<T> && CConstructibleFrom<TDecay<T>, initializer_list<U>, Ts&&...>)
|
||||
FORCEINLINE explicit FAny(TInPlaceType<T>, initializer_list<U> IL, Ts&&... Args)
|
||||
{
|
||||
EmplaceImpl<T>(IL, Forward<Ts>(Args)...);
|
||||
}
|
||||
|
||||
/** Destroys the contained object, if any, as if by a call to Reset(). */
|
||||
FORCEINLINE ~FAny()
|
||||
@ -275,7 +282,7 @@ public:
|
||||
* First destroys the current contained object (if any) by Reset(), then constructs an object of type
|
||||
* TDecay<T>, direct-non-list-initialized from Forward<Ts>(Args)..., as the contained object.
|
||||
*
|
||||
* @param Args - The arguments to be passed to the constructor of the contained object.
|
||||
* @param Args - The arguments to be passed to the constructor of the contained object.
|
||||
*
|
||||
* @return A reference to the new contained object.
|
||||
*/
|
||||
@ -287,6 +294,23 @@ public:
|
||||
return GetValue<TDecay<T>>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the contained object to one of type TDecay<T> constructed from the arguments.
|
||||
* First destroys the current contained object (if any) by Reset(), then constructs an object of type
|
||||
* TDecay<T>, direct-non-list-initialized from IL, Forward<Ts>(Args)..., as the contained object.
|
||||
*
|
||||
* @param IL, Args - The arguments to be passed to the constructor of the contained object.
|
||||
*
|
||||
* @return A reference to the new contained object.
|
||||
*/
|
||||
template <typename T, typename U, typename... Ts> requires (NAMESPACE_PRIVATE::CFAnyPlaceable<T> && CConstructibleFrom<TDecay<T>, initializer_list<U>, Ts&&...>)
|
||||
FORCEINLINE TDecay<T>& Emplace(initializer_list<U> IL, Ts&&... Args)
|
||||
{
|
||||
Destroy();
|
||||
EmplaceImpl<T>(IL, Forward<Ts>(Args)...);
|
||||
return GetValue<TDecay<T>>();
|
||||
}
|
||||
|
||||
/** @return The typeid of the contained value if instance is non-empty, otherwise typeid(void). */
|
||||
NODISCARD FORCEINLINE constexpr const type_info& GetTypeInfo() const { return IsValid() ? GetTypeInfoImpl() : typeid(void); }
|
||||
|
||||
|
@ -506,8 +506,8 @@ protected:
|
||||
FORCEINLINE constexpr void Invalidate() { Storage.Invalidate(); }
|
||||
|
||||
// Make sure you call this function after you have destroyed the held object using Destroy().
|
||||
template <typename T, typename... ArgTypes>
|
||||
FORCEINLINE constexpr TDecay<T>& Emplace(ArgTypes&&... Args)
|
||||
template <typename T, typename... Us>
|
||||
FORCEINLINE constexpr TDecay<T>& Emplace(Us&&... Args)
|
||||
{
|
||||
using DecayedType = TDecay<T>;
|
||||
|
||||
@ -525,7 +525,7 @@ protected:
|
||||
|
||||
Storage.template Emplace<DecayedType>(
|
||||
reinterpret_cast<uintptr>(Callable),
|
||||
Forward<ArgTypes>(Args)...
|
||||
Forward<Us>(Args)...
|
||||
);
|
||||
|
||||
return *reinterpret_cast<DecayedType*>(Storage.GetValuePtr());
|
||||
@ -641,12 +641,24 @@ public:
|
||||
* Constructs an TFunction with initial content an function object of type TDecay<T>,
|
||||
* direct-non-list-initialized from Forward<Ts>(Args)....
|
||||
*/
|
||||
template <typename T, typename... ArgTypes> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
||||
&& CConstructibleFrom<TDecay<T>, ArgTypes...> && CCopyConstructible<TDecay<T>>
|
||||
template <typename T, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
||||
&& CConstructibleFrom<TDecay<T>, Ts...> && CCopyConstructible<TDecay<T>>
|
||||
&& CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
||||
FORCEINLINE explicit TFunction(TInPlaceType<T>, ArgTypes&&... Args)
|
||||
FORCEINLINE explicit TFunction(TInPlaceType<T>, Ts&&... Args)
|
||||
{
|
||||
Impl::template Emplace<T>(Forward<ArgTypes>(Args)...);
|
||||
Impl::template Emplace<T>(Forward<Ts>(Args)...);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an TFunction with initial content an function object of type TDecay<T>,
|
||||
* direct-non-list-initialized from IL, Forward<Ts>(Args)....
|
||||
*/
|
||||
template <typename T, typename U, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
||||
&& CConstructibleFrom<TDecay<T>, initializer_list<U>, Ts...> && CCopyConstructible<TDecay<T>>
|
||||
&& CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
||||
FORCEINLINE explicit TFunction(TInPlaceType<T>, initializer_list<U> IL, Ts&&... Args)
|
||||
{
|
||||
Impl::template Emplace<T>(IL, Forward<Ts>(Args)...);
|
||||
}
|
||||
|
||||
/** Removes any bound callable from the TFunction, restoring it to the default empty state. */
|
||||
@ -670,17 +682,35 @@ public:
|
||||
* First destroys the current function object (if any) by Reset(), then constructs an object of type
|
||||
* TDecay<T>, direct-non-list-initialized from Forward<Ts>(Args)..., as the function object.
|
||||
*
|
||||
* @param Args - The arguments to be passed to the constructor of the function object.
|
||||
* @param Args - The arguments to be passed to the constructor of the function object.
|
||||
*
|
||||
* @return A reference to the new function object.
|
||||
*/
|
||||
template <typename T, typename... ArgTypes> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
||||
&& CConstructibleFrom<TDecay<T>, ArgTypes...> && CCopyConstructible<TDecay<T>>
|
||||
template <typename T, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
||||
&& CConstructibleFrom<TDecay<T>, Ts...> && CCopyConstructible<TDecay<T>>
|
||||
&& CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
||||
FORCEINLINE TDecay<T>& Emplace(ArgTypes&&... Args)
|
||||
FORCEINLINE TDecay<T>& Emplace(Ts&&... Args)
|
||||
{
|
||||
Impl::Destroy();
|
||||
return Impl::template Emplace<T>(Forward<ArgTypes>(Args)...);
|
||||
return Impl::template Emplace<T>(Forward<Ts>(Args)...);
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the function object to one of type TDecay<T> constructed from the arguments.
|
||||
* First destroys the current function object (if any) by Reset(), then constructs an object of type
|
||||
* TDecay<T>, direct-non-list-initialized from IL, Forward<Ts>(Args)..., as the function object.
|
||||
*
|
||||
* @param IL, Args - The arguments to be passed to the constructor of the function object.
|
||||
*
|
||||
* @return A reference to the new function object.
|
||||
*/
|
||||
template <typename T, typename U, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
||||
&& CConstructibleFrom<TDecay<T>, initializer_list<U>, Ts...> && CCopyConstructible<TDecay<T>>
|
||||
&& CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
||||
FORCEINLINE TDecay<T>& Emplace(initializer_list<U> IL, Ts&&... Args)
|
||||
{
|
||||
Impl::Destroy();
|
||||
return Impl::template Emplace<T>(IL, Forward<Ts>(Args)...);
|
||||
}
|
||||
|
||||
/** Removes any bound callable from the TFunction, restoring it to the default empty state. */
|
||||
@ -765,11 +795,22 @@ public:
|
||||
* Constructs an TUniqueFunction with initial content an function object of type TDecay<T>,
|
||||
* direct-non-list-initialized from Forward<Ts>(Args)....
|
||||
*/
|
||||
template <typename T, typename... ArgTypes> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
||||
&& CConstructibleFrom<TDecay<T>, ArgTypes...> && CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
||||
FORCEINLINE explicit TUniqueFunction(TInPlaceType<T>, ArgTypes&&... Args)
|
||||
template <typename T, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
||||
&& CConstructibleFrom<TDecay<T>, Ts...> && CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
||||
FORCEINLINE explicit TUniqueFunction(TInPlaceType<T>, Ts&&... Args)
|
||||
{
|
||||
Impl::template Emplace<T>(Forward<ArgTypes>(Args)...);
|
||||
Impl::template Emplace<T>(Forward<Ts>(Args)...);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an TUniqueFunction with initial content an function object of type TDecay<T>,
|
||||
* direct-non-list-initialized from IL, Forward<Ts>(Args)....
|
||||
*/
|
||||
template <typename T, typename U, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
||||
&& CConstructibleFrom<TDecay<T>, initializer_list<U>, Ts...> && CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
||||
FORCEINLINE explicit TUniqueFunction(TInPlaceType<T>, initializer_list<U> IL, Ts&&... Args)
|
||||
{
|
||||
Impl::template Emplace<T>(IL, Forward<Ts>(Args)...);
|
||||
}
|
||||
|
||||
/** Removes any bound callable from the TUniqueFunction, restoring it to the default empty state. */
|
||||
@ -795,13 +836,31 @@ public:
|
||||
*
|
||||
* @return A reference to the new function object.
|
||||
*/
|
||||
template <typename T, typename... ArgTypes> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
||||
&& CConstructibleFrom<TDecay<T>, ArgTypes...> && CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
||||
FORCEINLINE TDecay<T>& Emplace(ArgTypes&&... Args)
|
||||
template <typename T, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
||||
&& CConstructibleFrom<TDecay<T>, Ts...> && CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
||||
FORCEINLINE TDecay<T>& Emplace(Ts&&... Args)
|
||||
{
|
||||
Impl::Destroy();
|
||||
using DecayedType = TDecay<T>;
|
||||
return Impl::template Emplace<T>(Forward<ArgTypes>(Args)...);
|
||||
return Impl::template Emplace<T>(Forward<Ts>(Args)...);
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the function object to one of type TDecay<T> constructed from the arguments.
|
||||
* First destroys the current function object (if any) by Reset(), then constructs an object of type
|
||||
* TDecay<T>, direct-non-list-initialized from IL, Forward<Ts>(Args)..., as the function object.
|
||||
*
|
||||
* @param IL, Args - The arguments to be passed to the constructor of the function object.
|
||||
*
|
||||
* @return A reference to the new function object.
|
||||
*/
|
||||
template <typename T, typename U, typename... Ts> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
||||
&& CConstructibleFrom<TDecay<T>, initializer_list<U>, Ts...> && CMoveConstructible<TDecay<T>> && CDestructible<TDecay<T>>)
|
||||
FORCEINLINE TDecay<T>& Emplace(initializer_list<U> IL, Ts&&... Args)
|
||||
{
|
||||
Impl::Destroy();
|
||||
using DecayedType = TDecay<T>;
|
||||
return Impl::template Emplace<T>(IL, Forward<Ts>(Args)...);
|
||||
}
|
||||
|
||||
/** Removes any bound callable from the TUniqueFunction, restoring it to the default empty state. */
|
||||
|
@ -66,6 +66,14 @@ public:
|
||||
{
|
||||
new (&Value) OptionalType(Forward<Ts>(Args)...);
|
||||
}
|
||||
|
||||
/** Constructs an object with initial content an object, direct-non-list-initialized from IL, Forward<Ts>(Args).... */
|
||||
template <typename U, typename... Ts> requires (CConstructibleFrom<OptionalType, initializer_list<U>, Ts...>)
|
||||
FORCEINLINE constexpr explicit TOptional(FInPlace, initializer_list<U> IL, Ts&&... Args)
|
||||
: bIsValid(true)
|
||||
{
|
||||
new (&Value) OptionalType(IL, Forward<Ts>(Args)...);
|
||||
}
|
||||
|
||||
/** Copies content of other into a new instance. */
|
||||
FORCEINLINE constexpr TOptional(const TOptional& InValue) requires (CTriviallyCopyConstructible<OptionalType>) = default;
|
||||
@ -258,16 +266,36 @@ public:
|
||||
* First destroys the current contained object (if any) by Reset(),
|
||||
* then constructs an object, direct-non-list-initialized from Forward<Ts>(Args)..., as the contained object.
|
||||
*
|
||||
* @param Args - The arguments to be passed to the constructor of the object.
|
||||
* @param Args - The arguments to be passed to the constructor of the object.
|
||||
*
|
||||
* @return A reference to the new object.
|
||||
*/
|
||||
template <typename... ArgTypes> requires (CConstructibleFrom<OptionalType, ArgTypes...>)
|
||||
FORCEINLINE constexpr OptionalType& Emplace(ArgTypes&&... Args)
|
||||
template <typename... Ts> requires (CConstructibleFrom<OptionalType, Ts...>)
|
||||
FORCEINLINE constexpr OptionalType& Emplace(Ts&&... Args)
|
||||
{
|
||||
Reset();
|
||||
|
||||
OptionalType* Result = new (&Value) OptionalType(Forward<ArgTypes>(Args)...);
|
||||
OptionalType* Result = new (&Value) OptionalType(Forward<Ts>(Args)...);
|
||||
bIsValid = true;
|
||||
|
||||
return *Result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the contained object to one constructed from the arguments.
|
||||
* First destroys the current contained object (if any) by Reset(),
|
||||
* then constructs an object, direct-non-list-initialized from IL, Forward<Ts>(Args)..., as the contained object.
|
||||
*
|
||||
* @param IL, Args - The arguments to be passed to the constructor of the object.
|
||||
*
|
||||
* @return A reference to the new object.
|
||||
*/
|
||||
template <typename U, typename... Ts> requires (CConstructibleFrom<OptionalType, initializer_list<U>, Ts...>)
|
||||
FORCEINLINE constexpr OptionalType& Emplace(initializer_list<U> IL, Ts&&... Args)
|
||||
{
|
||||
Reset();
|
||||
|
||||
OptionalType* Result = new (&Value) OptionalType(IL, Forward<Ts>(Args)...);
|
||||
bIsValid = true;
|
||||
|
||||
return *Result;
|
||||
|
@ -142,6 +142,22 @@ public:
|
||||
using SelectedType = TVariantAlternative<I, TVariant<Ts...>>;
|
||||
new (&Value) SelectedType(Forward<Us>(Args)...);
|
||||
}
|
||||
|
||||
/** Constructs a variant with the specified alternative T and initializes the contained value with the arguments IL, Forward<Us>(Args).... */
|
||||
template <typename T, typename U, typename... Us> requires (CConstructibleFrom<T, initializer_list<U>, Us...>)
|
||||
FORCEINLINE constexpr explicit TVariant(TInPlaceType<T>, initializer_list<U> IL, Us&&... Args)
|
||||
: TVariant(InPlaceIndex<TVariantIndex<T, TVariant<Ts...>>>, IL, Forward<Us>(Args)...)
|
||||
{ }
|
||||
|
||||
/** Constructs a variant with the alternative T specified by the index I and initializes the contained value with the arguments IL, Forward<Us>(Args).... */
|
||||
template <size_t I, typename T, typename... Us> requires (I < sizeof...(Ts)
|
||||
&& CConstructibleFrom<TVariantAlternative<I, TVariant<Ts...>>, initializer_list<T>, Us...>)
|
||||
FORCEINLINE constexpr explicit TVariant(TInPlaceIndex<I>, initializer_list<T> IL, Us&&... Args)
|
||||
: TypeIndex(I)
|
||||
{
|
||||
using SelectedType = TVariantAlternative<I, TVariant<Ts...>>;
|
||||
new (&Value) SelectedType(IL, Forward<Us>(Args)...);
|
||||
}
|
||||
|
||||
/** Destroys the contained object, if any, as if by a call to Reset(). */
|
||||
FORCEINLINE constexpr ~TVariant() requires (true && ... && CTriviallyDestructible<Ts>) = default;
|
||||
@ -273,7 +289,7 @@ public:
|
||||
* First, destroys the currently contained value if any.
|
||||
* Then direct-initializes the contained value as if constructing a value of type T with the arguments Forward<Us>(Args)....
|
||||
*
|
||||
* @param Args - The arguments to be passed to the constructor of the contained object.
|
||||
* @param Args - The arguments to be passed to the constructor of the contained object.
|
||||
*
|
||||
* @return A reference to the new contained object.
|
||||
*/
|
||||
@ -289,6 +305,34 @@ public:
|
||||
|
||||
return *Result;
|
||||
}
|
||||
|
||||
/** Equivalent to Emplace<I>(IL, Forward<Us>(Args)...), where I is the zero-based index of T in Types.... */
|
||||
template <typename T, typename U, typename... Us> requires (CConstructibleFrom<T, initializer_list<U>, Us...>)
|
||||
FORCEINLINE constexpr T& Emplace(initializer_list<U> IL, Us&&... Args)
|
||||
{
|
||||
return Emplace<TVariantIndex<T, TVariant<Ts...>>>(IL, Forward<Us>(Args)...);
|
||||
}
|
||||
|
||||
/**
|
||||
* First, destroys the currently contained value if any.
|
||||
* Then direct-initializes the contained value as if constructing a value of type T with the arguments IL, Forward<Us>(Args)....
|
||||
*
|
||||
* @param IL, Args - The arguments to be passed to the constructor of the contained object.
|
||||
*
|
||||
* @return A reference to the new contained object.
|
||||
*/
|
||||
template <size_t I, typename T, typename... Us> requires (I < sizeof...(Ts)
|
||||
&& CConstructibleFrom<TVariantAlternative<I, TVariant<Ts...>>, initializer_list<T>, Us...>)
|
||||
FORCEINLINE constexpr TVariantAlternative<I, TVariant<Ts...>>& Emplace(initializer_list<T> IL, Us&&... Args)
|
||||
{
|
||||
Reset();
|
||||
|
||||
using SelectedType = TVariantAlternative<I, TVariant<Ts...>>;
|
||||
SelectedType* Result = new (&Value) SelectedType(IL, Forward<Us>(Args)...);
|
||||
TypeIndex = I;
|
||||
|
||||
return *Result;
|
||||
}
|
||||
|
||||
/** @return The typeid of the contained value if instance is non-empty, otherwise typeid(void). */
|
||||
NODISCARD FORCEINLINE constexpr const type_info& GetTypeInfo() const { return IsValid() ? *TypeInfos[GetIndex()] : typeid(void); }
|
||||
|
Loading…
Reference in New Issue
Block a user