diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-27 09:04:05 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-27 09:04:05 -0500 |
commit | b8345246dcc2121bcb6d1515a9341789de20199f (patch) | |
tree | 4a25857512a90ff38e8a40166c54694b74920216 /include/fud_memory.hpp | |
parent | f84b8259f6e980fed647d8e1ec0634f89ee59c06 (diff) |
First crack at file objects.
Diffstat (limited to 'include/fud_memory.hpp')
-rw-r--r-- | include/fud_memory.hpp | 26 |
1 files changed, 21 insertions, 5 deletions
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<Size>& container, T&& value) } } +template <typename T> +constexpr void zeroObject(T& object) +{ + static_assert(std::is_standard_layout_v<T>); + static_assert(std::is_trivially_copyable_v<T>); + + auto* objPtr = reinterpret_cast<uint8_t*>(&object); + for (size_t idx = 0; idx < sizeof(object); ++idx) { + objPtr[idx] = 0; + } +} + template <size_t Count, typename T, typename U> 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<T>); static_assert(std::is_standard_layout_v<U>); + static_assert(std::is_trivially_copyable_v<T>); + static_assert(std::is_trivially_copyable_v<U>); + + // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) + auto* destPtr = reinterpret_cast<char*>(&destination); + const auto* srcPtr = reinterpret_cast<const char*>(&source); + // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) for (size_t idx = 0; idx < Count; ++idx) { - // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) - reinterpret_cast<char*>(&destination)[idx] = reinterpret_cast<const char*>(&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<const uint8_t*>(&lhs)[idx] - - reinterpret_cast<const uint8_t*>(&uRhs)[idx]; + difference = reinterpret_cast<const uint8_t*>(&lhs)[idx] - reinterpret_cast<const uint8_t*>(&uRhs)[idx]; // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) if (difference != 0) { break; |