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-21 14:39:22 +00:00
|
|
|
if constexpr (!CTriviallyDefaultConstructible<ElementType>)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
2022-05-21 14:39:22 +00:00
|
|
|
ElementType* Element = Address;
|
2022-03-19 08:05:47 +00:00
|
|
|
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-21 14:39:22 +00:00
|
|
|
if constexpr (CTriviallyConstructibleFrom<DestinationElementType, const SourceElementType> && sizeof(DestinationElementType) == sizeof(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-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-21 14:39:22 +00:00
|
|
|
if constexpr (CTriviallyMoveAssignable<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-21 14:39:22 +00:00
|
|
|
requires CDestructible<ElementType>
|
|
|
|
FORCEINLINE void Destruct(ElementType* Element, size_t Count = 1)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
2022-05-21 14:39:22 +00:00
|
|
|
if constexpr (!CTriviallyDestructible<ElementType>)
|
2022-03-19 08:05:47 +00:00
|
|
|
{
|
|
|
|
while (Count)
|
|
|
|
{
|
2022-05-21 14:39:22 +00:00
|
|
|
Element->~ElementType();
|
|
|
|
++Element;
|
2022-03-19 08:05:47 +00:00
|
|
|
--Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END(Memory)
|
|
|
|
|
|
|
|
NAMESPACE_MODULE_END(Utility)
|
|
|
|
NAMESPACE_MODULE_END(Redcraft)
|
|
|
|
NAMESPACE_REDCRAFT_END
|