Skip to content

Commit

Permalink
[struct_pack][fix] fix compile error for user-defined serialization w…
Browse files Browse the repository at this point in the history
…ith user-defined stream input/output
  • Loading branch information
poor-circle committed Sep 19, 2024
1 parent cc48815 commit 0f96f21
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
5 changes: 3 additions & 2 deletions include/ylt/struct_pack/user_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <std::size_t size_width = sizeof(uint64_t), typename Writer,
typename T>
Expand All @@ -17,7 +18,7 @@ template <std::size_t size_width = sizeof(uint64_t), typename Writer,
STRUCT_PACK_INLINE void write(Writer& writer, const T* t, std::size_t len) {
if constexpr (detail::is_trivial_serializable<T>::value &&
detail::is_little_endian_copyable<sizeof(T)>) {
write_bytes_array(writer, (char*)t, sizeof(T) * len);
detail::write_bytes_array(writer, (char*)t, sizeof(T) * len);
}
else {
struct_pack::detail::packer<Writer, T, true> packer{writer};
Expand All @@ -40,7 +41,7 @@ STRUCT_PACK_INLINE struct_pack::err_code read(Reader& reader, T* t,
if constexpr (detail::is_trivial_serializable<T>::value &&
detail::is_little_endian_copyable<sizeof(T)>) {
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;
}
}
Expand Down
44 changes: 40 additions & 4 deletions src/struct_pack/examples/user_defined_serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<my_name_space::array2D> 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<decltype(ar)>(buffer);
assert(result.has_value());
assert(ar == result);
{
auto buffer = struct_pack::serialize(ar);
auto result = struct_pack::deserialize<decltype(ar)>(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<decltype(ar)>(in);
assert(result.has_value());
assert(ar == result);
}
}

0 comments on commit 0f96f21

Please sign in to comment.