2022-04-07 07:57:02 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "CoreTypes.h"
|
|
|
|
#include "Templates/Any.h"
|
|
|
|
#include "Templates/Tuple.h"
|
|
|
|
#include "Templates/Invoke.h"
|
2022-04-27 14:50:56 +00:00
|
|
|
#include "Memory/Alignment.h"
|
2022-04-08 03:44:36 +00:00
|
|
|
#include "Templates/Utility.h"
|
2022-04-07 07:57:02 +00:00
|
|
|
#include "TypeTraits/TypeTraits.h"
|
|
|
|
#include "Miscellaneous/AssertionMacros.h"
|
|
|
|
|
2022-04-30 15:38:09 +00:00
|
|
|
// NOTE: Disable alignment limit warning
|
|
|
|
#pragma warning(disable : 4359)
|
|
|
|
|
2022-04-07 07:57:02 +00:00
|
|
|
NAMESPACE_REDCRAFT_BEGIN
|
|
|
|
NAMESPACE_MODULE_BEGIN(Redcraft)
|
|
|
|
NAMESPACE_MODULE_BEGIN(Utility)
|
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
inline constexpr size_t FUNCTION_DEFAULT_INLINE_SIZE = ANY_DEFAULT_INLINE_SIZE - sizeof(uintptr);
|
|
|
|
inline constexpr size_t FUNCTION_DEFAULT_INLINE_ALIGNMENT = ANY_DEFAULT_INLINE_ALIGNMENT;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-15 14:56:53 +00:00
|
|
|
template <typename F> requires CFunction<F>
|
2022-05-14 14:52:21 +00:00
|
|
|
struct TFunctionRef;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-15 14:56:53 +00:00
|
|
|
template <typename F, size_t InlineSize, size_t InlineAlignment> requires CFunction<F> && (Memory::IsValidAlignment(InlineAlignment))
|
2022-05-14 14:52:21 +00:00
|
|
|
struct TFunction;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-15 14:56:53 +00:00
|
|
|
template <typename F, size_t InlineSize, size_t InlineAlignment> requires CFunction<F> && (Memory::IsValidAlignment(InlineAlignment))
|
2022-05-14 14:52:21 +00:00
|
|
|
struct TUniqueFunction;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename T> struct TIsTFunctionRef : FFalse { };
|
|
|
|
template <typename F> struct TIsTFunctionRef<TFunctionRef<F>> : FTrue { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename T> struct TIsTFunction : FFalse { };
|
|
|
|
template <typename F, size_t I, size_t J> struct TIsTFunction<TFunction<F, I, J>> : FTrue { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename T> struct TIsTUniqueFunction : FFalse { };
|
|
|
|
template <typename F, size_t I, size_t J> struct TIsTUniqueFunction<TUniqueFunction<F, I, J>> : FTrue { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
NAMESPACE_PRIVATE_BEGIN
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename T>
|
|
|
|
constexpr bool FunctionIsBound(const T& Func)
|
2022-04-07 07:57:02 +00:00
|
|
|
{
|
2022-05-15 15:10:02 +00:00
|
|
|
if constexpr (CPointer<T> || CMemberPointer<T> || TIsTFunctionRef<T>::Value || TIsTFunction<T>::Value || TIsTUniqueFunction<T>::Value)
|
2022-04-07 07:57:02 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
return !!Func;
|
2022-04-07 07:57:02 +00:00
|
|
|
}
|
2022-05-14 14:52:21 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename Signature, typename F> struct TIsInvocableSignature : FFalse { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename Ret, typename... Types, typename F>
|
|
|
|
struct TIsInvocableSignature<Ret(Types...), F>
|
2022-05-16 15:09:04 +00:00
|
|
|
: TBoolConstant<CInvocableResult<Ret, F, Types...> && CInvocableResult<Ret, F&, Types...>>
|
2022-04-07 07:57:02 +00:00
|
|
|
{ };
|
|
|
|
|
2022-05-16 15:09:04 +00:00
|
|
|
template <typename Ret, typename... Types, typename F> struct TIsInvocableSignature<Ret(Types...) & , F> : TBoolConstant<CInvocableResult<Ret, F&, Types...>> { };
|
|
|
|
template <typename Ret, typename... Types, typename F> struct TIsInvocableSignature<Ret(Types...) &&, F> : TBoolConstant<CInvocableResult<Ret, F , Types...>> { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename Ret, typename... Types, typename F>
|
|
|
|
struct TIsInvocableSignature<Ret(Types...) const, F>
|
2022-05-16 15:09:04 +00:00
|
|
|
: TBoolConstant<CInvocableResult<Ret, const F, Types...> && CInvocableResult<Ret, const F&, Types...>>
|
2022-04-07 07:57:02 +00:00
|
|
|
{ };
|
|
|
|
|
2022-05-16 15:09:04 +00:00
|
|
|
template <typename Ret, typename... Types, typename F> struct TIsInvocableSignature<Ret(Types...) const& , F> : TBoolConstant<CInvocableResult<Ret, const F&, Types...>> { };
|
|
|
|
template <typename Ret, typename... Types, typename F> struct TIsInvocableSignature<Ret(Types...) const&&, F> : TBoolConstant<CInvocableResult<Ret, const F , Types...>> { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename F> struct TFunctionInfo;
|
|
|
|
template <typename Ret, typename... Types> struct TFunctionInfo<Ret(Types...) > { using Fn = Ret(Types...); using CVRef = int; };
|
|
|
|
template <typename Ret, typename... Types> struct TFunctionInfo<Ret(Types...) & > { using Fn = Ret(Types...); using CVRef = int&; };
|
|
|
|
template <typename Ret, typename... Types> struct TFunctionInfo<Ret(Types...) && > { using Fn = Ret(Types...); using CVRef = int&&; };
|
|
|
|
template <typename Ret, typename... Types> struct TFunctionInfo<Ret(Types...) const > { using Fn = Ret(Types...); using CVRef = const int; };
|
|
|
|
template <typename Ret, typename... Types> struct TFunctionInfo<Ret(Types...) const& > { using Fn = Ret(Types...); using CVRef = const int&; };
|
|
|
|
template <typename Ret, typename... Types> struct TFunctionInfo<Ret(Types...) const&&> { using Fn = Ret(Types...); using CVRef = const int&&; };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename F, typename CVRef, size_t InlineSize, size_t InlineAlignment, bool bIsRef> struct TFunctionImpl;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename Ret, typename... Types, typename CVRef, size_t InlineSize, size_t InlineAlignment, bool bIsRef>
|
|
|
|
struct alignas(InlineAlignment) TFunctionImpl<Ret(Types...), CVRef, InlineSize, InlineAlignment, bIsRef>
|
|
|
|
{
|
|
|
|
public:
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
using ResultType = Ret;
|
|
|
|
using ArgumentType = TTuple<Types...>;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
TFunctionImpl() = default;
|
|
|
|
TFunctionImpl(const TFunctionImpl&) = default;
|
|
|
|
TFunctionImpl(TFunctionImpl&& InValue) = default;
|
|
|
|
TFunctionImpl& operator=(const TFunctionImpl&) = default;
|
|
|
|
TFunctionImpl& operator=(TFunctionImpl&&) = default;
|
2022-04-07 07:57:02 +00:00
|
|
|
~TFunctionImpl() = default;
|
|
|
|
|
2022-05-16 15:09:04 +00:00
|
|
|
FORCEINLINE ResultType operator()(Types... Args) requires (CSameAs<CVRef, int >) { return CallImpl(Forward<Types>(Args)...); }
|
|
|
|
FORCEINLINE ResultType operator()(Types... Args) & requires (CSameAs<CVRef, int& >) { return CallImpl(Forward<Types>(Args)...); }
|
|
|
|
FORCEINLINE ResultType operator()(Types... Args) && requires (CSameAs<CVRef, int&&>) { return CallImpl(Forward<Types>(Args)...); }
|
|
|
|
FORCEINLINE ResultType operator()(Types... Args) const requires (CSameAs<CVRef, const int >) { return CallImpl(Forward<Types>(Args)...); }
|
|
|
|
FORCEINLINE ResultType operator()(Types... Args) const& requires (CSameAs<CVRef, const int& >) { return CallImpl(Forward<Types>(Args)...); }
|
|
|
|
FORCEINLINE ResultType operator()(Types... Args) const&& requires (CSameAs<CVRef, const int&&>) { return CallImpl(Forward<Types>(Args)...); }
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-04-24 14:42:40 +00:00
|
|
|
constexpr bool IsValid() const { return Callable != nullptr; }
|
|
|
|
constexpr explicit operator bool() const { return Callable != nullptr; }
|
2022-04-08 08:52:17 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE const type_info& TargetType() const requires (!bIsRef) { return IsValid() ? Storage.GetTypeInfo() : typeid(void); };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-16 14:42:17 +00:00
|
|
|
template <typename T> FORCEINLINE T& Target() & requires (!bIsRef) && CDestructible<typename TDecay<T>::Type> { return static_cast< StorageType& >(Storage).template GetValue<T>(); }
|
|
|
|
template <typename T> FORCEINLINE T&& Target() && requires (!bIsRef) && CDestructible<typename TDecay<T>::Type> { return static_cast< StorageType&&>(Storage).template GetValue<T>(); }
|
|
|
|
template <typename T> FORCEINLINE const T& Target() const& requires (!bIsRef) && CDestructible<typename TDecay<T>::Type> { return static_cast<const StorageType& >(Storage).template GetValue<T>(); }
|
|
|
|
template <typename T> FORCEINLINE const T&& Target() const&& requires (!bIsRef) && CDestructible<typename TDecay<T>::Type> { return static_cast<const StorageType&&>(Storage).template GetValue<T>(); }
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
constexpr void Swap(TFunctionImpl& InValue) requires (!bIsRef)
|
2022-04-14 14:41:22 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
using NAMESPACE_REDCRAFT::Swap;
|
|
|
|
|
2022-04-14 14:41:22 +00:00
|
|
|
if (!IsValid() && !InValue.IsValid()) return;
|
|
|
|
|
|
|
|
if (IsValid() && !InValue.IsValid())
|
|
|
|
{
|
|
|
|
InValue = MoveTemp(*this);
|
2022-05-14 14:52:21 +00:00
|
|
|
ResetImpl();
|
2022-04-14 14:41:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (InValue.IsValid() && !IsValid())
|
|
|
|
{
|
|
|
|
*this = MoveTemp(InValue);
|
2022-05-14 14:52:21 +00:00
|
|
|
InValue.ResetImpl();
|
2022-04-14 14:41:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
Swap(Callable, InValue.Callable);
|
|
|
|
Swap(Storage, InValue.Storage);
|
2022-04-14 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 07:57:02 +00:00
|
|
|
private:
|
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
using StorageType = typename TConditional<bIsRef, typename TCopyConst<CVRef, void>::Type*, TAny<InlineSize, 1>>::Type;
|
|
|
|
using StorageRef = typename TConditional<bIsRef, typename TCopyConst<CVRef, void>::Type*, typename TCopyCVRef<CVRef, StorageType>::Type&>::Type;
|
2022-05-01 10:02:26 +00:00
|
|
|
|
2022-04-24 14:42:40 +00:00
|
|
|
using CallFunc = ResultType(*)(StorageRef, Types&&...);
|
2022-04-07 07:57:02 +00:00
|
|
|
|
|
|
|
StorageType Storage;
|
2022-04-24 14:42:40 +00:00
|
|
|
CallFunc Callable;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE ResultType CallImpl(Types&&... Args)
|
|
|
|
{
|
|
|
|
checkf(IsValid(), TEXT("Attempting to call an unbound TFunction!"));
|
|
|
|
return Callable(Storage, Forward<Types>(Args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE ResultType CallImpl(Types&&... Args) const
|
|
|
|
{
|
|
|
|
checkf(IsValid(), TEXT("Attempting to call an unbound TFunction!"));
|
|
|
|
return Callable(Storage, Forward<Types>(Args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2022-05-01 10:02:26 +00:00
|
|
|
template <typename DecayedType, typename... ArgTypes>
|
2022-04-07 07:57:02 +00:00
|
|
|
FORCEINLINE void EmplaceImpl(ArgTypes&&... Args)
|
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
using CallableType = typename TCopyConst<typename TRemoveReference<CVRef>::Type, DecayedType>::Type;
|
2022-05-01 10:02:26 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
if constexpr (bIsRef) Storage = ((reinterpret_cast<StorageType>(AddressOf(Args))), ...);
|
2022-05-01 10:02:26 +00:00
|
|
|
else Storage.template Emplace<DecayedType>(Forward<ArgTypes>(Args)...);
|
2022-04-24 14:42:40 +00:00
|
|
|
|
|
|
|
Callable = [](StorageRef Storage, Types&&... Args) -> ResultType
|
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
using InvokeType = typename TConditional<
|
2022-05-15 15:10:02 +00:00
|
|
|
CReference<CVRef>,
|
2022-05-14 14:52:21 +00:00
|
|
|
typename TCopyCVRef<CVRef, CallableType>::Type,
|
|
|
|
typename TCopyCVRef<CVRef, CallableType>::Type&
|
|
|
|
>::Type;
|
|
|
|
|
|
|
|
const auto GetFunc = [&Storage]() -> InvokeType
|
2022-04-24 14:42:40 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
if constexpr (!bIsRef) return Storage.template GetValue<DecayedType>();
|
|
|
|
else return static_cast<InvokeType>(*reinterpret_cast<CallableType*>(Storage));
|
2022-04-24 14:42:40 +00:00
|
|
|
};
|
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
return InvokeResult<ResultType>(GetFunc(), Forward<Types>(Args)...);
|
2022-04-24 14:42:40 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-07 07:57:02 +00:00
|
|
|
FORCEINLINE void AssignImpl(const TFunctionImpl& InValue)
|
|
|
|
{
|
|
|
|
if (InValue.IsValid())
|
|
|
|
{
|
2022-04-24 14:42:40 +00:00
|
|
|
Callable = InValue.Callable;
|
2022-04-07 07:57:02 +00:00
|
|
|
Storage = InValue.Storage;
|
|
|
|
}
|
2022-05-14 14:52:21 +00:00
|
|
|
else ResetImpl();
|
2022-04-07 07:57:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE void AssignImpl(TFunctionImpl&& InValue)
|
|
|
|
{
|
|
|
|
if (InValue.IsValid())
|
|
|
|
{
|
2022-04-24 14:42:40 +00:00
|
|
|
Callable = InValue.Callable;
|
2022-04-07 07:57:02 +00:00
|
|
|
Storage = MoveTemp(InValue.Storage);
|
2022-05-14 14:52:21 +00:00
|
|
|
InValue.ResetImpl();
|
2022-04-07 07:57:02 +00:00
|
|
|
}
|
2022-05-14 14:52:21 +00:00
|
|
|
else ResetImpl();
|
2022-04-07 07:57:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
constexpr void ResetImpl() { Callable = nullptr; }
|
|
|
|
|
2022-04-07 07:57:02 +00:00
|
|
|
};
|
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
NAMESPACE_PRIVATE_END
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-15 14:56:53 +00:00
|
|
|
template <typename F> requires CFunction<F>
|
2022-05-14 14:52:21 +00:00
|
|
|
struct TFunctionRef
|
|
|
|
: public NAMESPACE_PRIVATE::TFunctionImpl<
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
|
|
|
FUNCTION_DEFAULT_INLINE_SIZE,
|
|
|
|
FUNCTION_DEFAULT_INLINE_ALIGNMENT,
|
|
|
|
true>
|
2022-04-07 07:57:02 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
private:
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
using Super = NAMESPACE_PRIVATE::TFunctionImpl<
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
|
|
|
FUNCTION_DEFAULT_INLINE_SIZE,
|
|
|
|
FUNCTION_DEFAULT_INLINE_ALIGNMENT,
|
|
|
|
true>;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
TFunctionRef() = delete;
|
|
|
|
|
|
|
|
TFunctionRef(const TFunctionRef& InValue) = default;
|
|
|
|
TFunctionRef(TFunctionRef&& InValue) = default;
|
|
|
|
|
|
|
|
TFunctionRef& operator=(const TFunctionRef& InValue) = delete;
|
|
|
|
TFunctionRef& operator=(TFunctionRef&& InValue) = delete;
|
|
|
|
|
|
|
|
template <typename T> requires (!TIsTFunctionRef<typename TDecay<T>::Type>::Value) && (!TIsTInPlaceType<typename TDecay<T>::Type>::Value)
|
|
|
|
&& NAMESPACE_PRIVATE::TIsInvocableSignature<F, typename TDecay<T>::Type>::Value
|
|
|
|
FORCEINLINE TFunctionRef(T&& InValue)
|
|
|
|
{
|
|
|
|
using DecayedType = typename TDecay<T>::Type;
|
|
|
|
checkf(NAMESPACE_PRIVATE::FunctionIsBound(InValue), TEXT("Cannot bind a null/unbound callable to a TFunctionRef"));
|
|
|
|
Super::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
TFunctionRef(const T&&) = delete;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename F, size_t InlineSize = FUNCTION_DEFAULT_INLINE_SIZE, size_t InlineAlignment = FUNCTION_DEFAULT_INLINE_ALIGNMENT>
|
2022-05-15 14:56:53 +00:00
|
|
|
requires CFunction<F> && (Memory::IsValidAlignment(InlineAlignment))
|
2022-05-14 14:52:21 +00:00
|
|
|
struct TFunction
|
|
|
|
: public NAMESPACE_PRIVATE::TFunctionImpl<
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
|
|
|
InlineSize,
|
|
|
|
InlineAlignment,
|
|
|
|
false>
|
2022-04-07 07:57:02 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
using Super = NAMESPACE_PRIVATE::TFunctionImpl<
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
|
|
|
InlineSize,
|
|
|
|
InlineAlignment,
|
|
|
|
false>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
constexpr TFunction(nullptr_t = nullptr) { Super::ResetImpl(); }
|
|
|
|
|
|
|
|
FORCEINLINE TFunction(const TFunction& InValue) = default;
|
|
|
|
FORCEINLINE TFunction(TFunction&& InValue) : Super(MoveTemp(InValue)) { InValue.ResetImpl(); }
|
|
|
|
|
|
|
|
FORCEINLINE TFunction& operator=(const TFunction& InValue)
|
|
|
|
{
|
|
|
|
Super::AssignImpl(InValue);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE TFunction& operator=(TFunction&& InValue)
|
|
|
|
{
|
|
|
|
if (&InValue == this) return *this;
|
|
|
|
Super::AssignImpl(MoveTemp(InValue));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> requires (!TIsTInPlaceType<typename TDecay<T>::Type>::Value)
|
|
|
|
&& (!TIsTFunctionRef<typename TDecay<T>::Type>::Value) && (!TIsTFunction<typename TDecay<T>::Type>::Value) && (!TIsTUniqueFunction<typename TDecay<T>::Type>::Value)
|
2022-05-16 14:42:17 +00:00
|
|
|
&& CConstructible<typename TDecay<T>::Type, T&&> && CCopyConstructible<typename TDecay<T>::Type>
|
2022-05-14 14:52:21 +00:00
|
|
|
&& NAMESPACE_PRIVATE::TIsInvocableSignature<F, typename TDecay<T>::Type>::Value
|
|
|
|
FORCEINLINE TFunction(T&& InValue)
|
|
|
|
{
|
|
|
|
using DecayedType = typename TDecay<T>::Type;
|
|
|
|
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Super::ResetImpl();
|
|
|
|
else Super::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... ArgTypes> requires NAMESPACE_PRIVATE::TIsInvocableSignature<F, typename TDecay<T>::Type>::Value
|
2022-05-16 14:42:17 +00:00
|
|
|
&& CConstructible<typename TDecay<T>::Type, ArgTypes...> && CCopyConstructible<typename TDecay<T>::Type>
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TFunction(TInPlaceType<T>, ArgTypes&&... Args)
|
|
|
|
{
|
|
|
|
using DecayedType = typename TDecay<T>::Type;
|
|
|
|
Super::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr TFunction& operator=(nullptr_t) { Super::ResetImpl(); return *this; }
|
|
|
|
|
|
|
|
template <typename T> requires NAMESPACE_PRIVATE::TIsInvocableSignature<F, typename TDecay<T>::Type>::Value
|
|
|
|
&& (!TIsTFunctionRef<typename TDecay<T>::Type>::Value) && (!TIsTFunction<typename TDecay<T>::Type>::Value) && (!TIsTUniqueFunction<typename TDecay<T>::Type>::Value)
|
2022-05-16 14:42:17 +00:00
|
|
|
&& CConstructible<typename TDecay<T>::Type, T&&> && CCopyConstructible<typename TDecay<T>::Type>
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TFunction& operator=(T&& InValue)
|
|
|
|
{
|
|
|
|
using DecayedType = typename TDecay<T>::Type;
|
|
|
|
|
|
|
|
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Super::ResetImpl();
|
|
|
|
else Super::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... ArgTypes> requires NAMESPACE_PRIVATE::TIsInvocableSignature<F, typename TDecay<T>::Type>::Value
|
2022-05-16 14:42:17 +00:00
|
|
|
&& CConstructible<typename TDecay<T>::Type, ArgTypes...>&& CCopyConstructible<typename TDecay<T>::Type>
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE typename TDecay<T>::Type& Emplace(ArgTypes&&... Args)
|
|
|
|
{
|
|
|
|
using DecayedType = typename TDecay<T>::Type;
|
|
|
|
Super::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
|
|
|
|
return Super::template Target<DecayedType>();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void Reset() { Super::ResetImpl(); }
|
|
|
|
|
2022-04-07 07:57:02 +00:00
|
|
|
};
|
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename F, size_t InlineSize = FUNCTION_DEFAULT_INLINE_SIZE, size_t InlineAlignment = FUNCTION_DEFAULT_INLINE_ALIGNMENT>
|
2022-05-15 14:56:53 +00:00
|
|
|
requires CFunction<F> && (Memory::IsValidAlignment(InlineAlignment))
|
2022-05-14 14:52:21 +00:00
|
|
|
struct TUniqueFunction
|
|
|
|
: public NAMESPACE_PRIVATE::TFunctionImpl<
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
|
|
|
InlineSize,
|
|
|
|
InlineAlignment,
|
|
|
|
false>
|
2022-04-07 07:57:02 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
private:
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
using Super = NAMESPACE_PRIVATE::TFunctionImpl<
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
|
|
|
InlineSize,
|
|
|
|
InlineAlignment,
|
|
|
|
false>;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
public:
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
constexpr TUniqueFunction(nullptr_t = nullptr) { Super::ResetImpl(); }
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TUniqueFunction(const TUniqueFunction& InValue) = delete;
|
|
|
|
TUniqueFunction(TUniqueFunction&& InValue) : Super(MoveTemp(InValue)) { InValue.ResetImpl(); }
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TUniqueFunction& operator=(const TUniqueFunction& InValue) = delete;
|
|
|
|
FORCEINLINE TUniqueFunction& operator=(TUniqueFunction&& InValue)
|
|
|
|
{
|
|
|
|
if (&InValue == this) return *this;
|
|
|
|
Super::AssignImpl(MoveTemp(InValue));
|
|
|
|
return *this;
|
|
|
|
}
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TUniqueFunction(const TFunction<F, InlineSize, InlineAlignment>& InValue)
|
|
|
|
: Super(*reinterpret_cast<const TUniqueFunction*>(&InValue))
|
|
|
|
{ }
|
|
|
|
|
|
|
|
FORCEINLINE TUniqueFunction(TFunction<F, InlineSize, InlineAlignment>&& InValue)
|
|
|
|
: Super(MoveTemp(*reinterpret_cast<const TUniqueFunction*>(&InValue)))
|
|
|
|
{
|
|
|
|
InValue.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE TUniqueFunction& operator=(const TFunction<F, InlineSize, InlineAlignment>& InValue)
|
|
|
|
{
|
|
|
|
Super::AssignImpl(*reinterpret_cast<const TUniqueFunction*>(&InValue));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE TUniqueFunction& operator=(TFunction<F, InlineSize, InlineAlignment>&& InValue)
|
|
|
|
{
|
|
|
|
Super::AssignImpl(MoveTemp(*reinterpret_cast<TUniqueFunction*>(&InValue)));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> requires (!TIsTInPlaceType<typename TDecay<T>::Type>::Value)
|
|
|
|
&& (!TIsTFunctionRef<typename TDecay<T>::Type>::Value) && (!TIsTFunction<typename TDecay<T>::Type>::Value) && (!TIsTUniqueFunction<typename TDecay<T>::Type>::Value)
|
2022-05-16 14:42:17 +00:00
|
|
|
&& CConstructible<typename TDecay<T>::Type, T&&> && CMoveConstructible<typename TDecay<T>::Type>
|
2022-05-14 14:52:21 +00:00
|
|
|
&& NAMESPACE_PRIVATE::TIsInvocableSignature<F, typename TDecay<T>::Type>::Value
|
|
|
|
FORCEINLINE TUniqueFunction(T&& InValue)
|
|
|
|
{
|
|
|
|
using DecayedType = typename TDecay<T>::Type;
|
|
|
|
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Super::ResetImpl();
|
|
|
|
else Super::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... ArgTypes> requires NAMESPACE_PRIVATE::TIsInvocableSignature<F, typename TDecay<T>::Type>::Value
|
2022-05-16 14:42:17 +00:00
|
|
|
&& CConstructible<typename TDecay<T>::Type, ArgTypes...> && CMoveConstructible<typename TDecay<T>::Type>
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TUniqueFunction(TInPlaceType<T>, ArgTypes&&... Args)
|
|
|
|
{
|
|
|
|
using DecayedType = typename TDecay<T>::Type;
|
|
|
|
Super::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr TUniqueFunction& operator=(nullptr_t) { Super::ResetImpl(); return *this; }
|
|
|
|
|
|
|
|
template <typename T> requires NAMESPACE_PRIVATE::TIsInvocableSignature<F, typename TDecay<T>::Type>::Value
|
|
|
|
&& (!TIsTFunctionRef<typename TDecay<T>::Type>::Value) && (!TIsTFunction<typename TDecay<T>::Type>::Value) && (!TIsTUniqueFunction<typename TDecay<T>::Type>::Value)
|
2022-05-16 14:42:17 +00:00
|
|
|
&& CConstructible<typename TDecay<T>::Type, T&&>&& CMoveConstructible<typename TDecay<T>::Type>
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TUniqueFunction& operator=(T&& InValue)
|
|
|
|
{
|
|
|
|
using DecayedType = typename TDecay<T>::Type;
|
|
|
|
|
|
|
|
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Super::ResetImpl();
|
|
|
|
else Super::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... ArgTypes> requires NAMESPACE_PRIVATE::TIsInvocableSignature<F, typename TDecay<T>::Type>::Value
|
2022-05-16 14:42:17 +00:00
|
|
|
&& CConstructible<typename TDecay<T>::Type, ArgTypes...>&& CMoveConstructible<typename TDecay<T>::Type>
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE typename TDecay<T>::Type& Emplace(ArgTypes&&... Args)
|
|
|
|
{
|
|
|
|
using DecayedType = typename TDecay<T>::Type;
|
|
|
|
Super::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
|
|
|
|
return Super::template Target<DecayedType>();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void Reset() { Super::ResetImpl(); }
|
|
|
|
|
|
|
|
};
|
2022-04-07 07:57:02 +00:00
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
constexpr bool operator==(const TFunctionRef<F>& LHS, nullptr_t)
|
|
|
|
{
|
|
|
|
return !LHS;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
constexpr bool operator==(const TFunction<F>& LHS, nullptr_t)
|
|
|
|
{
|
|
|
|
return !LHS;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
constexpr bool operator==(const TUniqueFunction<F>& LHS, nullptr_t)
|
|
|
|
{
|
|
|
|
return !LHS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static_assert(sizeof(TFunction<void()>) == 64, "The byte size of TFunction is unexpected");
|
|
|
|
static_assert(sizeof(TUniqueFunction<void()>) == 64, "The byte size of TUniqueFunction is unexpected");
|
|
|
|
|
2022-04-08 03:44:36 +00:00
|
|
|
NAMESPACE_PRIVATE_BEGIN
|
|
|
|
|
|
|
|
template <typename F>
|
2022-05-14 14:52:21 +00:00
|
|
|
struct TNotFunction
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
F Storage;
|
2022-04-08 03:44:36 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
TNotFunction(const TNotFunction&) = default;
|
|
|
|
TNotFunction(TNotFunction&&) = default;
|
2022-04-08 03:44:36 +00:00
|
|
|
|
|
|
|
template <typename InF>
|
2022-05-14 14:52:21 +00:00
|
|
|
constexpr TNotFunction(InF&& InFunc) : Storage(Forward<InF>(InFunc)) { }
|
2022-04-08 03:44:36 +00:00
|
|
|
|
2022-05-16 15:09:04 +00:00
|
|
|
template <typename... Types> requires CInvocable<F&, Types&&...>
|
2022-05-14 14:52:21 +00:00
|
|
|
constexpr auto operator()(Types&&... Args) &
|
|
|
|
-> decltype(!Invoke(Storage, Forward<Types>(Args)...))
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
return !Invoke(Storage, Forward<Types>(Args)...);
|
2022-04-08 03:44:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 15:09:04 +00:00
|
|
|
template <typename... Types> requires CInvocable<F&&, Types&&...>
|
2022-05-14 14:52:21 +00:00
|
|
|
constexpr auto operator()(Types&&... Args) &&
|
|
|
|
-> decltype(!Invoke(MoveTemp(Storage), Forward<Types>(Args)...))
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
return !Invoke(MoveTemp(Storage), Forward<Types>(Args)...);
|
2022-04-08 03:44:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 15:09:04 +00:00
|
|
|
template <typename... Types> requires CInvocable<const F&, Types&&...>
|
2022-05-14 14:52:21 +00:00
|
|
|
constexpr auto operator()(Types&&... Args) const&
|
|
|
|
-> decltype(!Invoke(Storage, Forward<Types>(Args)...))
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
return !Invoke(Storage, Forward<Types>(Args)...);
|
2022-04-08 03:44:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 15:09:04 +00:00
|
|
|
template <typename... Types> requires CInvocable<const F&&, Types&&...>
|
2022-05-14 14:52:21 +00:00
|
|
|
constexpr auto operator()(Types&&... Args) const&&
|
|
|
|
-> decltype(!Invoke(MoveTemp(Storage), Forward<Types>(Args)...))
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
return !Invoke(MoveTemp(Storage), Forward<Types>(Args)...);
|
2022-04-08 03:44:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NAMESPACE_PRIVATE_END
|
|
|
|
|
|
|
|
template <typename F>
|
2022-05-14 14:52:21 +00:00
|
|
|
constexpr NAMESPACE_PRIVATE::TNotFunction<typename TDecay<F>::Type> NotFn(F&& Func)
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
return NAMESPACE_PRIVATE::TNotFunction<typename TDecay<F>::Type>(Forward<F>(Func));
|
2022-04-08 03:44:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 07:57:02 +00:00
|
|
|
NAMESPACE_MODULE_END(Utility)
|
|
|
|
NAMESPACE_MODULE_END(Redcraft)
|
|
|
|
NAMESPACE_REDCRAFT_END
|