OleksandrKvl / sbepp

C++ implementation of the FIX Simple Binary Encoding
https://oleksandrkvl.github.io/sbepp/
MIT License
42 stars 4 forks source link

Cannot wrap the buffer to access the messageHeader #22

Closed ujos closed 6 months ago

ujos commented 6 months ago

There is following code inside the examples.md :

auto header = sbepp::make_const_view<>(buf.data(), buf.size());

I cannot make this code to compile. Is there another nice way to read the SBE header?

OleksandrKvl commented 6 months ago

That's a typo in documentation, obviously there's no way to create a header without specifying its type. If you check other usages of make_const_view on that same page or the make_const_view documentation, you'll see that template parameter is required. There are couple of ways you can access SBE header:

  1. Pass hard-coded header type (messageHeader in this example) to make_const_view:

    auto header = sbepp::make_view<your_schema::types::messageHeader>(buf.data(), buf.size());
  2. Similar to the above but get the header type using schema_traits:

    auto header = sbepp::make_const_view<sbepp::schema_traits<your_schema::schema>::header_type>(buf.data(), buf.size());
  3. If you know for sure what schema you're working with and just want to get the header from a message:

    // create message somehow
    your_schema::messages::msg_1<std::byte> msg;
    auto header = sbepp::get_header(msg);

    Note that at the moment it's not an error to create a wrong message view from a valid buffer, e.g., if you have a buffer with msg_1 but created msg_2 just to get the header from it.

P.S. Please don't close it even you got the answer, I'll fix that typo and close it afterwards.