#pragma once #include "CoreTypes.h" #include "Templates/Any.h" #include "Templates/Tuple.h" #include "Templates/Invoke.h" #include "Memory/Alignment.h" #include "Templates/Utility.h" #include "TypeTraits/TypeTraits.h" #include "Miscellaneous/AssertionMacros.h" NAMESPACE_REDCRAFT_BEGIN NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Utility) template class TFunctionRef; template class TFunction; template class TUniqueFunction; NAMESPACE_PRIVATE_BEGIN template struct TIsTFunctionRef : FFalse { }; template struct TIsTFunctionRef> : FTrue { }; template struct TIsTFunction : FFalse { }; template struct TIsTFunction> : FTrue { }; template struct TIsTUniqueFunction : FFalse { }; template struct TIsTUniqueFunction> : FTrue { }; NAMESPACE_PRIVATE_END template concept CTFunctionRef = NAMESPACE_PRIVATE::TIsTFunctionRef>::Value; template concept CTFunction = NAMESPACE_PRIVATE::TIsTFunction>::Value; template concept CTUniqueFunction = NAMESPACE_PRIVATE::TIsTUniqueFunction>::Value; NAMESPACE_PRIVATE_BEGIN template constexpr bool FunctionIsBound(const T& Func) { if constexpr (CPointer || CMemberPointer || CTFunctionRef || CTFunction || CTUniqueFunction) { return !!Func; } else { return true; } } template struct TIsInvocableSignature : FFalse { }; template struct TIsInvocableSignature : TBoolConstant && CInvocableResult> { }; template struct TIsInvocableSignature : TBoolConstant> { }; template struct TIsInvocableSignature : TBoolConstant> { }; template struct TIsInvocableSignature : TBoolConstant && CInvocableResult> { }; template struct TIsInvocableSignature : TBoolConstant> { }; template struct TIsInvocableSignature : TBoolConstant> { }; template struct TFunctionInfo; template struct TFunctionInfo { using Fn = Ret(Ts...); using CVRef = int; }; template struct TFunctionInfo { using Fn = Ret(Ts...); using CVRef = int&; }; template struct TFunctionInfo { using Fn = Ret(Ts...); using CVRef = int&&; }; template struct TFunctionInfo { using Fn = Ret(Ts...); using CVRef = const int; }; template struct TFunctionInfo { using Fn = Ret(Ts...); using CVRef = const int&; }; template struct TFunctionInfo { using Fn = Ret(Ts...); using CVRef = const int&&; }; template class TFunctionImpl; template class TFunctionImpl { public: using ResultType = Ret; using ArgumentType = TTuple; TFunctionImpl() = default; TFunctionImpl(const TFunctionImpl&) = default; TFunctionImpl(TFunctionImpl&& InValue) = default; TFunctionImpl& operator=(const TFunctionImpl&) = default; TFunctionImpl& operator=(TFunctionImpl&&) = default; ~TFunctionImpl() = default; FORCEINLINE ResultType operator()(Ts... Args) requires (CSameAs) { return CallImpl(Forward(Args)...); } FORCEINLINE ResultType operator()(Ts... Args) & requires (CSameAs) { return CallImpl(Forward(Args)...); } FORCEINLINE ResultType operator()(Ts... Args) && requires (CSameAs) { return CallImpl(Forward(Args)...); } FORCEINLINE ResultType operator()(Ts... Args) const requires (CSameAs) { return CallImpl(Forward(Args)...); } FORCEINLINE ResultType operator()(Ts... Args) const& requires (CSameAs) { return CallImpl(Forward(Args)...); } FORCEINLINE ResultType operator()(Ts... Args) const&& requires (CSameAs) { return CallImpl(Forward(Args)...); } constexpr bool IsValid() const { return GetCallableImpl() != nullptr; } constexpr explicit operator bool() const { return GetCallableImpl() != nullptr; } FORCEINLINE const type_info& TargetType() const requires (!bIsRef) { return IsValid() ? Storage.GetTypeInfo() : typeid(void); }; template FORCEINLINE T& Target() & requires (!bIsRef && CDestructible>) { return static_cast< StorageType& >(Storage).template GetValue(); } template FORCEINLINE T&& Target() && requires (!bIsRef && CDestructible>) { return static_cast< StorageType&&>(Storage).template GetValue(); } template FORCEINLINE const T& Target() const& requires (!bIsRef && CDestructible>) { return static_cast(Storage).template GetValue(); } template FORCEINLINE const T&& Target() const&& requires (!bIsRef && CDestructible>) { return static_cast(Storage).template GetValue(); } constexpr void Swap(TFunctionImpl& InValue) requires (!bIsRef) { using NAMESPACE_REDCRAFT::Swap; if (!IsValid() && !InValue.IsValid()) return; if (IsValid() && !InValue.IsValid()) { InValue = MoveTemp(*this); ResetImpl(); return; } if (InValue.IsValid() && !IsValid()) { *this = MoveTemp(InValue); InValue.ResetImpl(); return; } Swap(Storage, InValue.Storage); } private: using StoragePtrType = TCopyConst*; using CallableType = ResultType(*)(StoragePtrType, Ts&&...); struct FunctionRefStorage { StoragePtrType Ptr; CallableType Callable; }; template struct alignas(16) FFunctionStorage : FSingleton { //~ 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; }; using FunctionStorage = TAny>; using StorageType = TConditional; StorageType Storage; 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; } FORCEINLINE ResultType CallImpl(Ts&&... Args) { checkf(IsValid(), TEXT("Attempting to call an unbound TFunction!")); if constexpr (bIsRef) return GetCallableImpl()(Storage.Ptr, Forward(Args)...); else return GetCallableImpl()(&Storage, Forward(Args)...); } FORCEINLINE ResultType CallImpl(Ts&&... Args) const { checkf(IsValid(), TEXT("Attempting to call an unbound TFunction!")); if constexpr (bIsRef) return GetCallableImpl()(Storage.Ptr, Forward(Args)...); else return GetCallableImpl()(&Storage, Forward(Args)...); } protected: // These functions should not be used by user-defined class template FORCEINLINE void EmplaceImpl(ArgTypes&&... Args) { using FuncType = TCopyConst, DecayedType>; if constexpr (bIsRef) Storage.Ptr = (AddressOf(Args), ...); else Storage.template Emplace(Forward(Args)...); GetCallableImpl() = [](StoragePtrType Storage, Ts&&... Args) -> ResultType { using InvokeType = TConditional< CReference, TCopyCVRef, TCopyCVRef& >; const auto GetFunc = [Storage]() -> InvokeType { if constexpr (!bIsRef) return static_cast*>(Storage)->template GetValue(); else return static_cast(*reinterpret_cast(Storage)); }; return InvokeResult(GetFunc(), Forward(Args)...); }; } FORCEINLINE void AssignImpl(const TFunctionImpl& InValue) { if (InValue.IsValid()) Storage = InValue.Storage; else ResetImpl(); } FORCEINLINE void AssignImpl(TFunctionImpl&& InValue) { if (InValue.IsValid()) { Storage = MoveTemp(InValue.Storage); InValue.ResetImpl(); } else ResetImpl(); } constexpr void ResetImpl() { GetCallableImpl() = nullptr; } }; NAMESPACE_PRIVATE_END template class TFunctionRef : public NAMESPACE_PRIVATE::TFunctionImpl< typename NAMESPACE_PRIVATE::TFunctionInfo::Fn, typename NAMESPACE_PRIVATE::TFunctionInfo::CVRef, true> { private: using Impl = NAMESPACE_PRIVATE::TFunctionImpl< typename NAMESPACE_PRIVATE::TFunctionInfo::Fn, typename NAMESPACE_PRIVATE::TFunctionInfo::CVRef, true>; public: TFunctionRef() = delete; TFunctionRef(const TFunctionRef& InValue) = default; TFunctionRef(TFunctionRef&& InValue) = default; TFunctionRef& operator=(const TFunctionRef& InValue) = delete; TFunctionRef& operator=(TFunctionRef&& InValue) = delete; template requires (!CTFunctionRef> && !CTInPlaceType> && NAMESPACE_PRIVATE::TIsInvocableSignature>::Value) FORCEINLINE TFunctionRef(T&& InValue) { using DecayedType = TDecay; checkf(NAMESPACE_PRIVATE::FunctionIsBound(InValue), TEXT("Cannot bind a null/unbound callable to a TFunctionRef")); Impl::template EmplaceImpl(Forward(InValue)); } template TFunctionRef(const T&&) = delete; }; template class TFunction : public NAMESPACE_PRIVATE::TFunctionImpl< typename NAMESPACE_PRIVATE::TFunctionInfo::Fn, typename NAMESPACE_PRIVATE::TFunctionInfo::CVRef, false> { private: using Impl = NAMESPACE_PRIVATE::TFunctionImpl< typename NAMESPACE_PRIVATE::TFunctionInfo::Fn, typename NAMESPACE_PRIVATE::TFunctionInfo::CVRef, false>; public: constexpr TFunction(nullptr_t = nullptr) { Impl::ResetImpl(); } FORCEINLINE TFunction(const TFunction& InValue) = default; FORCEINLINE TFunction(TFunction&& InValue) : Impl(MoveTemp(InValue)) { InValue.ResetImpl(); } FORCEINLINE TFunction& operator=(const TFunction& InValue) { Impl::AssignImpl(InValue); return *this; } FORCEINLINE TFunction& operator=(TFunction&& InValue) { if (&InValue == this) return *this; Impl::AssignImpl(MoveTemp(InValue)); return *this; } template requires (!CTInPlaceType> && !CTFunctionRef> && !CTFunction> && !CTUniqueFunction> && CConstructibleFrom, T&&> && CCopyConstructible> && NAMESPACE_PRIVATE::TIsInvocableSignature>::Value) FORCEINLINE TFunction(T&& InValue) { using DecayedType = TDecay; if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Impl::ResetImpl(); else Impl::template EmplaceImpl(Forward(InValue)); } template requires (NAMESPACE_PRIVATE::TIsInvocableSignature>::Value && CConstructibleFrom, ArgTypes...> && CCopyConstructible>) FORCEINLINE TFunction(TInPlaceType, ArgTypes&&... Args) { using DecayedType = TDecay; Impl::template EmplaceImpl(Forward(Args)...); } constexpr TFunction& operator=(nullptr_t) { Impl::ResetImpl(); return *this; } template requires (NAMESPACE_PRIVATE::TIsInvocableSignature>::Value && !CTFunctionRef> && !CTFunction> && !CTUniqueFunction> && CConstructibleFrom, T&&> && CCopyConstructible>) FORCEINLINE TFunction& operator=(T&& InValue) { using DecayedType = TDecay; if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Impl::ResetImpl(); else Impl::template EmplaceImpl(Forward(InValue)); return *this; } template requires (NAMESPACE_PRIVATE::TIsInvocableSignature>::Value && CConstructibleFrom, ArgTypes...> && CCopyConstructible>) FORCEINLINE TDecay& Emplace(ArgTypes&&... Args) { using DecayedType = TDecay; Impl::template EmplaceImpl(Forward(Args)...); return Impl::template Target(); } constexpr void Reset() { Impl::ResetImpl(); } }; template class TUniqueFunction : public NAMESPACE_PRIVATE::TFunctionImpl< typename NAMESPACE_PRIVATE::TFunctionInfo::Fn, typename NAMESPACE_PRIVATE::TFunctionInfo::CVRef, false> { private: using Impl = NAMESPACE_PRIVATE::TFunctionImpl< typename NAMESPACE_PRIVATE::TFunctionInfo::Fn, typename NAMESPACE_PRIVATE::TFunctionInfo::CVRef, false>; public: constexpr TUniqueFunction(nullptr_t = nullptr) { Impl::ResetImpl(); } FORCEINLINE TUniqueFunction(const TUniqueFunction& InValue) = delete; TUniqueFunction(TUniqueFunction&& InValue) : Impl(MoveTemp(InValue)) { InValue.ResetImpl(); } FORCEINLINE TUniqueFunction& operator=(const TUniqueFunction& InValue) = delete; FORCEINLINE TUniqueFunction& operator=(TUniqueFunction&& InValue) { if (&InValue == this) return *this; Impl::AssignImpl(MoveTemp(InValue)); return *this; } FORCEINLINE TUniqueFunction(const TFunction& InValue) : Impl(*reinterpret_cast(&InValue)) { } FORCEINLINE TUniqueFunction(TFunction&& InValue) : Impl(MoveTemp(*reinterpret_cast(&InValue))) { InValue.Reset(); } FORCEINLINE TUniqueFunction& operator=(const TFunction& InValue) { Impl::AssignImpl(*reinterpret_cast(&InValue)); return *this; } FORCEINLINE TUniqueFunction& operator=(TFunction&& InValue) { Impl::AssignImpl(MoveTemp(*reinterpret_cast(&InValue))); return *this; } template requires (!CTInPlaceType> && !CTFunctionRef> && !CTFunction> && !CTUniqueFunction> && CConstructibleFrom, T&&> && CMoveConstructible> && NAMESPACE_PRIVATE::TIsInvocableSignature>::Value) FORCEINLINE TUniqueFunction(T&& InValue) { using DecayedType = TDecay; if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Impl::ResetImpl(); else Impl::template EmplaceImpl(Forward(InValue)); } template requires (NAMESPACE_PRIVATE::TIsInvocableSignature>::Value && CConstructibleFrom, ArgTypes...> && CMoveConstructible>) FORCEINLINE TUniqueFunction(TInPlaceType, ArgTypes&&... Args) { using DecayedType = TDecay; Impl::template EmplaceImpl(Forward(Args)...); } constexpr TUniqueFunction& operator=(nullptr_t) { Impl::ResetImpl(); return *this; } template requires (NAMESPACE_PRIVATE::TIsInvocableSignature>::Value && !CTFunctionRef> && !CTFunction> && !CTUniqueFunction> && CConstructibleFrom, T&&> && CMoveConstructible>) FORCEINLINE TUniqueFunction& operator=(T&& InValue) { using DecayedType = TDecay; if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Impl::ResetImpl(); else Impl::template EmplaceImpl(Forward(InValue)); return *this; } template requires (NAMESPACE_PRIVATE::TIsInvocableSignature>::Value && CConstructibleFrom, ArgTypes...> && CMoveConstructible>) FORCEINLINE TDecay& Emplace(ArgTypes&&... Args) { using DecayedType = TDecay; Impl::template EmplaceImpl(Forward(Args)...); return Impl::template Target(); } constexpr void Reset() { Impl::ResetImpl(); } }; template constexpr bool operator==(const TFunctionRef& LHS, nullptr_t) { return !LHS; } template constexpr bool operator==(const TFunction& LHS, nullptr_t) { return !LHS; } template constexpr bool operator==(const TUniqueFunction& LHS, nullptr_t) { return !LHS; } static_assert(sizeof(TFunction) == 64, "The byte size of TFunction is unexpected"); static_assert(sizeof(TUniqueFunction) == 64, "The byte size of TUniqueFunction is unexpected"); NAMESPACE_PRIVATE_BEGIN template struct TNotFunction { F Storage; TNotFunction(const TNotFunction&) = default; TNotFunction(TNotFunction&&) = default; template constexpr TNotFunction(InF&& InFunc) : Storage(Forward(InFunc)) { } template requires (CInvocable) constexpr auto operator()(Ts&&... Args) & -> decltype(!Invoke(Storage, Forward(Args)...)) { return !Invoke(Storage, Forward(Args)...); } template requires (CInvocable) constexpr auto operator()(Ts&&... Args) && -> decltype(!Invoke(MoveTemp(Storage), Forward(Args)...)) { return !Invoke(MoveTemp(Storage), Forward(Args)...); } template requires (CInvocable) constexpr auto operator()(Ts&&... Args) const& -> decltype(!Invoke(Storage, Forward(Args)...)) { return !Invoke(Storage, Forward(Args)...); } template requires (CInvocable) constexpr auto operator()(Ts&&... Args) const&& -> decltype(!Invoke(MoveTemp(Storage), Forward(Args)...)) { return !Invoke(MoveTemp(Storage), Forward(Args)...); } }; NAMESPACE_PRIVATE_END template requires (CConstructibleFrom) constexpr NAMESPACE_PRIVATE::TNotFunction> NotFn(F&& Func) { return NAMESPACE_PRIVATE::TNotFunction>(Forward(Func)); } NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END