feat(templates): add initializer_list overloads to the in place constructor and emplace
This commit is contained in:
@ -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); }
|
||||
|
Reference in New Issue
Block a user