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.cpp | 10 ++----- serialize/binary-serializer.hpp | 36 ++++++++++++++++++++----- serialize/binary-serializer.inl | 58 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 85 insertions(+), 19 deletions(-) diff --git a/serialize/binary-serializer.cpp b/serialize/binary-serializer.cpp index 08606768..1a6da0fd 100644 --- a/serialize/binary-serializer.cpp +++ b/serialize/binary-serializer.cpp @@ -74,13 +74,7 @@ static constexpr bool test2() } static_assert(test2()); -template -[[maybe_unused]] static constexpr T maybe_byteswap(T x) -{ - if constexpr(std::endian::native == std::endian::big) - return std::byteswap(x); - else - return x; -} +using test3 = binary_reader::iterator>; +static_assert(std::is_same_v() >> std::declval() )>); } // namespace floormat::Serialize diff --git a/serialize/binary-serializer.hpp b/serialize/binary-serializer.hpp index 19aa5548..589e437e 100644 --- a/serialize/binary-serializer.hpp +++ b/serialize/binary-serializer.hpp @@ -56,27 +56,49 @@ concept char_sequence = requires(T& x, const T& cx) { requires std::same_as>; }; -template +template +concept string_input_iterator = requires(It it) { + requires std::forward_iterator; + requires std::is_same_v>; +}; + +template +concept serializable = requires(T x) { + requires std::floating_point || integer; +}; + +template struct binary_reader final { - fm_DECLARE_DEFAULT_MOVE_COPY_ASSIGNMENTS(binary_reader); - constexpr binary_reader(It begin, It end) noexcept : it{begin}, end{end} {} - template - explicit constexpr binary_reader(const Seq& seq) noexcept : it{std::begin(seq)}, end{std::end(seq)} {} + template explicit constexpr binary_reader(const Seq& seq) noexcept; + constexpr binary_reader(It begin, It end) noexcept; constexpr ~binary_reader() noexcept; template constexpr value_u read_u() noexcept; template constexpr value_u read_u() noexcept; template T read() noexcept; - static_assert(std::is_same_v())>>); + template + friend binary_reader& operator>>(binary_reader& reader, T& x) noexcept; private: It it, end; }; -template binary_reader(It&& begin, It&& end) -> binary_reader>; +template binary_reader(It&& begin, It&& end) -> binary_reader>; template binary_reader(Array&& array) -> binary_reader>; +template It> +struct binary_writer final { + explicit constexpr binary_writer(It it) noexcept; + template void write(T x) noexcept; + + template + friend binary_writer& operator>>(binary_writer& writer, T x) noexcept; + +private: + It it; +}; + } // namespace floormat::Serialize 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