From b8345246dcc2121bcb6d1515a9341789de20199f Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Sun, 27 Oct 2024 09:04:05 -0500 Subject: First crack at file objects. --- include/fud_memory.hpp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'include/fud_memory.hpp') diff --git a/include/fud_memory.hpp b/include/fud_memory.hpp index 6ce6312..6f6816f 100644 --- a/include/fud_memory.hpp +++ b/include/fud_memory.hpp @@ -73,6 +73,18 @@ constexpr void setMemory(Container& container, T&& value) } } +template +constexpr void zeroObject(T& object) +{ + static_assert(std::is_standard_layout_v); + static_assert(std::is_trivially_copyable_v); + + auto* objPtr = reinterpret_cast(&object); + for (size_t idx = 0; idx < sizeof(object); ++idx) { + objPtr[idx] = 0; + } +} + template void copyMem(T& destination, const U& source) { @@ -80,11 +92,16 @@ void copyMem(T& destination, const U& source) static_assert(Count <= sizeof(T)); static_assert(std::is_standard_layout_v); static_assert(std::is_standard_layout_v); + static_assert(std::is_trivially_copyable_v); + static_assert(std::is_trivially_copyable_v); + + // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) + auto* destPtr = reinterpret_cast(&destination); + const auto* srcPtr = reinterpret_cast(&source); + // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) for (size_t idx = 0; idx < Count; ++idx) { - // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) - reinterpret_cast(&destination)[idx] = reinterpret_cast(&source)[idx]; - // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) + destPtr[idx] = srcPtr[idx]; } } @@ -126,8 +143,7 @@ int compareMem(const T& lhs, U&& rhs) int difference = 0; for (size_t idx = 0; idx < Count; ++idx) { // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) - difference = reinterpret_cast(&lhs)[idx] - - reinterpret_cast(&uRhs)[idx]; + difference = reinterpret_cast(&lhs)[idx] - reinterpret_cast(&uRhs)[idx]; // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) if (difference != 0) { break; -- cgit v1.2.3