From 23ad1b2b9fef5fd86bbb84ff97ea47168ddf75c0 Mon Sep 17 00:00:00 2001 From: elsid Date: Mon, 8 Nov 2021 19:25:42 +0100 Subject: [PATCH] Move enum related serialization logic from format to visitors --- components/serialization/binaryreader.hpp | 18 +++++++++++------- components/serialization/binarywriter.hpp | 14 +++++++++----- components/serialization/format.hpp | 16 +--------------- components/serialization/sizeaccumulator.hpp | 4 ++-- 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/components/serialization/binaryreader.hpp b/components/serialization/binaryreader.hpp index 65b1e2f9d8..c1a9031676 100644 --- a/components/serialization/binaryreader.hpp +++ b/components/serialization/binaryreader.hpp @@ -23,12 +23,14 @@ namespace Serialization template void operator()(Format&& format, T& value) { - if constexpr (std::is_arithmetic_v) + if constexpr (std::is_enum_v) + (*this)(std::forward(format), static_cast&>(value)); + else if constexpr (std::is_arithmetic_v) { - if (mEnd - mPos < static_cast(sizeof(value))) + if (mEnd - mPos < static_cast(sizeof(T))) throw std::runtime_error("Not enough data"); - std::memcpy(&value, mPos, sizeof(value)); - mPos += sizeof(value); + std::memcpy(&value, mPos, sizeof(T)); + mPos += sizeof(T); } else { @@ -39,11 +41,13 @@ namespace Serialization template auto operator()(Format&& format, T* data, std::size_t count) { - if constexpr (std::is_arithmetic_v) + if constexpr (std::is_enum_v) + (*this)(std::forward(format), reinterpret_cast*>(data), count); + else if constexpr (std::is_arithmetic_v) { - if (mEnd - mPos < static_cast(count * sizeof(T))) - throw std::runtime_error("Not enough data"); const std::size_t size = sizeof(T) * count; + if (mEnd - mPos < static_cast(size)) + throw std::runtime_error("Not enough data"); std::memcpy(data, mPos, size); mPos += size; } diff --git a/components/serialization/binarywriter.hpp b/components/serialization/binarywriter.hpp index de549c7e02..5250bcfdf2 100644 --- a/components/serialization/binarywriter.hpp +++ b/components/serialization/binarywriter.hpp @@ -23,12 +23,14 @@ namespace Serialization template void operator()(Format&& format, const T& value) { - if constexpr (std::is_arithmetic_v) + if constexpr (std::is_enum_v) + (*this)(std::forward(format), static_cast>(value)); + else if constexpr (std::is_arithmetic_v) { - if (mEnd - mDest < static_cast(sizeof(value))) + if (mEnd - mDest < static_cast(sizeof(T))) throw std::runtime_error("Not enough space"); - std::memcpy(mDest, &value, sizeof(value)); - mDest += sizeof(value); + std::memcpy(mDest, &value, sizeof(T)); + mDest += sizeof(T); } else { @@ -39,7 +41,9 @@ namespace Serialization template auto operator()(Format&& format, const T* data, std::size_t count) { - if constexpr (std::is_arithmetic_v) + if constexpr (std::is_enum_v) + (*this)(std::forward(format), reinterpret_cast*>(data), count); + else if constexpr (std::is_arithmetic_v) { const std::size_t size = sizeof(T) * count; if (mEnd - mDest < static_cast(size)) diff --git a/components/serialization/format.hpp b/components/serialization/format.hpp index 5d287e80cf..595afd0dad 100644 --- a/components/serialization/format.hpp +++ b/components/serialization/format.hpp @@ -34,24 +34,10 @@ namespace Serialization template void operator()(Visitor&& visitor, T* data, std::size_t size) const { - if constexpr (std::is_arithmetic_v) - { + if constexpr (std::is_arithmetic_v || std::is_enum_v) visitor(self(), data, size); - } - else if constexpr (std::is_enum_v) - { - if constexpr (mode == Mode::Write) - visitor(self(), reinterpret_cast*>(data), size); - else - { - static_assert(mode == Mode::Read); - visitor(self(), reinterpret_cast*>(data), size); - } - } else - { std::for_each(data, data + size, [&] (auto& v) { visitor(self(), v); }); - } } template diff --git a/components/serialization/sizeaccumulator.hpp b/components/serialization/sizeaccumulator.hpp index 2386fe7bea..4dcc004f73 100644 --- a/components/serialization/sizeaccumulator.hpp +++ b/components/serialization/sizeaccumulator.hpp @@ -18,7 +18,7 @@ namespace Serialization template void operator()(Format&& format, const T& value) { - if constexpr (std::is_arithmetic_v) + if constexpr (std::is_arithmetic_v || std::is_enum_v) mValue += sizeof(T); else format(*this, value); @@ -27,7 +27,7 @@ namespace Serialization template auto operator()(Format&& format, const T* data, std::size_t count) { - if constexpr (std::is_arithmetic_v) + if constexpr (std::is_arithmetic_v || std::is_enum_v) mValue += count * sizeof(T); else format(*this, data, count);