-
Notifications
You must be signed in to change notification settings - Fork 42
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
Comments
@p-ranav Any comment ? |
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 // 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
|
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 ? |
Yes, I think so. We can add overloads to |
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.
The text was updated successfully, but these errors were encountered: