summaryrefslogtreecommitdiffhomepage
path: root/serialize
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-29 16:49:12 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-29 16:49:12 +0200
commit4c982b2ffe99f5dee70657424161de9541e00a21 (patch)
treefd46a497140aca5c4c6d9e994dca1daa09aa8ccc /serialize
parent32021bc251bf110338f28271e87964578cf07e1e (diff)
serialize: use std::bit_cast
Diffstat (limited to 'serialize')
-rw-r--r--serialize/binary-reader.hpp20
-rw-r--r--serialize/binary-reader.inl48
-rw-r--r--serialize/binary-serializer.cpp13
-rw-r--r--serialize/binary-serializer.hpp7
-rw-r--r--serialize/binary-writer.hpp3
-rw-r--r--serialize/binary-writer.inl111
6 files changed, 67 insertions, 135 deletions
diff --git a/serialize/binary-reader.hpp b/serialize/binary-reader.hpp
index 4876e0dc..2d09d51b 100644
--- a/serialize/binary-reader.hpp
+++ b/serialize/binary-reader.hpp
@@ -5,19 +5,6 @@
namespace floormat::Serialize {
-union alignas(alignof(double)) value_u {
- char bytes[8];
- unsigned char uc;
- std::uint8_t u8;
- std::uint16_t u16;
- std::uint32_t u32;
- std::uint64_t u64;
- float f32;
- double f64;
-};
-
-static_assert(sizeof(value_u) == 8);
-
template<typename T>
concept char_sequence = requires(T& x, const T& cx) {
requires std::same_as<decltype(std::begin(x)), decltype(std::end(x))>;
@@ -40,13 +27,10 @@ struct binary_reader final {
constexpr binary_reader(It begin, It end) noexcept;
constexpr void assert_end() noexcept;
- template<integer T> constexpr value_u read_u() noexcept;
- template<std::floating_point T> constexpr value_u read_u() noexcept;
- template<typename T> T read() noexcept;
+ template<serializable T> constexpr T read() noexcept;
template<std::size_t N> constexpr std::array<char, N> read() noexcept;
constexpr std::size_t bytes_read() const noexcept { return num_bytes_read; }
- template<std::size_t Max>
- auto read_asciiz_string() noexcept;
+ template<std::size_t Max> constexpr auto read_asciiz_string() noexcept;
private:
std::size_t num_bytes_read = 0;
diff --git a/serialize/binary-reader.inl b/serialize/binary-reader.inl
index 0c5b8531..b190f463 100644
--- a/serialize/binary-reader.inl
+++ b/serialize/binary-reader.inl
@@ -16,24 +16,16 @@ constexpr binary_reader<It>::binary_reader(It begin, It end) noexcept :
{}
template<string_input_iterator It>
-template<std::floating_point T>
-constexpr value_u binary_reader<It>::read_u() noexcept
+template<serializable T>
+constexpr T binary_reader<It>::read() noexcept
{
- value_u buf;
- static_assert(sizeof(T) <= sizeof(buf));
- fm_assert(std::distance(it, end) >= sizeof(T));
- num_bytes_read += sizeof(T);
- for (int i = 0; i < sizeof(T); i++)
- buf.bytes[i] = *it++;
- return buf;
-}
-
-template<string_input_iterator It>
-template<typename T>
-T binary_reader<It>::read() noexcept
-{
- value_u buf = read_u<T>();
- return *reinterpret_cast<T*>(buf.bytes);
+ constexpr std::size_t N = sizeof(T);
+ fm_assert((std::ptrdiff_t)N <= std::distance(it, end));
+ num_bytes_read += N;
+ char buf[N];
+ for (std::size_t i = 0; i < N; i++)
+ buf[i] = *it++;
+ return maybe_byteswap(std::bit_cast<T, decltype(buf)>(buf));
}
template<string_input_iterator It>
@@ -56,26 +48,6 @@ constexpr void binary_reader<It>::assert_end() noexcept
fm_assert(it == end);
}
-template<string_input_iterator It>
-template<integer T>
-constexpr value_u binary_reader<It>::read_u() noexcept
-{
- value_u buf;
- if (std::is_constant_evaluated())
- for (std::size_t i = 0; i < std::size(buf.bytes); i++)
- buf.bytes[i] = 0;
- static_assert(sizeof(T) <= sizeof(buf));
- fm_assert((std::ptrdiff_t)sizeof(T) <= std::distance(it, end));
- num_bytes_read += sizeof(T);
- if constexpr(std::endian::native == std::endian::big)
- for (int i = sizeof(T) - 1; i >= 0; i--)
- buf.bytes[i] = *it++;
- else
- for (std::size_t i = 0; i < sizeof(T); i++)
- buf.bytes[i] = *it++;
- return buf;
-}
-
template<string_input_iterator It, serializable T>
binary_reader<It>& operator>>(binary_reader<It>& reader, T& x) noexcept
{
@@ -85,7 +57,7 @@ binary_reader<It>& operator>>(binary_reader<It>& reader, T& x) noexcept
template<string_input_iterator It>
template<std::size_t MAX>
-auto binary_reader<It>::read_asciiz_string() noexcept
+constexpr auto binary_reader<It>::read_asciiz_string() noexcept
{
static_assert(MAX > 0);
diff --git a/serialize/binary-serializer.cpp b/serialize/binary-serializer.cpp
index d632febd..682ae2f5 100644
--- a/serialize/binary-serializer.cpp
+++ b/serialize/binary-serializer.cpp
@@ -34,23 +34,20 @@ template struct byte_array_iterator<sizeof(double)>;
[[maybe_unused]]
static constexpr bool test1()
{
- constexpr std::array<char, 4> bytes = { 1, 0, 1, 0 };
+ constexpr std::array<char, 4> bytes = { 1, 2, 3, 4 };
auto x = binary_reader(bytes.cbegin(), bytes.cend());
- return x.read_u<unsigned char>().bytes[0] == 1 &&
- x.read_u<unsigned char>().bytes[0] == 0 &&
- x.read_u<unsigned char>().bytes[0] == 1 &&
- x.read_u<unsigned char>().bytes[0] == 0;
+ return x.read<std::uint32_t>() == 67305985;
}
static_assert(test1());
[[maybe_unused]]
static constexpr bool test2()
{
- constexpr std::array<char, 4> bytes = { 1, 0, 1, 0 };
+ constexpr std::array<char, 4> bytes = { 4, 3, 2, 1 };
auto r = binary_reader(bytes.cbegin(), bytes.cend());
- const auto x = r.read_u<int>();
+ const auto x = r.read<std::uint32_t>();
r.assert_end();
- return x.bytes[0] == 1 && x.bytes[1] == 0 && x.bytes[2] == 1 && x.bytes[3] == 0;
+ return x == 16909060;
}
static_assert(test2());
diff --git a/serialize/binary-serializer.hpp b/serialize/binary-serializer.hpp
index 43fffea8..66281cc1 100644
--- a/serialize/binary-serializer.hpp
+++ b/serialize/binary-serializer.hpp
@@ -41,9 +41,16 @@ concept integer = requires(T x) {
template<typename T>
concept serializable = requires(T x) {
+ requires std::same_as<T, std::decay_t<T>>;
requires std::floating_point<T> || integer<T>;
};
+template<typename T>
+constexpr inline T maybe_byteswap(T x)
+{
+ return x;
+}
+
template<integer T>
constexpr inline T maybe_byteswap(T x)
{
diff --git a/serialize/binary-writer.hpp b/serialize/binary-writer.hpp
index 02fe427d..13580eb0 100644
--- a/serialize/binary-writer.hpp
+++ b/serialize/binary-writer.hpp
@@ -8,8 +8,7 @@ namespace floormat::Serialize {
template<std::output_iterator<char> It>
struct binary_writer final {
explicit constexpr binary_writer(It it) noexcept;
- template<integer T> constexpr void write(T x) noexcept;
- template<std::floating_point T> void write(T x) noexcept;
+ template<serializable T> constexpr void write(T x) noexcept;
constexpr void write_asciiz_string(StringView str) noexcept;
constexpr std::size_t bytes_written() const noexcept { return _bytes_written; }
diff --git a/serialize/binary-writer.inl b/serialize/binary-writer.inl
index 5fb4db6d..e5a7ba16 100644
--- a/serialize/binary-writer.inl
+++ b/serialize/binary-writer.inl
@@ -1,69 +1,42 @@
-#pragma once
-#include "binary-writer.hpp"
-#include "binary-serializer.hpp"
-#include "compat/assert.hpp"
-#include <type_traits>
-#include <Corrade/Containers/StringView.h>
-
-namespace floormat::Serialize {
-
-template<std::output_iterator<char> It>
-constexpr binary_writer<It>::binary_writer(It it) noexcept : it{it}, _bytes_written{0} {}
-
-template<std::output_iterator<char> It>
-template<integer T>
-constexpr void binary_writer<It>::write(T x) noexcept
-{
- union {
- T datum;
- char bytes[sizeof(T)];
- } buf;
-
- if (std::is_constant_evaluated())
- for (std::size_t i = 0; i < std::size(buf.bytes); i++)
- buf.bytes[i] = 0;
- _bytes_written += sizeof(T);
- if constexpr(sizeof(T) == 1)
- buf.bytes[0] = (char)x;
- else if (!std::is_constant_evaluated())
- buf.datum = maybe_byteswap(x);
- else
- for (std::size_t i = 0; i < sizeof(T); x >>= 8, i++)
- buf.bytes[i] = (char)(unsigned char)x;
- for (std::size_t i = 0; i < sizeof(T); i++)
- *it++ = buf.bytes[i];
-}
-
-template<std::output_iterator<char> It>
-template<std::floating_point T>
-void binary_writer<It>::write(T x) noexcept
-{
- union {
- T datum;
- char bytes[sizeof(T)];
- } buf;
- _bytes_written += sizeof(T);
- buf.datum = maybe_byteswap(x);
- for (std::size_t i = 0; i < sizeof(T); i++)
- *it++ = buf.bytes[i];
-}
-
-template<std::output_iterator<char> It, serializable T>
-constexpr binary_writer<It>& operator<<(binary_writer<It>& writer, T x) noexcept
-{
- writer.template write<T>(x);
- return writer;
-}
-
-template<std::output_iterator<char> It>
-constexpr void binary_writer<It>::write_asciiz_string(StringView str) noexcept
-{
- fm_debug_assert(str.flags() & StringViewFlag::NullTerminated);
- const auto sz = str.size();
- _bytes_written += sz + 1;
- for (std::size_t i = 0; i < sz; i++)
- *it++ = str[i];
- *it++ = '\0';
-}
-
-} // namespace floormat::Serialize
+#pragma once
+#include "binary-writer.hpp"
+#include "binary-serializer.hpp"
+#include "compat/assert.hpp"
+#include <type_traits>
+#include <Corrade/Containers/StringView.h>
+
+namespace floormat::Serialize {
+
+template<std::output_iterator<char> It>
+constexpr binary_writer<It>::binary_writer(It it) noexcept : it{it}, _bytes_written{0} {}
+
+template<std::output_iterator<char> It>
+template<serializable T>
+constexpr void binary_writer<It>::write(T x) noexcept
+{
+ _bytes_written += sizeof(T);
+ constexpr std::size_t N = sizeof(T);
+ const auto buf = std::bit_cast<std::array<char, N>, T>(maybe_byteswap(x));
+ for (std::size_t i = 0; i < N; i++)
+ *it++ = buf[i];
+}
+
+template<std::output_iterator<char> It, serializable T>
+constexpr binary_writer<It>& operator<<(binary_writer<It>& writer, T x) noexcept
+{
+ writer.template write<T>(x);
+ return writer;
+}
+
+template<std::output_iterator<char> It>
+constexpr void binary_writer<It>::write_asciiz_string(StringView str) noexcept
+{
+ fm_debug_assert(str.flags() & StringViewFlag::NullTerminated);
+ const auto sz = str.size();
+ _bytes_written += sz + 1;
+ for (std::size_t i = 0; i < sz; i++)
+ *it++ = str[i];
+ *it++ = '\0';
+}
+
+} // namespace floormat::Serialize