From 0f96f2155dcf6733a661a742e69c5c212f496e1e Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Thu, 19 Sep 2024 15:22:48 +0800 Subject: [PATCH] [struct_pack][fix] fix compile error for user-defined serialization with user-defined stream input/output --- include/ylt/struct_pack/user_helper.hpp | 5 ++- .../examples/user_defined_serialization.cpp | 44 +++++++++++++++++-- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/include/ylt/struct_pack/user_helper.hpp b/include/ylt/struct_pack/user_helper.hpp index 35a0a1f3f..3bdecaad1 100644 --- a/include/ylt/struct_pack/user_helper.hpp +++ b/include/ylt/struct_pack/user_helper.hpp @@ -5,6 +5,7 @@ #include "ylt/struct_pack/packer.hpp" #include "ylt/struct_pack/reflection.hpp" #include "ylt/struct_pack/unpacker.hpp" +#include "ylt/struct_pack/endian_wrapper.hpp" namespace struct_pack { template @@ -17,7 +18,7 @@ template ::value && detail::is_little_endian_copyable) { - write_bytes_array(writer, (char*)t, sizeof(T) * len); + detail::write_bytes_array(writer, (char*)t, sizeof(T) * len); } else { struct_pack::detail::packer packer{writer}; @@ -40,7 +41,7 @@ STRUCT_PACK_INLINE struct_pack::err_code read(Reader& reader, T* t, if constexpr (detail::is_trivial_serializable::value && detail::is_little_endian_copyable) { if constexpr (!ifSkip) { - if SP_UNLIKELY (!read_bytes_array(reader, (char*)t, sizeof(T) * len)) { + if SP_UNLIKELY (!detail::read_bytes_array(reader, (char*)t, sizeof(T) * len)) { return struct_pack::errc::no_buffer_space; } } diff --git a/src/struct_pack/examples/user_defined_serialization.cpp b/src/struct_pack/examples/user_defined_serialization.cpp index 1f0701ac3..1fca94fbd 100644 --- a/src/struct_pack/examples/user_defined_serialization.cpp +++ b/src/struct_pack/examples/user_defined_serialization.cpp @@ -96,14 +96,50 @@ struct_pack::err_code sp_deserialize_to(Reader& reader, array2D& ar) { // struct_pack::errc sp_deserialize_to_with_skip(Reader& reader, array2D& ar); } // namespace my_name_space + +struct fwrite_stream { + FILE* file; + bool write(const char* data, std::size_t sz) { + return fwrite(data, sz, 1, file) == 1; + } + fwrite_stream(const char* file_name) : file(fopen(file_name, "wb")) {} + ~fwrite_stream() { fclose(file); } +}; + +struct fread_stream { + FILE* file; + bool read(char* data, std::size_t sz) { + return fread(data, sz, 1, file) == 1; + } + bool ignore(std::size_t sz) { return fseek(file, sz, SEEK_CUR) == 0; } + std::size_t tellg() { + // if you worry about ftell performance, just use an variable to record it. + return ftell(file); + } + fread_stream(const char* file_name) : file(fopen(file_name, "rb")) {} + ~fread_stream() { fclose(file); } +}; + void user_defined_serialization() { std::vector ar; ar.emplace_back(11, 22); ar.emplace_back(114, 514); ar[0](1, 6) = 3.14; ar[1](87, 111) = 2.71; - auto buffer = struct_pack::serialize(ar); - auto result = struct_pack::deserialize(buffer); - assert(result.has_value()); - assert(ar == result); + { + auto buffer = struct_pack::serialize(ar); + auto result = struct_pack::deserialize(buffer); + assert(result.has_value()); + assert(ar == result); + } + { + { + fwrite_stream out("1.tmp"); + struct_pack::serialize_to(out,ar); + } + fread_stream in("1.tmp"); + auto result = struct_pack::deserialize(in); + assert(result.has_value()); + assert(ar == result); + } } \ No newline at end of file