diff --git a/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp b/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp index 39f2ef3..fc80494 100644 --- a/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp +++ b/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp @@ -146,10 +146,10 @@ NAMESPACE_UNNAMED_BEGIN struct FTracker { static int32 Status; - FTracker() { always_check(Status == 0); Status = -1; } - FTracker(const FTracker&) { always_check(Status == 1); Status = -1; } - FTracker(FTracker&&) { always_check(Status == 2); Status = -1; } - ~FTracker() { always_check(Status == 3); Status = -1; } + FTracker() { always_check(Status == 0); Status = -1; } + FTracker(const FTracker&) { always_check(Status == 1); Status = -1; } + FTracker(FTracker&&) { always_check(Status == 2); Status = -1; } + ~FTracker() { always_check(Status == 3); Status = -1; } FTracker& operator=(const FTracker&) { always_check(Status == 4); Status = -1; return *this; } FTracker& operator=(FTracker&&) { always_check(Status == 5); Status = -1; return *this; } friend bool operator==(const FTracker&, const FTracker&) { always_check(Status == 6); Status = -1; return true; } @@ -173,6 +173,10 @@ void TestMemoryOperator() Memory::Construct(PtrA, PtrB); always_check(FTracker::Status == -1); + FTracker::Status = 1; + Memory::CopyConstruct(PtrA, PtrB); + always_check(FTracker::Status == -1); + FTracker::Status = 2; Memory::MoveConstruct(PtrA, PtrB); always_check(FTracker::Status == -1); diff --git a/Redcraft.Utility/Source/Public/Memory/MemoryOperator.h b/Redcraft.Utility/Source/Public/Memory/MemoryOperator.h index 17300d7..37c9a40 100644 --- a/Redcraft.Utility/Source/Public/Memory/MemoryOperator.h +++ b/Redcraft.Utility/Source/Public/Memory/MemoryOperator.h @@ -12,9 +12,9 @@ NAMESPACE_MODULE_BEGIN(Utility) NAMESPACE_BEGIN(Memory) -template +template requires (TIsDefaultConstructible::Value || TIsZeroConstructible::Value) -FORCEINLINE void DefaultConstruct(ElementType* Address, SizeType Count = 1) +FORCEINLINE void DefaultConstruct(ElementType* Address, size_t Count = 1) { if constexpr (TIsZeroConstructible::Value) { @@ -32,9 +32,9 @@ FORCEINLINE void DefaultConstruct(ElementType* Address, SizeType Count = 1) } } -template +template requires (TIsConstructible::Value || TIsBitwiseConstructible::Value) -FORCEINLINE void Construct(DestinationElementType* Destination, const SourceElementType* Source, SizeType Count = 1) +FORCEINLINE void Construct(DestinationElementType* Destination, const SourceElementType* Source, size_t Count = 1) { if constexpr (TIsBitwiseConstructible::Value) { @@ -52,9 +52,29 @@ FORCEINLINE void Construct(DestinationElementType* Destination, const SourceElem } } -template +template + requires (TIsCopyConstructible::Value) +FORCEINLINE void CopyConstruct(ElementType* Destination, const ElementType* Source, size_t Count = 1) +{ + if constexpr (TIsTriviallyCopyConstructible::Value) + { + Memory::Memcpy(Destination, Source, sizeof(ElementType) * Count); + } + else + { + while (Count) + { + new (Destination) ElementType(*Source); + ++(ElementType*&)Destination; + ++Source; + --Count; + } + } +} + +template requires (TIsMoveConstructible::Value) -FORCEINLINE void MoveConstruct(ElementType* Destination, ElementType* Source, SizeType Count = 1) +FORCEINLINE void MoveConstruct(ElementType* Destination, ElementType* Source, size_t Count = 1) { if constexpr (TIsTriviallyMoveConstructible::Value) { @@ -72,9 +92,9 @@ FORCEINLINE void MoveConstruct(ElementType* Destination, ElementType* Source, Si } } -template +template requires ((TIsConstructible::Value && TIsDestructible::Value) || TIsBitwiseRelocatable::Value) -FORCEINLINE void RelocateConstruct(DestinationElementType* Destination, SourceElementType* Source, SizeType Count = 1) +FORCEINLINE void RelocateConstruct(DestinationElementType* Destination, SourceElementType* Source, size_t Count = 1) { if constexpr (TIsBitwiseRelocatable::Value) { @@ -94,9 +114,9 @@ FORCEINLINE void RelocateConstruct(DestinationElementType* Destination, SourceEl } } -template +template requires (TIsDestructible::Value) -FORCEINLINE void Destruct(ElementType* Element, SizeType Count = 1) +FORCEINLINE void Destruct(ElementType* Element, size_t Count = 1) { if constexpr (!TIsTriviallyDestructible::Value) { @@ -111,9 +131,9 @@ FORCEINLINE void Destruct(ElementType* Element, SizeType Count = 1) } } -template +template requires (TIsCopyAssignable::Value) -FORCEINLINE void CopyAssign(ElementType* Destination, const ElementType* Source, SizeType Count = 1) +FORCEINLINE void CopyAssign(ElementType* Destination, const ElementType* Source, size_t Count = 1) { if constexpr (TIsTriviallyCopyAssignable::Value) { @@ -131,9 +151,9 @@ FORCEINLINE void CopyAssign(ElementType* Destination, const ElementType* Source, } } -template +template requires (TIsMoveAssignable::Value) -FORCEINLINE void MoveAssign(ElementType* Destination, ElementType* Source, SizeType Count = 1) +FORCEINLINE void MoveAssign(ElementType* Destination, ElementType* Source, size_t Count = 1) { if constexpr (TIsTriviallyCopyConstructible::Value) { @@ -151,9 +171,9 @@ FORCEINLINE void MoveAssign(ElementType* Destination, ElementType* Source, SizeT } } -template +template requires (CEqualityComparable || TIsBitwiseComparable::Value) -FORCEINLINE bool Compare(const ElementType* LHS, const ElementType* RHS, SizeType Count = 1) +FORCEINLINE bool Compare(const ElementType* LHS, const ElementType* RHS, size_t Count = 1) { if constexpr (TIsBitwiseComparable::Value) { @@ -163,10 +183,7 @@ FORCEINLINE bool Compare(const ElementType* LHS, const ElementType* RHS, SizeTyp { while (Count) { - if (!(*LHS == *RHS)) - { - return false; - } + if (!(*LHS == *RHS)) return false; ++LHS; ++RHS;