refactor(templates): enhance encapsulation by changing part of the struct to a class

This commit is contained in:
_Redstone_c_ 2022-11-15 19:28:43 +08:00
parent 226f5a34e9
commit af07b1a134
7 changed files with 104 additions and 98 deletions

View File

@ -784,10 +784,10 @@ void TestTuple()
{
int8 Flag;
FTracker(int8 InFlag) : Flag(InFlag) { }
FTracker(const FTracker& InValue) { Flag = InValue.Flag - 1; always_check(!Flag); }
FTracker(FTracker&& InValue) { Flag = InValue.Flag + 1; always_check(!Flag); }
FTracker(const FTracker& InValue) { Flag = InValue.Flag - 1; always_check(!Flag); }
FTracker(FTracker&& InValue) { Flag = InValue.Flag + 1; always_check(!Flag); }
FTracker& operator=(const FTracker& InValue) { Flag = InValue.Flag - 1; always_check(!Flag); return *this; }
FTracker& operator=(FTracker&& InValue) { Flag = InValue.Flag + 1; always_check(!Flag); return *this; }
FTracker& operator=(FTracker&& InValue) { Flag = InValue.Flag + 1; always_check(!Flag); return *this; }
};
{

View File

@ -72,8 +72,10 @@ static_assert(CAnyCustomStorage<FAnyDefaultStorage>);
// You can add custom storage area through CustomStorage, such as TFunction.
// It is not recommended to use this, FAny is recommended.
template <CAnyCustomStorage CustomStorage = FAnyDefaultStorage>
struct TAny
class TAny
{
public:
inline static constexpr size_t InlineSize = CustomStorage::InlineSize;
inline static constexpr size_t InlineAlignment = CustomStorage::InlineAlignment;

View File

@ -13,14 +13,14 @@ NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
template <typename F> requires CFunction<F>
struct TFunctionRef;
template <CFunction F>
class TFunctionRef;
template <typename F> requires CFunction<F>
struct TFunction;
template <CFunction F>
class TFunction;
template <typename F> requires CFunction<F>
struct TUniqueFunction;
template <CFunction F>
class TUniqueFunction;
NAMESPACE_PRIVATE_BEGIN
@ -80,38 +80,10 @@ template <typename Ret, typename... Types> struct TFunctionInfo<Ret(Types...) co
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 CallableType>
struct alignas(16) FFunctionStorage
{
//~ 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;
};
template <typename F, typename CVRef, bool bIsRef> struct TFunctionImpl;
template <typename F, typename CVRef, bool bIsRef> class TFunctionImpl;
template <typename Ret, typename... Types, typename CVRef, bool bIsRef>
struct TFunctionImpl<Ret(Types...), CVRef, bIsRef>
class TFunctionImpl<Ret(Types...), CVRef, bIsRef>
{
public:
@ -176,6 +148,34 @@ private:
CallableType Callable;
};
template <typename CallableType>
struct alignas(16) FFunctionStorage
{
//~ 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<FFunctionStorage<CallableType>>;
using StorageType = TConditional<bIsRef, FunctionRefStorage, FunctionStorage>;
@ -207,7 +207,7 @@ private:
else return GetCallableImpl()(&Storage, Forward<Types>(Args)...);
}
protected:
protected: // These functions should not be used by user-defined class.
template <typename DecayedType, typename... ArgTypes>
FORCEINLINE void EmplaceImpl(ArgTypes&&... Args)
@ -257,8 +257,8 @@ protected:
NAMESPACE_PRIVATE_END
template <typename F> requires CFunction<F>
struct TFunctionRef
template <CFunction F>
class TFunctionRef
: public NAMESPACE_PRIVATE::TFunctionImpl<
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
@ -266,7 +266,7 @@ struct TFunctionRef
{
private:
using Super = NAMESPACE_PRIVATE::TFunctionImpl<
using Impl = NAMESPACE_PRIVATE::TFunctionImpl<
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
true>;
@ -287,7 +287,7 @@ public:
{
using DecayedType = TDecay<T>;
checkf(NAMESPACE_PRIVATE::FunctionIsBound(InValue), TEXT("Cannot bind a null/unbound callable to a TFunctionRef"));
Super::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
Impl::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
}
template <typename T>
@ -295,8 +295,8 @@ public:
};
template <typename F> requires CFunction<F>
struct TFunction
template <CFunction F>
class TFunction
: public NAMESPACE_PRIVATE::TFunctionImpl<
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
@ -304,28 +304,28 @@ struct TFunction
{
private:
using Super = NAMESPACE_PRIVATE::TFunctionImpl<
using Impl = NAMESPACE_PRIVATE::TFunctionImpl<
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
false>;
public:
constexpr TFunction(nullptr_t = nullptr) { Super::ResetImpl(); }
constexpr TFunction(nullptr_t = nullptr) { Impl::ResetImpl(); }
FORCEINLINE TFunction(const TFunction& InValue) = default;
FORCEINLINE TFunction(TFunction&& InValue) : Super(MoveTemp(InValue)) { InValue.ResetImpl(); }
FORCEINLINE TFunction(TFunction&& InValue) : Impl(MoveTemp(InValue)) { InValue.ResetImpl(); }
FORCEINLINE TFunction& operator=(const TFunction& InValue)
{
Super::AssignImpl(InValue);
Impl::AssignImpl(InValue);
return *this;
}
FORCEINLINE TFunction& operator=(TFunction&& InValue)
{
if (&InValue == this) return *this;
Super::AssignImpl(MoveTemp(InValue));
Impl::AssignImpl(MoveTemp(InValue));
return *this;
}
@ -336,8 +336,8 @@ public:
FORCEINLINE TFunction(T&& InValue)
{
using DecayedType = TDecay<T>;
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Super::ResetImpl();
else Super::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Impl::ResetImpl();
else Impl::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
}
template <typename T, typename... ArgTypes> requires NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
@ -345,10 +345,10 @@ public:
FORCEINLINE TFunction(TInPlaceType<T>, ArgTypes&&... Args)
{
using DecayedType = TDecay<T>;
Super::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
Impl::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
}
constexpr TFunction& operator=(nullptr_t) { Super::ResetImpl(); return *this; }
constexpr TFunction& operator=(nullptr_t) { Impl::ResetImpl(); return *this; }
template <typename T> requires NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
&& (!CTFunctionRef<TDecay<T>>) && (!CTFunction<TDecay<T>>) && (!CTUniqueFunction<TDecay<T>>)
@ -357,8 +357,8 @@ public:
{
using DecayedType = TDecay<T>;
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Super::ResetImpl();
else Super::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Impl::ResetImpl();
else Impl::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
return *this;
}
@ -368,62 +368,62 @@ public:
FORCEINLINE TDecay<T>& Emplace(ArgTypes&&... Args)
{
using DecayedType = TDecay<T>;
Super::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
return Super::template Target<DecayedType>();
Impl::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
return Impl::template Target<DecayedType>();
}
constexpr void Reset() { Super::ResetImpl(); }
constexpr void Reset() { Impl::ResetImpl(); }
};
template <typename F> requires CFunction<F>
struct TUniqueFunction
template <CFunction F>
class TUniqueFunction
: public NAMESPACE_PRIVATE::TFunctionImpl<
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
false>
false>
{
private:
using Super = NAMESPACE_PRIVATE::TFunctionImpl<
using Impl = NAMESPACE_PRIVATE::TFunctionImpl<
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::Fn,
typename NAMESPACE_PRIVATE::TFunctionInfo<F>::CVRef,
false>;
public:
constexpr TUniqueFunction(nullptr_t = nullptr) { Super::ResetImpl(); }
constexpr TUniqueFunction(nullptr_t = nullptr) { Impl::ResetImpl(); }
FORCEINLINE TUniqueFunction(const TUniqueFunction& InValue) = delete;
TUniqueFunction(TUniqueFunction&& InValue) : Super(MoveTemp(InValue)) { InValue.ResetImpl(); }
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;
Super::AssignImpl(MoveTemp(InValue));
Impl::AssignImpl(MoveTemp(InValue));
return *this;
}
FORCEINLINE TUniqueFunction(const TFunction<F>& InValue)
: Super(*reinterpret_cast<const TUniqueFunction*>(&InValue))
: Impl(*reinterpret_cast<const TUniqueFunction*>(&InValue))
{ }
FORCEINLINE TUniqueFunction(TFunction<F>&& InValue)
: Super(MoveTemp(*reinterpret_cast<const TUniqueFunction*>(&InValue)))
: Impl(MoveTemp(*reinterpret_cast<const TUniqueFunction*>(&InValue)))
{
InValue.Reset();
}
FORCEINLINE TUniqueFunction& operator=(const TFunction<F>& InValue)
{
Super::AssignImpl(*reinterpret_cast<const TUniqueFunction*>(&InValue));
Impl::AssignImpl(*reinterpret_cast<const TUniqueFunction*>(&InValue));
return *this;
}
FORCEINLINE TUniqueFunction& operator=(TFunction<F>&& InValue)
{
Super::AssignImpl(MoveTemp(*reinterpret_cast<TUniqueFunction*>(&InValue)));
Impl::AssignImpl(MoveTemp(*reinterpret_cast<TUniqueFunction*>(&InValue)));
return *this;
}
@ -434,8 +434,8 @@ public:
FORCEINLINE TUniqueFunction(T&& InValue)
{
using DecayedType = TDecay<T>;
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Super::ResetImpl();
else Super::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Impl::ResetImpl();
else Impl::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
}
template <typename T, typename... ArgTypes> requires NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
@ -443,10 +443,10 @@ public:
FORCEINLINE TUniqueFunction(TInPlaceType<T>, ArgTypes&&... Args)
{
using DecayedType = TDecay<T>;
Super::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
Impl::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
}
constexpr TUniqueFunction& operator=(nullptr_t) { Super::ResetImpl(); return *this; }
constexpr TUniqueFunction& operator=(nullptr_t) { Impl::ResetImpl(); return *this; }
template <typename T> requires NAMESPACE_PRIVATE::TIsInvocableSignature<F, TDecay<T>>::Value
&& (!CTFunctionRef<TDecay<T>>) && (!CTFunction<TDecay<T>>) && (!CTUniqueFunction<TDecay<T>>)
@ -455,8 +455,8 @@ public:
{
using DecayedType = TDecay<T>;
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Super::ResetImpl();
else Super::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
if (!NAMESPACE_PRIVATE::FunctionIsBound(InValue)) Impl::ResetImpl();
else Impl::template EmplaceImpl<DecayedType>(Forward<T>(InValue));
return *this;
}
@ -466,27 +466,27 @@ public:
FORCEINLINE TDecay<T>& Emplace(ArgTypes&&... Args)
{
using DecayedType = TDecay<T>;
Super::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
return Super::template Target<DecayedType>();
Impl::template EmplaceImpl<DecayedType>(Forward<ArgTypes>(Args)...);
return Impl::template Target<DecayedType>();
}
constexpr void Reset() { Super::ResetImpl(); }
constexpr void Reset() { Impl::ResetImpl(); }
};
template <typename F>
template <CFunction F>
constexpr bool operator==(const TFunctionRef<F>& LHS, nullptr_t)
{
return !LHS;
}
template <typename F>
template <CFunction F>
constexpr bool operator==(const TFunction<F>& LHS, nullptr_t)
{
return !LHS;
}
template <typename F>
template <CFunction F>
constexpr bool operator==(const TUniqueFunction<F>& LHS, nullptr_t)
{
return !LHS;

View File

@ -12,7 +12,7 @@ NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
template <typename OptionalType> requires CDestructible<OptionalType>
struct TOptional
class TOptional
{
private:

View File

@ -11,7 +11,7 @@ NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
template <typename ReferencedType> requires (CObject<ReferencedType> || CFunction<ReferencedType>)
struct TReferenceWrapper
class TReferenceWrapper
{
public:
@ -61,11 +61,11 @@ private:
ReferencedType* Pointer;
template <typename T> requires (CObject<T> || CFunction<T>) friend struct TReferenceWrapper;
template <typename T> requires (CObject<T> || CFunction<T>) friend class TReferenceWrapper;
// Optimize TOptional with these hacking
constexpr TReferenceWrapper(FInvalid) : Pointer(nullptr) { };
template <typename T> requires CDestructible<T> friend struct TOptional;
template <typename T> requires CDestructible<T> friend class TOptional;
};
@ -121,7 +121,7 @@ template <typename T>
using TUnwrapRefDecay = typename NAMESPACE_PRIVATE::TUnwrapRefDecayImpl<T>::Type;
template <typename ReferencedType>
struct TOptional<TReferenceWrapper<ReferencedType>>
class TOptional<TReferenceWrapper<ReferencedType>>
{
private:
@ -235,7 +235,7 @@ public:
private:
TReferenceWrapper<ReferencedType> Reference;
template <typename T> requires CDestructible<T> friend struct TOptional;
template <typename T> requires CDestructible<T> friend class TOptional;
};

View File

@ -17,7 +17,7 @@ NAMESPACE_MODULE_BEGIN(Utility)
#define RS_TUPLE_ELEMENT_STATIC_ALIAS 1
template <typename... Types>
struct TTuple;
class TTuple;
NAMESPACE_PRIVATE_BEGIN
@ -209,10 +209,10 @@ constexpr TTuple<TUnwrapRefDecay<Types>...> MakeTupleImpl(Types&&... Args)
}
template <typename Indices, typename... Types>
struct TTupleImpl;
class TTupleImpl;
template <size_t... Indices, typename... Types>
struct TTupleImpl<TIndexSequence<Indices...>, Types...> : TTupleBasicElement<Types, Indices>...
class TTupleImpl<TIndexSequence<Indices...>, Types...> : public TTupleBasicElement<Types, Indices>...
{
protected:
@ -237,11 +237,13 @@ protected:
};
template <typename Indices, typename... Types>
struct TTupleHelper;
class TTupleHelper;
template <size_t... Indices>
struct TTupleHelper<TIndexSequence<Indices...>>
class TTupleHelper<TIndexSequence<Indices...>>
{
public:
template <typename LHSTupleType, typename RHSTupleType>
static constexpr void Assign(LHSTupleType& LHS, RHSTupleType&& RHS)
{
@ -299,7 +301,7 @@ template <size_t I, typename TupleType>
using TTupleElement = typename NAMESPACE_PRIVATE::TTupleElementImpl<I, TupleType>::Type;
template <typename... Types>
struct TTuple : NAMESPACE_PRIVATE::TTupleImpl<TIndexSequenceFor<Types...>, Types...>
class TTuple : public NAMESPACE_PRIVATE::TTupleImpl<TIndexSequenceFor<Types...>, Types...>
{
private:

View File

@ -14,7 +14,7 @@ NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
template <typename... Types> requires (true && ... && CDestructible<Types>)
struct TVariant;
class TVariant;
NAMESPACE_PRIVATE_BEGIN
@ -139,8 +139,10 @@ template <size_t I, typename VariantType>
using TVariantAlternative = typename NAMESPACE_PRIVATE::TVariantAlternativeImpl<I, VariantType>::Type;
template <typename... Types> requires (true && ... && CDestructible<Types>)
struct TVariant
class TVariant
{
public:
constexpr TVariant() : TypeIndex(0xFF) { };
constexpr TVariant(FInvalid) : TVariant() { };