mirror of
https://github.com/OpenMW/openmw.git
synced 2025-06-20 21:11:33 +00:00
Move enum related serialization logic from format to visitors
This commit is contained in:
parent
5325495f46
commit
23ad1b2b9f
4 changed files with 23 additions and 29 deletions
|
@ -23,12 +23,14 @@ namespace Serialization
|
||||||
template <class Format, class T>
|
template <class Format, class T>
|
||||||
void operator()(Format&& format, T& value)
|
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");
|
throw std::runtime_error("Not enough data");
|
||||||
std::memcpy(&value, mPos, sizeof(value));
|
std::memcpy(&value, mPos, sizeof(T));
|
||||||
mPos += sizeof(value);
|
mPos += sizeof(T);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -39,11 +41,13 @@ namespace Serialization
|
||||||
template <class Format, class T>
|
template <class Format, class T>
|
||||||
auto operator()(Format&& format, T* data, std::size_t count)
|
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;
|
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);
|
std::memcpy(data, mPos, size);
|
||||||
mPos += size;
|
mPos += size;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,12 +23,14 @@ namespace Serialization
|
||||||
template <class Format, class T>
|
template <class Format, class T>
|
||||||
void operator()(Format&& format, const T& value)
|
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");
|
throw std::runtime_error("Not enough space");
|
||||||
std::memcpy(mDest, &value, sizeof(value));
|
std::memcpy(mDest, &value, sizeof(T));
|
||||||
mDest += sizeof(value);
|
mDest += sizeof(T);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -39,7 +41,9 @@ namespace Serialization
|
||||||
template <class Format, class T>
|
template <class Format, class T>
|
||||||
auto operator()(Format&& format, const T* data, std::size_t count)
|
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;
|
const std::size_t size = sizeof(T) * count;
|
||||||
if (mEnd - mDest < static_cast<std::ptrdiff_t>(size))
|
if (mEnd - mDest < static_cast<std::ptrdiff_t>(size))
|
||||||
|
|
|
@ -34,25 +34,11 @@ namespace Serialization
|
||||||
template <class Visitor, class T>
|
template <class Visitor, class T>
|
||||||
void operator()(Visitor&& visitor, T* data, std::size_t size) const
|
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);
|
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
|
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); });
|
std::for_each(data, data + size, [&] (auto& v) { visitor(self(), v); });
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
template <class Visitor, class T, std::size_t size>
|
template <class Visitor, class T, std::size_t size>
|
||||||
void operator()(Visitor&& visitor, T(& data)[size]) const
|
void operator()(Visitor&& visitor, T(& data)[size]) const
|
||||||
|
|
|
@ -18,7 +18,7 @@ namespace Serialization
|
||||||
template <class Format, class T>
|
template <class Format, class T>
|
||||||
void operator()(Format&& format, const T& value)
|
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);
|
mValue += sizeof(T);
|
||||||
else
|
else
|
||||||
format(*this, value);
|
format(*this, value);
|
||||||
|
@ -27,7 +27,7 @@ namespace Serialization
|
||||||
template <class Format, class T>
|
template <class Format, class T>
|
||||||
auto operator()(Format&& format, const T* data, std::size_t count)
|
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);
|
mValue += count * sizeof(T);
|
||||||
else
|
else
|
||||||
format(*this, data, count);
|
format(*this, data, count);
|
||||||
|
|
Loading…
Reference in a new issue