Compare commits

..

No commits in common. "84bf41175244f360b595e25d814ad7321c79fdab" and "c8650b4aa566cdae0e34adbb8d273b3f2364c5c5" have entirely different histories.

6 changed files with 362 additions and 343 deletions

View File

@ -124,6 +124,7 @@ NAMESPACE_UNNAMED_END
void TestArray()
{
TestArrayTemplate<FDefaultAllocator, 0>();
TestArrayTemplate<FHeapAllocator, 0>();
TestArrayTemplate<TInlineAllocator<8>, 8>();
TestArrayTemplate<TFixedAllocator<64>, 64>();

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,7 @@ NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
template <CElementalObject T, size_t InExtent>
template <CObject T, size_t InExtent>
class TArrayView;
NAMESPACE_PRIVATE_BEGIN
@ -55,8 +55,8 @@ public:
FORCEINLINE constexpr TArrayViewIterator& operator++() { ++Pointer; CheckThis(); return *this; }
FORCEINLINE constexpr TArrayViewIterator& operator--() { --Pointer; CheckThis(); return *this; }
FORCEINLINE constexpr TArrayViewIterator operator++(int) { TArrayViewIterator Temp = *this; ++*this; return Temp; }
FORCEINLINE constexpr TArrayViewIterator operator--(int) { TArrayViewIterator Temp = *this; --*this; return Temp; }
FORCEINLINE constexpr TArrayViewIterator operator++(int) { TArrayViewIterator Temp = *this; ++Pointer; CheckThis(); return Temp; }
FORCEINLINE constexpr TArrayViewIterator operator--(int) { TArrayViewIterator Temp = *this; --Pointer; CheckThis(); return Temp; }
FORCEINLINE constexpr TArrayViewIterator& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; }
FORCEINLINE constexpr TArrayViewIterator& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; }
@ -103,7 +103,7 @@ private:
template <typename U>
friend class TArrayViewIterator;
template <CElementalObject U, size_t InExtent>
template <CObject U, size_t InExtent>
friend class NAMESPACE_REDCRAFT::TArrayView;
};
@ -116,10 +116,10 @@ struct TEnableArrayNum<false> { size_t ArrayNum; };
NAMESPACE_PRIVATE_END
template <CElementalObject T, size_t N>
template <CObject T, size_t N>
struct TStaticArray;
template <CElementalObject T, CInstantiableAllocator A> requires (!CConst<T>)
template <CObject T, typename A> requires (!CConst<T> && CDestructible<T> && CInstantiableAllocator<A>)
class TArray;
inline constexpr size_t DynamicExtent = INDEX_NONE;
@ -129,7 +129,7 @@ inline constexpr size_t DynamicExtent = INDEX_NONE;
* the sequence at position zero. A TArrayView can either have a static extent, in which case the number of elements in the sequence
* is known at compile-time and encoded in the type, or a dynamic extent.
*/
template <CElementalObject T, size_t InExtent = DynamicExtent>
template <CObject T, size_t InExtent = DynamicExtent>
class TArrayView final : private NAMESPACE_PRIVATE::TEnableArrayNum<InExtent == DynamicExtent>
{
private:
@ -140,8 +140,6 @@ public:
using ElementType = T;
using Reference = T&;
using Iterator = NAMESPACE_PRIVATE::TArrayViewIterator<ElementType>;
using ReverseIterator = TReverseIterator<Iterator>;

View File

@ -14,9 +14,6 @@ NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
template <typename T>
concept CElementalObject = CObject<T> && CDestructible<T>;
NAMESPACE_PRIVATE_BEGIN
template <typename T> using WithReference = T&;

View File

@ -17,15 +17,12 @@ NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
/** TStaticArray is a container that encapsulates fixed size arrays. */
template <CElementalObject T, size_t N>
template <CObject T, size_t N>
struct TStaticArray final
{
using ElementType = T;
using Reference = T&;
using ConstReference = const T&;
using Iterator = TArrayView< T, N>::Iterator;
using ConstIterator = TArrayView<const T, N>::Iterator;

View File

@ -55,40 +55,6 @@ struct FAllocatorInterface
};
};
#define ALLOCATOR_WRAPPER_BEGIN(Allocator, Name) \
\
struct PREPROCESSOR_JOIN(F, Name) : private FSingleton
#define ALLOCATOR_WRAPPER_END(Allocator, Name) ; \
\
template <typename A, bool = CEmpty<A> && !CFinal<A>> \
struct PREPROCESSOR_JOIN(T, Name); \
\
template <typename A> \
struct PREPROCESSOR_JOIN(T, Name)<A, true> : public PREPROCESSOR_JOIN(F, Name), private A \
{ \
NODISCARD FORCEINLINE A& operator*() { return *this; } \
NODISCARD FORCEINLINE const A& operator*() const { return *this; } \
NODISCARD FORCEINLINE A* operator->() { return this; } \
NODISCARD FORCEINLINE const A* operator->() const { return this; } \
}; \
\
template <typename A> \
struct PREPROCESSOR_JOIN(T, Name)<A, false> : public PREPROCESSOR_JOIN(F, Name) \
{ \
NODISCARD FORCEINLINE A& operator*() { return AllocatorInstance; } \
NODISCARD FORCEINLINE const A& operator*() const { return AllocatorInstance; } \
NODISCARD FORCEINLINE A* operator->() { return &AllocatorInstance; } \
NODISCARD FORCEINLINE const A* operator->() const { return &AllocatorInstance; } \
\
private: \
\
A AllocatorInstance; \
\
}; \
\
PREPROCESSOR_JOIN(T, Name)<typename Allocator::template ForElementType<T>> Name;
/** This is heap allocator that calls Memory::Malloc() directly for memory allocation. */
struct FHeapAllocator : public FAllocatorInterface
{
@ -154,11 +120,13 @@ struct FHeapAllocator : public FAllocatorInterface
};
};
using FDefaultAllocator = FHeapAllocator;
/**
* The inline allocator allocates up to a specified number of elements in the same allocation as the container.
* Any allocation needed beyond that causes all data to be moved into an indirect allocation.
*/
template <size_t NumInline, CInstantiableAllocator SecondaryAllocator = FHeapAllocator>
template <size_t NumInline, CInstantiableAllocator SecondaryAllocator = FDefaultAllocator>
struct TInlineAllocator : public FAllocatorInterface
{
template <CObject T>
@ -172,23 +140,23 @@ struct TInlineAllocator : public FAllocatorInterface
check(InNum >= NumInline);
if (InNum == NumInline) return Impl.GetInline();
if (InNum == NumInline) return reinterpret_cast<T*>(&InlineStorage);
return Impl->Allocate(InNum);
return Secondary.Allocate(InNum);
}
FORCEINLINE void Deallocate(T* InPtr)
{
if (InPtr == Impl.GetInline()) return;
if (InPtr == reinterpret_cast<T*>(&InlineStorage)) return;
Impl->Deallocate(InPtr);
Secondary.Deallocate(InPtr);
}
NODISCARD FORCEINLINE bool IsTransferable(T* InPtr) const
{
if (InPtr == Impl.GetInline()) return false;
if (InPtr == reinterpret_cast<const T*>(&InlineStorage)) return false;
return Impl->IsTransferable(InPtr);
return Secondary.IsTransferable(InPtr);
}
NODISCARD FORCEINLINE size_t CalculateSlackGrow(size_t Num, size_t NumAllocated) const
@ -198,7 +166,7 @@ struct TInlineAllocator : public FAllocatorInterface
if (Num <= NumInline) return NumInline;
return Impl->CalculateSlackGrow(Num, NumAllocated <= NumInline ? 0 : NumAllocated);
return Secondary.CalculateSlackGrow(Num, NumAllocated <= NumInline ? 0 : NumAllocated);
}
NODISCARD FORCEINLINE size_t CalculateSlackShrink(size_t Num, size_t NumAllocated) const
@ -208,26 +176,21 @@ struct TInlineAllocator : public FAllocatorInterface
if (Num <= NumInline) return NumInline;
return Impl->CalculateSlackShrink(Num, NumAllocated);
return Secondary.CalculateSlackShrink(Num, NumAllocated);
}
NODISCARD FORCEINLINE size_t CalculateSlackReserve(size_t Num) const
{
if (Num <= NumInline) return NumInline;
return Impl->CalculateSlackReserve(Num);
return Secondary.CalculateSlackReserve(Num);
}
private:
ALLOCATOR_WRAPPER_BEGIN(SecondaryAllocator, Impl)
{
TAlignedStorage<sizeof(T), alignof(T)> InlineStorage[NumInline];
TAlignedStorage<sizeof(T), alignof(T)> InlineStorage[NumInline];
NODISCARD FORCEINLINE T* GetInline() { return reinterpret_cast< T*>(&InlineStorage); }
NODISCARD FORCEINLINE const T* GetInline() const { return reinterpret_cast<const T*>(&InlineStorage); }
}
ALLOCATOR_WRAPPER_END(SecondaryAllocator, Impl)
typename SecondaryAllocator::template ForElementType<T> Secondary;
};
};