Read fields lazily¶
Use a rbe::dsrl::proxy<T> to read exactly the fields you need, by name, by index, through nested structs,
or by the annotation a field carries.
Source: example/howto/lazy_fields.cpp, CMake target howto_lazy_fields.
struct [[=rbe::pack_be]] Header {
[[=rbe::id]] std::uint8_t type;
std::uint16_t length;
auto operator==(Header const&) const -> bool = default;
};
struct [[=rbe::pack_be]] Quote {
Header header;
std::array<char, 8> symbol;
std::uint32_t bid;
std::uint32_t ask;
auto operator==(Quote const&) const -> bool = default;
};
By name or by index¶
A field name that does not exist is a compile-time error.
auto quote = rbe::deserialize<Quote>(bytes, rbe::dsrl::lazy);
auto bid = quote.field<"bid">(); // by name: a typo is a compile-time error
auto ask = quote.field<3>(); // by declaration index
auto name = quote.field<"symbol">().value(); // a std::array member is a view too: value() decodes it
A member that is a struct, std::array included, comes back as another view. value() decodes it.
Nested structs¶
auto quote = rbe::deserialize<Quote>(bytes, rbe::dsrl::lazy);
auto header = quote.field<"header">(); // a struct member is another view (use auto:
auto length = header.field<"length">(); // its type carries the inherited annotations)
auto type = quote.field<"header", "type">(); // or reached in one go with a path
auto same = quote.field<rbe::id>(); // or by the annotation it carries, searching nested types
The type of a nested view carries the annotations it inherits from the enclosing struct, so write auto
rather than rbe::dsrl::proxy<Header>.
The whole value¶
auto view = rbe::deserialize<Quote>(bytes, rbe::dsrl::lazy);
Quote quote = view.value(); // decode everything, same result as rbe::dsrl::eager
Quote again = *view; // same thing
std::span<std::byte const> raw = view.as_span(); // exactly the bytes Quote occupies: view.size() of them
See also: Deserialization reference, proxy for the full interface.