2022-03-19 08:05:47 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "CoreTypes.h"
|
2022-03-19 15:07:04 +00:00
|
|
|
#include "Memory/Memory.h"
|
2022-03-19 08:05:47 +00:00
|
|
|
#include "Templates/Utility.h"
|
|
|
|
#include "TypeTraits/TypeTraits.h"
|
|
|
|
|
|
|
|
NAMESPACE_REDCRAFT_BEGIN
|
|
|
|
NAMESPACE_MODULE_BEGIN(Redcraft)
|
|
|
|
NAMESPACE_MODULE_BEGIN(Utility)
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(Memory)
|
|
|
|
|
2022-04-30 13:33:18 +00:00
|
|
|
template <typename ElementType>
|
2022-05-16 14:42:17 +00:00
|
|
|
requires CDefaultConstructible<ElementType>
|
2022-04-30 13:33:18 +00:00
|
|
|
FORCEINLINE void DefaultConstruct(ElementType* Address, size_t Count = 1)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
2022-05-20 15:35:36 +00:00
|
|
|
if constexpr (CZeroConstructible<ElementType>)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
|
|
|
Memory::Memset(Address, 0, sizeof(ElementType) * Count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ElementType* Element = (ElementType*)Address;
|
|
|
|
while (Count)
|
|
|
|
{
|
|
|
|
new (Element) ElementType;
|
|
|
|
++Element;
|
|
|
|
--Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 15:35:36 +00:00
|
|
|
template <typename DestinationElementType, typename SourceElementType = DestinationElementType>
|
|
|
|
requires CConstructibleFrom<DestinationElementType, const SourceElementType&>
|
2022-04-30 13:33:18 +00:00
|
|
|
FORCEINLINE void Construct(DestinationElementType* Destination, const SourceElementType* Source, size_t Count = 1)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
2022-05-20 15:35:36 +00:00
|
|
|
if constexpr (CBitwiseConstructible<DestinationElementType, const SourceElementType>)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
|
|
|
Memory::Memcpy(Destination, Source, sizeof(SourceElementType) * Count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (Count)
|
|
|
|
{
|
|
|
|
new (Destination) DestinationElementType(*Source);
|
|
|
|
++(DestinationElementType*&)Destination;
|
|
|
|
++Source;
|
|
|
|
--Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 13:33:18 +00:00
|
|
|
template <typename ElementType>
|
2022-05-16 14:42:17 +00:00
|
|
|
requires CCopyConstructible<ElementType>
|
2022-04-30 13:33:18 +00:00
|
|
|
FORCEINLINE void CopyConstruct(ElementType* Destination, const ElementType* Source, size_t Count = 1)
|
|
|
|
{
|
2022-05-16 14:42:17 +00:00
|
|
|
if constexpr (CTriviallyCopyConstructible<ElementType>)
|
2022-04-30 13:33:18 +00:00
|
|
|
{
|
|
|
|
Memory::Memcpy(Destination, Source, sizeof(ElementType) * Count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (Count)
|
|
|
|
{
|
|
|
|
new (Destination) ElementType(*Source);
|
|
|
|
++(ElementType*&)Destination;
|
|
|
|
++Source;
|
|
|
|
--Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ElementType>
|
2022-05-16 14:42:17 +00:00
|
|
|
requires CMoveConstructible<ElementType>
|
2022-04-30 13:33:18 +00:00
|
|
|
FORCEINLINE void MoveConstruct(ElementType* Destination, ElementType* Source, size_t Count = 1)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
2022-05-16 14:42:17 +00:00
|
|
|
if constexpr (CTriviallyMoveConstructible<ElementType>)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
|
|
|
Memory::Memmove(Destination, Source, sizeof(ElementType) * Count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (Count)
|
|
|
|
{
|
|
|
|
new (Destination) ElementType(MoveTemp(*Source));
|
|
|
|
++(ElementType*&)Destination;
|
|
|
|
++Source;
|
|
|
|
--Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 15:35:36 +00:00
|
|
|
template <typename DestinationElementType, typename SourceElementType = DestinationElementType>
|
|
|
|
requires CConstructibleFrom<DestinationElementType, SourceElementType&&> && CDestructible<SourceElementType>
|
2022-04-30 13:33:18 +00:00
|
|
|
FORCEINLINE void RelocateConstruct(DestinationElementType* Destination, SourceElementType* Source, size_t Count = 1)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
2022-05-20 15:35:36 +00:00
|
|
|
if constexpr (CBitwiseRelocatable<DestinationElementType, SourceElementType>)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
|
|
|
Memory::Memmove(Destination, Source, sizeof(SourceElementType) * Count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (Count)
|
|
|
|
{
|
|
|
|
typedef SourceElementType RelocateConstructItemsElementTypeTypedef;
|
|
|
|
|
|
|
|
new (Destination) DestinationElementType(MoveTemp(*Source));
|
|
|
|
++(DestinationElementType*&)Destination;
|
|
|
|
(Source++)->RelocateConstructItemsElementTypeTypedef::~RelocateConstructItemsElementTypeTypedef();
|
|
|
|
--Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 13:33:18 +00:00
|
|
|
template <typename ElementType>
|
2022-05-16 14:42:17 +00:00
|
|
|
requires CDestructible<ElementType>
|
2022-04-30 13:33:18 +00:00
|
|
|
FORCEINLINE void Destruct(ElementType* Element, size_t Count = 1)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
2022-05-16 14:42:17 +00:00
|
|
|
if constexpr (!CTriviallyDestructible<ElementType>)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
|
|
|
while (Count)
|
|
|
|
{
|
2022-05-15 06:18:39 +00:00
|
|
|
Element->~ElementType();
|
2022-03-19 08:05:47 +00:00
|
|
|
++Element;
|
|
|
|
--Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 13:33:18 +00:00
|
|
|
template <typename ElementType>
|
2022-05-16 14:42:17 +00:00
|
|
|
requires CCopyAssignable<ElementType>
|
2022-04-30 13:33:18 +00:00
|
|
|
FORCEINLINE void CopyAssign(ElementType* Destination, const ElementType* Source, size_t Count = 1)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
2022-05-16 14:42:17 +00:00
|
|
|
if constexpr (CTriviallyCopyAssignable<ElementType>)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
|
|
|
Memory::Memcpy(Destination, Source, sizeof(ElementType) * Count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (Count)
|
|
|
|
{
|
|
|
|
*Destination = *Source;
|
|
|
|
++Destination;
|
|
|
|
++Source;
|
|
|
|
--Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 13:33:18 +00:00
|
|
|
template <typename ElementType>
|
2022-05-16 14:42:17 +00:00
|
|
|
requires CMoveAssignable<ElementType>
|
2022-04-30 13:33:18 +00:00
|
|
|
FORCEINLINE void MoveAssign(ElementType* Destination, ElementType* Source, size_t Count = 1)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
2022-05-16 14:42:17 +00:00
|
|
|
if constexpr (CTriviallyCopyConstructible<ElementType>)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
|
|
|
Memory::Memmove(Destination, Source, sizeof(ElementType) * Count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (Count)
|
|
|
|
{
|
|
|
|
*Destination = MoveTemp(*Source);
|
|
|
|
++(ElementType*&)Destination;
|
|
|
|
++Source;
|
|
|
|
--Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 13:33:18 +00:00
|
|
|
template <typename ElementType>
|
2022-05-12 15:36:32 +00:00
|
|
|
requires CEqualityComparable<ElementType>
|
2022-04-30 13:33:18 +00:00
|
|
|
FORCEINLINE bool Compare(const ElementType* LHS, const ElementType* RHS, size_t Count = 1)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
2022-05-20 15:35:36 +00:00
|
|
|
if constexpr (CBitwiseComparable<ElementType>)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
|
|
|
return !Memory::Memcmp(LHS, RHS, sizeof(ElementType) * Count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (Count)
|
|
|
|
{
|
2022-04-30 13:33:18 +00:00
|
|
|
if (!(*LHS == *RHS)) return false;
|
2022-03-19 08:05:47 +00:00
|
|
|
|
|
|
|
++LHS;
|
|
|
|
++RHS;
|
|
|
|
--Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END(Memory)
|
|
|
|
|
|
|
|
NAMESPACE_MODULE_END(Utility)
|
|
|
|
NAMESPACE_MODULE_END(Redcraft)
|
|
|
|
NAMESPACE_REDCRAFT_END
|