Move enum related serialization logic from format to visitors

pull/3225/head
elsid 3 years ago
parent 5325495f46
commit 23ad1b2b9f
No known key found for this signature in database
GPG Key ID: D27B8E8D10A2896B

@ -23,12 +23,14 @@ namespace Serialization
template <class Format, class T>
void operator()(Format&& format, T& value)
{
if constexpr (std::is_arithmetic_v<T>)
if constexpr (std::is_enum_v<T>)
(*this)(std::forward<Format>(format), static_cast<std::underlying_type_t<T>&>(value));
else if constexpr (std::is_arithmetic_v<T>)
{
if (mEnd - mPos < static_cast<std::ptrdiff_t>(sizeof(value)))
if (mEnd - mPos < static_cast<std::ptrdiff_t>(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 <class Format, class T>
auto operator()(Format&& format, T* data, std::size_t count)
{
if constexpr (std::is_arithmetic_v<T>)
if constexpr (std::is_enum_v<T>)
(*this)(std::forward<Format>(format), reinterpret_cast<std::underlying_type_t<T>*>(data), count);
else if constexpr (std::is_arithmetic_v<T>)
{
if (mEnd - mPos < static_cast<std::ptrdiff_t>(count * sizeof(T)))
throw std::runtime_error("Not enough data");
const std::size_t size = sizeof(T) * count;
if (mEnd - mPos < static_cast<std::ptrdiff_t>(size))
throw std::runtime_error("Not enough data");
std::memcpy(data, mPos, size);
mPos += size;
}

@ -23,12 +23,14 @@ namespace Serialization
template <class Format, class T>
void operator()(Format&& format, const T& value)
{
if constexpr (std::is_arithmetic_v<T>)
if constexpr (std::is_enum_v<T>)
(*this)(std::forward<Format>(format), static_cast<std::underlying_type_t<T>>(value));
else if constexpr (std::is_arithmetic_v<T>)
{
if (mEnd - mDest < static_cast<std::ptrdiff_t>(sizeof(value)))
if (mEnd - mDest < static_cast<std::ptrdiff_t>(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 <class Format, class T>
auto operator()(Format&& format, const T* data, std::size_t count)
{
if constexpr (std::is_arithmetic_v<T>)
if constexpr (std::is_enum_v<T>)
(*this)(std::forward<Format>(format), reinterpret_cast<const std::underlying_type_t<T>*>(data), count);
else if constexpr (std::is_arithmetic_v<T>)
{
const std::size_t size = sizeof(T) * count;
if (mEnd - mDest < static_cast<std::ptrdiff_t>(size))

@ -34,24 +34,10 @@ namespace Serialization
template <class Visitor, class T>
void operator()(Visitor&& visitor, T* data, std::size_t size) const
{
if constexpr (std::is_arithmetic_v<T>)
{
if constexpr (std::is_arithmetic_v<T> || std::is_enum_v<T>)
visitor(self(), data, size);
}
else if constexpr (std::is_enum_v<T>)
{
if constexpr (mode == Mode::Write)
visitor(self(), reinterpret_cast<const std::underlying_type_t<T>*>(data), size);
else
{
static_assert(mode == Mode::Read);
visitor(self(), reinterpret_cast<std::underlying_type_t<T>*>(data), size);
}
}
else
{
std::for_each(data, data + size, [&] (auto& v) { visitor(self(), v); });
}
}
template <class Visitor, class T, std::size_t size>

@ -18,7 +18,7 @@ namespace Serialization
template <class Format, class T>
void operator()(Format&& format, const T& value)
{
if constexpr (std::is_arithmetic_v<T>)
if constexpr (std::is_arithmetic_v<T> || std::is_enum_v<T>)
mValue += sizeof(T);
else
format(*this, value);
@ -27,7 +27,7 @@ namespace Serialization
template <class Format, class T>
auto operator()(Format&& format, const T* data, std::size_t count)
{
if constexpr (std::is_arithmetic_v<T>)
if constexpr (std::is_arithmetic_v<T> || std::is_enum_v<T>)
mValue += count * sizeof(T);
else
format(*this, data, count);

Loading…
Cancel
Save