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

Needs to embed a fake struct to serialize direct STL types #14

Open
krawq opened this issue Nov 30, 2022 · 4 comments
Open

Needs to embed a fake struct to serialize direct STL types #14

krawq opened this issue Nov 30, 2022 · 4 comments

Comments

@krawq
Copy link

krawq commented Nov 30, 2022

This works:
constexpr auto OPTIONS = alpaca::options::with_checksum | alpaca::options::fixed_length_encoding; using serialize_t = std::map<int,bool>; auto Serialize(const serialize_t& to_serialize) { std::vector<uint8_t> buffer; struct MyStruct { serialize_t field; }; const MyStruct& object{to_serialize}; if(auto bytes_written=alpaca::serialize<OPTIONS>(object,buffer)) return std::make_pair(buffer,true); else return std::make_pair(buffer,false); };

This does not:

constexpr auto OPTIONS = alpaca::options::with_checksum | alpaca::options::fixed_length_encoding; using serialize_t = std::map<int,bool>; auto Serialize(const serialize_t& to_serialize) { std::vector<uint8_t> buffer; if(auto bytes_written=alpaca::serialize<OPTIONS>(to_serialize,buffer)) return std::make_pair(buffer,true); else return std::make_pair(buffer,false); };

Would be great to be able to use it without using MyStruct.

@krawq
Copy link
Author

krawq commented Dec 9, 2022

@p-ranav Any comment ?

@p-ranav
Copy link
Owner

p-ranav commented Dec 12, 2022

There are internal functions that can be used for this. Perhaps these can be moved out of detail and updated to match the API interface.

The function you're looking for is to_bytes(...). A few things don't work with this internal function, e.g., checksum. To include all features, we need a new PR to make the library work without wrapping the fields in a struct. I'm open to this PR but don't currently have the time for it. I'll keep this open so that I can get to it in the future.

// main.cpp
#include <alpaca/alpaca.h>

constexpr auto OPTIONS =
    alpaca::options::with_checksum | alpaca::options::fixed_length_encoding;

using serialize_t = std::map<int, bool>;

auto Serialize(const serialize_t &to_serialize) {
  std::vector<uint8_t> buffer;
  std::size_t byte_index{0};
  alpaca::detail::to_bytes<OPTIONS>(buffer, byte_index, to_serialize);
  return buffer;
};

int main() {
  auto foo = serialize_t{{5, true}, {3, false}};
  auto buffer = Serialize(foo);

  alpaca::detail::print_bytes(buffer);
}
foo:bar $ ./main 
bytes[18]:
  0x02 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
  0x03 0x00 0x00 0x00 0x00 0x05 0x00 0x00 
  0x00 0x01 

@krawq
Copy link
Author

krawq commented Dec 13, 2022

Without exposing to_bytes, is there any SFINAE magic that could check overload of to_bytes, and if found just call to_bytes instead of the rest of the serialize func ?

@p-ranav
Copy link
Owner

p-ranav commented Dec 13, 2022

Yes, I think so. We can add overloads to to_bytes that match the serialize interface (serialize(object, bytes)). The call can be routed to the relevant to_bytes implementation for that type. Just need to make sure that the OPTIONS still work correctly - calculating checksum if requested, calculating a type-hash if requested etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants