From f544cc4c469bd3073e6d78ca05ee70475dfa20cc Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 27 Oct 2022 19:36:32 +0200 Subject: more serializer work --- serialize/binary-serializer.inl | 58 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) (limited to 'serialize/binary-serializer.inl') diff --git a/serialize/binary-serializer.inl b/serialize/binary-serializer.inl index 9e7e58fa..200622f4 100644 --- a/serialize/binary-serializer.inl +++ b/serialize/binary-serializer.inl @@ -4,7 +4,18 @@ namespace floormat::Serialize { -template +template +template +constexpr binary_reader::binary_reader(const Seq& seq) noexcept + : it{std::begin(seq)}, end{std::end(seq)} +{} + +template +constexpr binary_reader::binary_reader(It begin, It end) noexcept : + it{begin}, end{end} +{} + +template template constexpr value_u binary_reader::read_u() noexcept { @@ -16,7 +27,7 @@ constexpr value_u binary_reader::read_u() noexcept return buf; } -template +template template T binary_reader::read() noexcept { @@ -24,13 +35,13 @@ T binary_reader::read() noexcept return *reinterpret_cast(buf.bytes); } -template +template constexpr binary_reader::~binary_reader() noexcept { fm_assert(it == end); } -template +template template constexpr value_u binary_reader::read_u() noexcept { @@ -49,6 +60,45 @@ constexpr value_u binary_reader::read_u() noexcept return buf; } +template +[[maybe_unused]] constexpr inline T maybe_byteswap(T x) +{ + if constexpr(std::endian::native == std::endian::big) + return std::byteswap(x); + else + return x; +} + +template +binary_reader& operator>>(binary_reader& reader, T x) noexcept +{ + value_u u = reader.template read(); + x = *reinterpret_cast(&u.bytes[0]); + return reader; +} + +template It> +constexpr binary_writer::binary_writer(It it) noexcept : it{it} {} + +template It> +template +void binary_writer::write(T x) noexcept +{ + union { + T datum; + char bytes[sizeof(T)]; + } buf; + buf.datum = x; + for (std::size_t i = 0; i < sizeof(T); i++) + *it++ = buf.bytes[i]; +}; + +template It, serializable T> +binary_writer& operator>>(binary_writer& writer, T x) noexcept +{ + +} + } // namespace floormat::Serialize -- cgit v1.2.3