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"
|
|
|
|
|
|
|
|
NAMESPACE_REDCRAFT_BEGIN
|
|
|
|
NAMESPACE_MODULE_BEGIN(Redcraft)
|
|
|
|
NAMESPACE_MODULE_BEGIN(Utility)
|
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
template <CFunction F>
|
|
|
|
class TFunctionRef;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
template <CFunction F>
|
|
|
|
class TFunction;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
template <CFunction F>
|
|
|
|
class TUniqueFunction;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-20 15:35:36 +00:00
|
|
|
NAMESPACE_PRIVATE_BEGIN
|
|
|
|
|
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-11-13 14:21:00 +00:00
|
|
|
template <typename T> struct TIsTFunction : FFalse { };
|
|
|
|
template <typename F> struct TIsTFunction<TFunction<F>> : FTrue { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-13 14:21:00 +00:00
|
|
|
template <typename T> struct TIsTUniqueFunction : FFalse { };
|
|
|
|
template <typename F> struct TIsTUniqueFunction<TUniqueFunction<F>> : FTrue { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-20 15:35:36 +00:00
|
|
|
NAMESPACE_PRIVATE_END
|
|
|
|
|
2022-11-17 12:57:54 +00:00
|
|
|
template <typename T> concept CTFunctionRef = NAMESPACE_PRIVATE::TIsTFunctionRef<TRemoveCV<T>>::Value;
|
|
|
|
template <typename T> concept CTFunction = NAMESPACE_PRIVATE::TIsTFunction<TRemoveCV<T>>::Value;
|
|
|
|
template <typename T> concept CTUniqueFunction = NAMESPACE_PRIVATE::TIsTUniqueFunction<TRemoveCV<T>>::Value;
|
2022-05-20 15:35:36 +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-20 15:35:36 +00:00
|
|
|
if constexpr (CPointer<T> || CMemberPointer<T> || CTFunctionRef<T> || CTFunction<T> || CTUniqueFunction<T>)
|
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-11-16 11:13:37 +00:00
|
|
|
template <typename Ret, typename... Ts, typename F>
|
|
|
|
struct TIsInvocableSignature<Ret(Ts...), F>
|
|
|
|
: TBoolConstant<CInvocableResult<Ret, F, Ts...> && CInvocableResult<Ret, F&, Ts...>>
|
2022-04-07 07:57:02 +00:00
|
|
|
{ };
|
|
|
|
|
2022-11-16 11:13:37 +00:00
|
|
|
template <typename Ret, typename... Ts, typename F> struct TIsInvocableSignature<Ret(Ts...) & , F> : TBoolConstant<CInvocableResult<Ret, F&, Ts...>> { };
|
|
|
|
template <typename Ret, typename... Ts, typename F> struct TIsInvocableSignature<Ret(Ts...) &&, F> : TBoolConstant<CInvocableResult<Ret, F , Ts...>> { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-16 11:13:37 +00:00
|
|
|
template <typename Ret, typename... Ts, typename F>
|
|
|
|
struct TIsInvocableSignature<Ret(Ts...) const, F>
|
|
|
|
: TBoolConstant<CInvocableResult<Ret, const F, Ts...> && CInvocableResult<Ret, const F&, Ts...>>
|
2022-04-07 07:57:02 +00:00
|
|
|
{ };
|
|
|
|
|
2022-11-16 11:13:37 +00:00
|
|
|
template <typename Ret, typename... Ts, typename F> struct TIsInvocableSignature<Ret(Ts...) const& , F> : TBoolConstant<CInvocableResult<Ret, const F&, Ts...>> { };
|
|
|
|
template <typename Ret, typename... Ts, typename F> struct TIsInvocableSignature<Ret(Ts...) const&&, F> : TBoolConstant<CInvocableResult<Ret, const F , Ts...>> { };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
template <typename F> struct TFunctionInfo;
|
2022-11-16 11:13:37 +00:00
|
|
|
template <typename Ret, typename... Ts> struct TFunctionInfo<Ret(Ts...) > { using Fn = Ret(Ts...); using CVRef = int; };
|
|
|
|
template <typename Ret, typename... Ts> struct TFunctionInfo<Ret(Ts...) & > { using Fn = Ret(Ts...); using CVRef = int&; };
|
|
|
|
template <typename Ret, typename... Ts> struct TFunctionInfo<Ret(Ts...) && > { using Fn = Ret(Ts...); using CVRef = int&&; };
|
|
|
|
template <typename Ret, typename... Ts> struct TFunctionInfo<Ret(Ts...) const > { using Fn = Ret(Ts...); using CVRef = const int; };
|
|
|
|
template <typename Ret, typename... Ts> struct TFunctionInfo<Ret(Ts...) const& > { using Fn = Ret(Ts...); using CVRef = const int&; };
|
|
|
|
template <typename Ret, typename... Ts> struct TFunctionInfo<Ret(Ts...) const&&> { using Fn = Ret(Ts...); using CVRef = const int&&; };
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
template <typename F, typename CVRef, bool bIsRef> class TFunctionImpl;
|
2022-11-13 14:21:00 +00:00
|
|
|
|
2022-11-16 11:13:37 +00:00
|
|
|
template <typename Ret, typename... Ts, typename CVRef, bool bIsRef>
|
|
|
|
class TFunctionImpl<Ret(Ts...), CVRef, bIsRef>
|
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
|
|
|
using ResultType = Ret;
|
2022-11-16 11:13:37 +00:00
|
|
|
using ArgumentType = TTuple<Ts...>;
|
2022-11-13 14:21:00 +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-11-16 11:13:37 +00:00
|
|
|
FORCEINLINE ResultType operator()(Ts... Args) requires (CSameAs<CVRef, int >) { return CallImpl(Forward<Ts>(Args)...); }
|
|
|
|
FORCEINLINE ResultType operator()(Ts... Args) & requires (CSameAs<CVRef, int& >) { return CallImpl(Forward<Ts>(Args)...); }
|
|
|
|
FORCEINLINE ResultType operator()(Ts... Args) && requires (CSameAs<CVRef, int&&>) { return CallImpl(Forward<Ts>(Args)...); }
|
|
|
|
FORCEINLINE ResultType operator()(Ts... Args) const requires (CSameAs<CVRef, const int >) { return CallImpl(Forward<Ts>(Args)...); }
|
|
|
|
FORCEINLINE ResultType operator()(Ts... Args) const& requires (CSameAs<CVRef, const int& >) { return CallImpl(Forward<Ts>(Args)...); }
|
|
|
|
FORCEINLINE ResultType operator()(Ts... Args) const&& requires (CSameAs<CVRef, const int&&>) { return CallImpl(Forward<Ts>(Args)...); }
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-13 14:21:00 +00:00
|
|
|
constexpr bool IsValid() const { return GetCallableImpl() != nullptr; }
|
|
|
|
constexpr explicit operator bool() const { return GetCallableImpl() != 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-11-16 14:03:54 +00:00
|
|
|
template <typename T> FORCEINLINE T& Target() & requires (!bIsRef && CDestructible<TDecay<T>>) { return static_cast< StorageType& >(Storage).template GetValue<T>(); }
|
|
|
|
template <typename T> FORCEINLINE T&& Target() && requires (!bIsRef && CDestructible<TDecay<T>>) { return static_cast< StorageType&&>(Storage).template GetValue<T>(); }
|
|
|
|
template <typename T> FORCEINLINE const T& Target() const& requires (!bIsRef && CDestructible<TDecay<T>>) { return static_cast<const StorageType& >(Storage).template GetValue<T>(); }
|
|
|
|
template <typename T> FORCEINLINE const T&& Target() const&& requires (!bIsRef && CDestructible<TDecay<T>>) { 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(Storage, InValue.Storage);
|
2022-04-14 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 07:57:02 +00:00
|
|
|
private:
|
|
|
|
|
2022-11-13 14:21:00 +00:00
|
|
|
using StoragePtrType = TCopyConst<CVRef, void>*;
|
2022-11-16 11:13:37 +00:00
|
|
|
using CallableType = ResultType(*)(StoragePtrType, Ts&&...);
|
2022-05-01 10:02:26 +00:00
|
|
|
|
2022-11-13 14:21:00 +00:00
|
|
|
struct FunctionRefStorage
|
|
|
|
{
|
|
|
|
StoragePtrType Ptr;
|
|
|
|
CallableType Callable;
|
|
|
|
};
|
2022-11-15 11:28:43 +00:00
|
|
|
|
|
|
|
template <typename CallableType>
|
2022-11-18 15:17:54 +00:00
|
|
|
struct alignas(16) FFunctionStorage : FSingleton
|
2022-11-15 11:28:43 +00:00
|
|
|
{
|
|
|
|
//~ Begin CAnyCustomStorage Interface
|
|
|
|
inline static constexpr size_t InlineSize = 64 - sizeof(uintptr) - sizeof(CallableType);
|
|
|
|
inline static constexpr size_t InlineAlignment = 16;
|
|
|
|
constexpr void* InlineAllocation() { return &InlineAllocationImpl; }
|
|
|
|
constexpr const void* InlineAllocation() const { return &InlineAllocationImpl; }
|
|
|
|
constexpr void*& HeapAllocation() { return HeapAllocationImpl; }
|
|
|
|
constexpr void* HeapAllocation() const { return HeapAllocationImpl; }
|
|
|
|
constexpr uintptr& TypeInfo() { return TypeInfoImpl; }
|
|
|
|
constexpr uintptr TypeInfo() const { return TypeInfoImpl; }
|
|
|
|
constexpr void CopyCustom(const FFunctionStorage& InValue) { Callable = InValue.Callable; }
|
|
|
|
constexpr void MoveCustom( FFunctionStorage&& InValue) { Callable = InValue.Callable; }
|
|
|
|
//~ End CAnyCustomStorage Interface
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
uint8 InlineAllocationImpl[InlineSize];
|
|
|
|
void* HeapAllocationImpl;
|
|
|
|
};
|
|
|
|
|
|
|
|
uintptr TypeInfoImpl;
|
|
|
|
|
|
|
|
CallableType Callable;
|
|
|
|
|
|
|
|
};
|
2022-11-13 14:21:00 +00:00
|
|
|
|
|
|
|
using FunctionStorage = TAny<FFunctionStorage<CallableType>>;
|
|
|
|
using StorageType = TConditional<bIsRef, FunctionRefStorage, FunctionStorage>;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
|
|
|
StorageType Storage;
|
2022-11-13 14:21:00 +00:00
|
|
|
|
|
|
|
FORCEINLINE CallableType& GetCallableImpl()
|
|
|
|
{
|
|
|
|
if constexpr (bIsRef) return Storage.Callable;
|
|
|
|
else return Storage.GetCustomStorage().Callable;
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE CallableType GetCallableImpl() const
|
|
|
|
{
|
|
|
|
if constexpr (bIsRef) return Storage.Callable;
|
|
|
|
else return Storage.GetCustomStorage().Callable;
|
|
|
|
}
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-16 11:13:37 +00:00
|
|
|
FORCEINLINE ResultType CallImpl(Ts&&... Args)
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
|
|
|
checkf(IsValid(), TEXT("Attempting to call an unbound TFunction!"));
|
2022-11-16 11:13:37 +00:00
|
|
|
if constexpr (bIsRef) return GetCallableImpl()(Storage.Ptr, Forward<Ts>(Args)...);
|
|
|
|
else return GetCallableImpl()(&Storage, Forward<Ts>(Args)...);
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 11:13:37 +00:00
|
|
|
FORCEINLINE ResultType CallImpl(Ts&&... Args) const
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
|
|
|
checkf(IsValid(), TEXT("Attempting to call an unbound TFunction!"));
|
2022-11-16 11:13:37 +00:00
|
|
|
if constexpr (bIsRef) return GetCallableImpl()(Storage.Ptr, Forward<Ts>(Args)...);
|
|
|
|
else return GetCallableImpl()(&Storage, Forward<Ts>(Args)...);
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
protected: // These functions should not be used by user-defined class
|
2022-05-14 14:52:21 +00:00
|
|
|
|
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-11-13 14:21:00 +00:00
|
|
|
using FuncType = TCopyConst<TRemoveReference<CVRef>, DecayedType>;
|
2022-05-01 10:02:26 +00:00
|
|
|
|
2022-11-13 14:21:00 +00:00
|
|
|
if constexpr (bIsRef) Storage.Ptr = (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
|
|
|
|
2022-11-16 11:13:37 +00:00
|
|
|
GetCallableImpl() = [](StoragePtrType Storage, Ts&&... Args) -> ResultType
|
2022-04-24 14:42:40 +00:00
|
|
|
{
|
2022-05-22 14:52:47 +00:00
|
|
|
using InvokeType = TConditional<
|
2022-05-15 15:10:02 +00:00
|
|
|
CReference<CVRef>,
|
2022-11-13 14:21:00 +00:00
|
|
|
TCopyCVRef<CVRef, FuncType>,
|
|
|
|
TCopyCVRef<CVRef, FuncType>&
|
2022-05-22 14:52:47 +00:00
|
|
|
>;
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-11-13 14:21:00 +00:00
|
|
|
const auto GetFunc = [Storage]() -> InvokeType
|
2022-04-24 14:42:40 +00:00
|
|
|
{
|
2022-11-13 14:21:00 +00:00
|
|
|
if constexpr (!bIsRef) return static_cast<TCopyConst<CVRef, FunctionStorage>*>(Storage)->template GetValue<DecayedType>();
|
|
|
|
else return static_cast<InvokeType>(*reinterpret_cast<FuncType*>(Storage));
|
2022-04-24 14:42:40 +00:00
|
|
|
};
|
|
|
|
|
2022-11-16 11:13:37 +00:00
|
|
|
return InvokeResult<ResultType>(GetFunc(), Forward<Ts>(Args)...);
|
2022-04-24 14:42:40 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-07 07:57:02 +00:00
|
|
|
FORCEINLINE void AssignImpl(const TFunctionImpl& InValue)
|
|
|
|
{
|
2022-11-13 14:21:00 +00:00
|
|
|
if (InValue.IsValid()) 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())
|
|
|
|
{
|
|
|
|
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-11-13 14:21:00 +00:00
|
|
|
constexpr void ResetImpl() { GetCallableImpl() = nullptr; }
|
2022-05-14 14:52:21 +00:00
|
|
|
|
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-11-15 11:28:43 +00:00
|
|
|
template <CFunction F>
|
|
|
|
class TFunctionRef
|
2022-05-14 14:52:21 +00:00
|
|
|
: public NAMESPACE_PRIVATE::TFunctionImpl<
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
|
|
|
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-11-15 11:28:43 +00:00
|
|
|
using Impl = NAMESPACE_PRIVATE::TFunctionImpl<
|
2022-05-14 14:52:21 +00:00
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
|
|
|
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;
|
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename T> requires (!CTFunctionRef<TDecay<T>> && !CTInPlaceType<TDecay<T>>
|
|
|
|
&& NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value)
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TFunctionRef(T&& InValue)
|
|
|
|
{
|
2022-05-22 14:52:47 +00:00
|
|
|
using DecayedType = TDecay<T>;
|
2022-05-14 14:52:21 +00:00
|
|
|
checkf(NAMESPACE_PRIVATE::FunctionIsBound(InValue), TEXT("Cannot bind a null/unbound callable to a TFunctionRef"));
|
2022-11-15 11:28:43 +00:00
|
|
|
Impl::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
TFunctionRef(const T&&) = delete;
|
2022-04-07 07:57:02 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
template <CFunction F>
|
|
|
|
class TFunction
|
2022-05-14 14:52:21 +00:00
|
|
|
: public NAMESPACE_PRIVATE::TFunctionImpl<
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
|
|
|
false>
|
2022-04-07 07:57:02 +00:00
|
|
|
{
|
2022-05-14 14:52:21 +00:00
|
|
|
private:
|
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
using Impl = NAMESPACE_PRIVATE::TFunctionImpl<
|
2022-05-14 14:52:21 +00:00
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
|
|
|
false>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
constexpr TFunction(nullptr_t = nullptr) { Impl::ResetImpl(); }
|
2022-05-14 14:52:21 +00:00
|
|
|
|
|
|
|
FORCEINLINE TFunction(const TFunction& InValue) = default;
|
2022-11-15 11:28:43 +00:00
|
|
|
FORCEINLINE TFunction(TFunction&& InValue) : Impl(MoveTemp(InValue)) { InValue.ResetImpl(); }
|
2022-05-14 14:52:21 +00:00
|
|
|
|
|
|
|
FORCEINLINE TFunction& operator=(const TFunction& InValue)
|
|
|
|
{
|
2022-11-15 11:28:43 +00:00
|
|
|
Impl::AssignImpl(InValue);
|
2022-05-14 14:52:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE TFunction& operator=(TFunction&& InValue)
|
|
|
|
{
|
|
|
|
if (&InValue == this) return *this;
|
2022-11-15 11:28:43 +00:00
|
|
|
Impl::AssignImpl(MoveTemp(InValue));
|
2022-05-14 14:52:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename T> requires (!CTInPlaceType<TDecay<T>>
|
|
|
|
&& !CTFunctionRef<TDecay<T>> && !CTFunction<TDecay<T>> && !CTUniqueFunction<TDecay<T>>
|
2022-05-22 14:52:47 +00:00
|
|
|
&& CConstructibleFrom<TDecay<T>, T&&> && CCopyConstructible<TDecay<T>>
|
2022-11-16 14:03:54 +00:00
|
|
|
&& NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value)
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TFunction(T&& InValue)
|
|
|
|
{
|
2022-05-22 14:52:47 +00:00
|
|
|
using DecayedType = TDecay<T>;
|
2022-11-15 11:28:43 +00:00
|
|
|
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Impl::ResetImpl();
|
|
|
|
else Impl::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename T, typename... ArgTypes> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& CConstructibleFrom<TDecay<T>, ArgTypes...> && CCopyConstructible<TDecay<T>>)
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TFunction(TInPlaceType<T>, ArgTypes&&... Args)
|
|
|
|
{
|
2022-05-22 14:52:47 +00:00
|
|
|
using DecayedType = TDecay<T>;
|
2022-11-15 11:28:43 +00:00
|
|
|
Impl::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
constexpr TFunction& operator=(nullptr_t) { Impl::ResetImpl(); return *this; }
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename T> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& !CTFunctionRef<TDecay<T>> && !CTFunction<TDecay<T>> && !CTUniqueFunction<TDecay<T>>
|
|
|
|
&& CConstructibleFrom<TDecay<T>, T&&> && CCopyConstructible<TDecay<T>>)
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TFunction& operator=(T&& InValue)
|
|
|
|
{
|
2022-05-22 14:52:47 +00:00
|
|
|
using DecayedType = TDecay<T>;
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Impl::ResetImpl();
|
|
|
|
else Impl::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
|
2022-05-14 14:52:21 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename T, typename... ArgTypes> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& CConstructibleFrom<TDecay<T>, ArgTypes...> && CCopyConstructible<TDecay<T>>)
|
2022-05-22 14:52:47 +00:00
|
|
|
FORCEINLINE TDecay<T>& Emplace(ArgTypes&&... Args)
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
2022-05-22 14:52:47 +00:00
|
|
|
using DecayedType = TDecay<T>;
|
2022-11-15 11:28:43 +00:00
|
|
|
Impl::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
|
|
|
|
return Impl::template Target<DecayedType>();
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
constexpr void Reset() { Impl::ResetImpl(); }
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-04-07 07:57:02 +00:00
|
|
|
};
|
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
template <CFunction F>
|
|
|
|
class TUniqueFunction
|
2022-05-14 14:52:21 +00:00
|
|
|
: public NAMESPACE_PRIVATE::TFunctionImpl<
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
2022-11-15 11:28:43 +00:00
|
|
|
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-11-15 11:28:43 +00:00
|
|
|
using Impl = NAMESPACE_PRIVATE::TFunctionImpl<
|
2022-05-14 14:52:21 +00:00
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
|
|
|
|
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
|
|
|
|
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-11-15 11:28:43 +00:00
|
|
|
constexpr TUniqueFunction(nullptr_t = nullptr) { Impl::ResetImpl(); }
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TUniqueFunction(const TUniqueFunction& InValue) = delete;
|
2022-11-15 11:28:43 +00:00
|
|
|
TUniqueFunction(TUniqueFunction&& InValue) : Impl(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;
|
2022-11-15 11:28:43 +00:00
|
|
|
Impl::AssignImpl(MoveTemp(InValue));
|
2022-05-14 14:52:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-13 14:21:00 +00:00
|
|
|
FORCEINLINE TUniqueFunction(const TFunction<F>& InValue)
|
2022-11-15 11:28:43 +00:00
|
|
|
: Impl(*reinterpret_cast<const TUniqueFunction*>(&InValue))
|
2022-05-14 14:52:21 +00:00
|
|
|
{ }
|
|
|
|
|
2022-11-13 14:21:00 +00:00
|
|
|
FORCEINLINE TUniqueFunction(TFunction<F>&& InValue)
|
2022-11-15 11:28:43 +00:00
|
|
|
: Impl(MoveTemp(*reinterpret_cast<const TUniqueFunction*>(&InValue)))
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
|
|
|
InValue.Reset();
|
|
|
|
}
|
|
|
|
|
2022-11-13 14:21:00 +00:00
|
|
|
FORCEINLINE TUniqueFunction& operator=(const TFunction<F>& InValue)
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
2022-11-15 11:28:43 +00:00
|
|
|
Impl::AssignImpl(*reinterpret_cast<const TUniqueFunction*>(&InValue));
|
2022-05-14 14:52:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-11-13 14:21:00 +00:00
|
|
|
FORCEINLINE TUniqueFunction& operator=(TFunction<F>&& InValue)
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
2022-11-15 11:28:43 +00:00
|
|
|
Impl::AssignImpl(MoveTemp(*reinterpret_cast<TUniqueFunction*>(&InValue)));
|
2022-05-14 14:52:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename T> requires (!CTInPlaceType<TDecay<T>>
|
|
|
|
&& !CTFunctionRef<TDecay<T>> && !CTFunction<TDecay<T>> && !CTUniqueFunction<TDecay<T>>
|
2022-05-22 14:52:47 +00:00
|
|
|
&& CConstructibleFrom<TDecay<T>, T&&> && CMoveConstructible<TDecay<T>>
|
2022-11-16 14:03:54 +00:00
|
|
|
&& NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value)
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TUniqueFunction(T&& InValue)
|
|
|
|
{
|
2022-05-22 14:52:47 +00:00
|
|
|
using DecayedType = TDecay<T>;
|
2022-11-15 11:28:43 +00:00
|
|
|
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Impl::ResetImpl();
|
|
|
|
else Impl::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename T, typename... ArgTypes> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& CConstructibleFrom<TDecay<T>, ArgTypes...> && CMoveConstructible<TDecay<T>>)
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TUniqueFunction(TInPlaceType<T>, ArgTypes&&... Args)
|
|
|
|
{
|
2022-05-22 14:52:47 +00:00
|
|
|
using DecayedType = TDecay<T>;
|
2022-11-15 11:28:43 +00:00
|
|
|
Impl::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
constexpr TUniqueFunction& operator=(nullptr_t) { Impl::ResetImpl(); return *this; }
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename T> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& !CTFunctionRef<TDecay<T>> && !CTFunction<TDecay<T>> && !CTUniqueFunction<TDecay<T>>
|
|
|
|
&& CConstructibleFrom<TDecay<T>, T&&> && CMoveConstructible<TDecay<T>>)
|
2022-05-14 14:52:21 +00:00
|
|
|
FORCEINLINE TUniqueFunction& operator=(T&& InValue)
|
|
|
|
{
|
2022-05-22 14:52:47 +00:00
|
|
|
using DecayedType = TDecay<T>;
|
2022-05-14 14:52:21 +00:00
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Impl::ResetImpl();
|
|
|
|
else Impl::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
|
2022-05-14 14:52:21 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename T, typename... ArgTypes> requires (NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
|
|
|
|
&& CConstructibleFrom<TDecay<T>, ArgTypes...> && CMoveConstructible<TDecay<T>>)
|
2022-05-22 14:52:47 +00:00
|
|
|
FORCEINLINE TDecay<T>& Emplace(ArgTypes&&... Args)
|
2022-05-14 14:52:21 +00:00
|
|
|
{
|
2022-05-22 14:52:47 +00:00
|
|
|
using DecayedType = TDecay<T>;
|
2022-11-15 11:28:43 +00:00
|
|
|
Impl::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
|
|
|
|
return Impl::template Target<DecayedType>();
|
2022-05-14 14:52:21 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
constexpr void Reset() { Impl::ResetImpl(); }
|
2022-05-14 14:52:21 +00:00
|
|
|
|
|
|
|
};
|
2022-04-07 07:57:02 +00:00
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
template <CFunction F>
|
2022-04-07 07:57:02 +00:00
|
|
|
constexpr bool operator==(const TFunctionRef<F>& LHS, nullptr_t)
|
|
|
|
{
|
|
|
|
return !LHS;
|
|
|
|
}
|
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
template <CFunction F>
|
2022-04-07 07:57:02 +00:00
|
|
|
constexpr bool operator==(const TFunction<F>& LHS, nullptr_t)
|
|
|
|
{
|
|
|
|
return !LHS;
|
|
|
|
}
|
|
|
|
|
2022-11-15 11:28:43 +00:00
|
|
|
template <CFunction F>
|
2022-04-07 07:57:02 +00:00
|
|
|
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-11-16 14:03:54 +00:00
|
|
|
template <typename... Ts> requires (CInvocable<F&, Ts&&...>)
|
2022-11-16 11:13:37 +00:00
|
|
|
constexpr auto operator()(Ts&&... Args) &
|
|
|
|
-> decltype(!Invoke(Storage, Forward<Ts>(Args)...))
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-11-16 11:13:37 +00:00
|
|
|
return !Invoke(Storage, Forward<Ts>(Args)...);
|
2022-04-08 03:44:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename... Ts> requires (CInvocable<F&&, Ts&&...>)
|
2022-11-16 11:13:37 +00:00
|
|
|
constexpr auto operator()(Ts&&... Args) &&
|
|
|
|
-> decltype(!Invoke(MoveTemp(Storage), Forward<Ts>(Args)...))
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-11-16 11:13:37 +00:00
|
|
|
return !Invoke(MoveTemp(Storage), Forward<Ts>(Args)...);
|
2022-04-08 03:44:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename... Ts> requires (CInvocable<const F&, Ts&&...>)
|
2022-11-16 11:13:37 +00:00
|
|
|
constexpr auto operator()(Ts&&... Args) const&
|
|
|
|
-> decltype(!Invoke(Storage, Forward<Ts>(Args)...))
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-11-16 11:13:37 +00:00
|
|
|
return !Invoke(Storage, Forward<Ts>(Args)...);
|
2022-04-08 03:44:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename... Ts> requires (CInvocable<const F&&, Ts&&...>)
|
2022-11-16 11:13:37 +00:00
|
|
|
constexpr auto operator()(Ts&&... Args) const&&
|
|
|
|
-> decltype(!Invoke(MoveTemp(Storage), Forward<Ts>(Args)...))
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-11-16 11:13:37 +00:00
|
|
|
return !Invoke(MoveTemp(Storage), Forward<Ts>(Args)...);
|
2022-04-08 03:44:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NAMESPACE_PRIVATE_END
|
|
|
|
|
2022-11-16 14:03:54 +00:00
|
|
|
template <typename F> requires (CConstructibleFrom<F, F&&>)
|
2022-05-22 14:52:47 +00:00
|
|
|
constexpr NAMESPACE_PRIVATE::TNotFunction<TDecay<F>> NotFn(F&& Func)
|
2022-04-08 03:44:36 +00:00
|
|
|
{
|
2022-05-22 14:52:47 +00:00
|
|
|
return NAMESPACE_PRIVATE::TNotFunction<TDecay<F>>(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
|