From 6b42dbdc05123c7841d8da8325ca851094e68f2a Mon Sep 17 00:00:00 2001 From: _Redstone_c_ Date: Thu, 15 Dec 2022 23:37:41 +0800 Subject: [PATCH] feat(memory): add template function overload for Memmove and Memcmp --- .../Source/Private/Testing/MemoryTesting.cpp | 11 ++++++++++- Redcraft.Utility/Source/Public/Memory/Memory.h | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp b/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp index 1c25135..c8ca6c0 100644 --- a/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp +++ b/Redcraft.Utility/Source/Private/Testing/MemoryTesting.cpp @@ -71,6 +71,10 @@ void TestMemoryBuffer() Memory::Memmove(PtrA, PtrA + 2, 6); always_check((TempA << 16) == TempB); + TempA = 0x0123456789ABCDEF; + Memory::Memmove(TempB, TempA); + always_check(TempB == TempA); + TempA = 1004; TempB = 1005; TempC = 1005; @@ -78,8 +82,13 @@ void TestMemoryBuffer() int32 ResultA = Memory::Memcmp(PtrA, PtrB, sizeof(int64)); int32 ResultB = Memory::Memcmp(PtrB, PtrC, sizeof(int64)); int32 ResultC = Memory::Memcmp(PtrC, PtrD, sizeof(int64)); - always_check((ResultA < 0) != (ResultB < 0)); + always_check((ResultA < 0) == (ResultC < 0)); always_check(ResultB == 0); + int32 ResultD = Memory::Memcmp(TempA, TempB); + int32 ResultE = Memory::Memcmp(TempB, TempC); + int32 ResultF = Memory::Memcmp(TempC, TempD); + always_check((ResultD < 0) == (ResultF < 0)); + always_check(ResultE == 0); Memory::Memset(PtrA, 0x3F, sizeof(int64)); always_check(TempA == 0x3F3F3F3F3F3F3F3F); diff --git a/Redcraft.Utility/Source/Public/Memory/Memory.h b/Redcraft.Utility/Source/Public/Memory/Memory.h index 471269f..784c062 100644 --- a/Redcraft.Utility/Source/Public/Memory/Memory.h +++ b/Redcraft.Utility/Source/Public/Memory/Memory.h @@ -53,6 +53,20 @@ FORCEINLINE void* Memcpy(void* Destination, const void* Source, size_t Count) return std::memcpy(Destination, Source, Count); } +template +FORCEINLINE void Memmove(T& Destination, const T& Source) +{ + static_assert(!CPointer, "For pointers use the three parameters function"); + Memmove(&Destination, &Source, sizeof(T)); +} + +template +FORCEINLINE int32 Memcmp(const T& BufferLHS, const T& BufferRHS) +{ + static_assert(!CPointer, "For pointers use the three parameters function"); + return Memcmp(&BufferLHS, &BufferRHS, sizeof(T)); +} + template FORCEINLINE void Memset(T& Source, uint8 ValueToSet) {