XB Library: xb::parser::element

The element parser functions are used to serialize and create xb::element objects to/from data buffer.


Interface:

namespace xb {
namespace parser {
namespace element {

    void write (const xb::element &, xb::alias & output) throw ();
    xb::element parse (xb::const_alias & input, xb::size_type * motion = 0) throw ();

    xb::size_type length (const xb::element &) throw ();

};
};
};

xb::parser::element::write

This function serializes XB element (the first parameter) into the output alias. The second parameter (output) is a reference to an object that implements the xb::alias interface. Upon exiting, the alias points to first byte after the encoded value.

xb::parser::element::parse

This function parses as much data from the input (first parameter) as needed (but not more than available) to construct a valid XB element. The element is returned and the input alias (reference to an object that implements the xb::const_alias interface) is updated to point to first byte after the element.

If the motion pointer is used (not null), the variable it points to is incremented of number of bytes parsed by this function.

xb::parser::element::length

This function returns number of bytes required to store the element passed as the parameter.

Examples:

// ...

xb::element e ("some value");
xb::size_type buffer_size = N;
unsigned char buffer [N];

// ...

if (xb::parser::element::length (e) <= buffer_size) {
    xb::buffer_alias output (buffer, buffer_size);
    xb::parser::element::write (e, output);

} else {
    // not enough space in buffer
};

// ...

Remarks:

The binary representation for XB element consists simply of VSN specifying data length followed by data itself (in case VSN is not zero). No leading or trailing magic byte is used.


[index]