Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[struct_pack][fix] fix compile error for user-defined serialization w… #776

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/ylt/struct_pack/user_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <cstdint>

#include "ylt/struct_pack/calculate_size.hpp"
#include "ylt/struct_pack/endian_wrapper.hpp"
#include "ylt/struct_pack/packer.hpp"
#include "ylt/struct_pack/reflection.hpp"
#include "ylt/struct_pack/unpacker.hpp"
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,8 @@ 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
43 changes: 39 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,49 @@ 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);
}
}
Loading