protozero::pbf_reader Class Reference

`#include <pbf_reader.hpp>`

Inheritance diagram for protozero::pbf_reader:
[legend]

## Public Types

typedef const_varint_iterator< int32_t > | const_bool_iterator
| Forward iterator for iterating over bool (int32 varint) values.
typedef const_varint_iterator< int32_t > | const_enum_iterator
| Forward iterator for iterating over enum (int32 varint) values.
typedef const_varint_iterator< int32_t > | const_int32_iterator
| Forward iterator for iterating over int32 (varint) values.
typedef const_svarint_iterator< int32_t > | const_sint32_iterator
| Forward iterator for iterating over sint32 (varint) values.
typedef const_varint_iterator< uint32_t > | const_uint32_iterator
| Forward iterator for iterating over uint32 (varint) values.
typedef const_varint_iterator< int64_t > | const_int64_iterator
| Forward iterator for iterating over int64 (varint) values.
typedef const_svarint_iterator< int64_t > | const_sint64_iterator
| Forward iterator for iterating over sint64 (varint) values.
typedef const_varint_iterator< uint64_t > | const_uint64_iterator
| Forward iterator for iterating over uint64 (varint) values.

## Public Member Functions

| pbf_reader (const char *data, size_t length) noexcept
| pbf_reader (std::pair< const char *, size_t > data) noexcept
| pbf_reader (const std::string &data) noexcept
| pbf_reader () noexcept=default
| pbf_reader (const pbf_reader &) noexcept=default
| pbf_reader messages can be copied trivially.
| pbf_reader (pbf_reader &&) noexcept=default
| pbf_reader messages can be moved trivially.
pbf_reader & | operator= (const pbf_reader &other) noexcept=default
| pbf_reader messages can be copied trivially.
pbf_reader & | operator= (pbf_reader &&other) noexcept=default
| pbf_reader messages can be moved trivially.
| operator bool () const noexcept
size_t | length () const noexcept
bool | next ()
bool | next (pbf_tag_type tag)
pbf_tag_type | tag () const noexcept
pbf_wire_type | wire_type () const noexcept
bool | has_wire_type (pbf_wire_type type) const noexcept
void | skip ()
Scalar field accessor functions
bool | get_bool ()
int32_t | get_enum ()
int32_t | get_int32 ()
int32_t | get_sint32 ()
uint32_t | get_uint32 ()
int64_t | get_int64 ()
int64_t | get_sint64 ()
uint64_t | get_uint64 ()
uint32_t | get_fixed32 ()
int32_t | get_sfixed32 ()
uint64_t | get_fixed64 ()
int64_t | get_sfixed64 ()
float | get_float ()
double | get_double ()
std::pair< const char *, pbf_length_type > | get_data ()
std::string | get_bytes ()
std::string | get_string ()
pbf_reader | get_message ()
Repeated packed field accessor functions
std::pair< pbf_reader::const_bool_iterator, pbf_reader::const_bool_iterator > | get_packed_bool ()
std::pair< pbf_reader::const_enum_iterator, pbf_reader::const_enum_iterator > | get_packed_enum ()
std::pair< pbf_reader::const_int32_iterator, pbf_reader::const_int32_iterator > | get_packed_int32 ()
std::pair< pbf_reader::const_sint32_iterator, pbf_reader::const_sint32_iterator > | get_packed_sint32 ()
std::pair< pbf_reader::const_uint32_iterator, pbf_reader::const_uint32_iterator > | get_packed_uint32 ()
std::pair< pbf_reader::const_int64_iterator, pbf_reader::const_int64_iterator > | get_packed_int64 ()
std::pair< pbf_reader::const_sint64_iterator, pbf_reader::const_sint64_iterator > | get_packed_sint64 ()
std::pair< pbf_reader::const_uint64_iterator, pbf_reader::const_uint64_iterator > | get_packed_uint64 ()
auto | get_packed_fixed32 () -> decltype(packed_fixed< uint32_t >())
auto | get_packed_sfixed32 () -> decltype(packed_fixed< int32_t >())
auto | get_packed_fixed64 () -> decltype(packed_fixed< uint64_t >())
auto | get_packed_sfixed64 () -> decltype(packed_fixed< int64_t >())
auto | get_packed_float () -> decltype(packed_fixed< float >())
auto | get_packed_double () -> decltype(packed_fixed< double >())

## Detailed Description

This class represents a protobuf message. Either a top-level message or a nested sub-message. Top-level messages can be created from any buffer with a pointer and length:

std::string buffer;
// fill buffer...
pbf_reader message(buffer.data(), buffer.size());

Sub-messages are created using get_message():

pbf_reader message(...);
message.next();
pbf_reader submessage = message.get_message();

All methods of the pbf_reader class except get_bytes() and get_string() provide the strong exception guarantee, ie they either succeed or do not change the pbf_reader object they are called on. Use the get_data() method instead of get_bytes() or get_string(), if you need this guarantee.

## Constructor & Destructor Documentation

protozero::pbf_reader::pbf_reader | ( | const char * | data,
|  | size_t | length
| ) |  |

inline noexcept

Construct a pbf_reader message from a data pointer and a length. The pointer will be stored inside the pbf_reader object, no data is copied. So you must make sure the buffer stays valid as long as the pbf_reader object is used.

The buffer must contain a complete protobuf message.

Postcondition
  There is no current field.

protozero::pbf_reader::pbf_reader | ( | std::pair< const char *, size_t > | data | ) |

inline noexcept

Construct a pbf_reader message from a data pointer and a length. The pointer will be stored inside the pbf_reader object, no data is copied. So you must make sure the buffer stays valid as long as the pbf_reader object is used.

The buffer must contain a complete protobuf message.

Postcondition
  There is no current field.

protozero::pbf_reader::pbf_reader | ( | const std::string & | data | ) |

inline noexcept

Construct a pbf_reader message from a std::string. A pointer to the string internals will be stored inside the pbf_reader object, no data is copied. So you must make sure the string is unchanged as long as the pbf_reader object is used.

The string must contain a complete protobuf message.

Postcondition
  There is no current field.

protozero::pbf_reader::pbf_reader | ( |  | ) |

inline default noexcept

pbf_reader can be default constructed and behaves like it has an empty buffer.

## Member Function Documentation

bool protozero::pbf_reader::get_bool | ( |  | ) |

inline

Consume and return value of current "bool" field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "bool".

Postcondition
  The current field was consumed and there is no current field now.

std::string protozero::pbf_reader::get_bytes | ( |  | ) |

inline

Consume and return value of current "bytes" field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "bytes".

Postcondition
  The current field was consumed and there is no current field now.

std::pair< const char *, pbf_length_type > protozero::pbf_reader::get_data | ( |  | ) |

inline

Consume and return value of current "bytes" or "string" field.

Returns
  A pair with a pointer to the data and the length of the data.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "bytes" or "string".

Postcondition
  The current field was consumed and there is no current field now.

double protozero::pbf_reader::get_double | ( |  | ) |

inline

Consume and return value of current "double" field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "double".

Postcondition
  The current field was consumed and there is no current field now.

int32_t protozero::pbf_reader::get_enum | ( |  | ) |

inline

Consume and return value of current "enum" field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "enum".

Postcondition
  The current field was consumed and there is no current field now.

uint32_t protozero::pbf_reader::get_fixed32 | ( |  | ) |

inline

Consume and return value of current "fixed32" field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "fixed32".

Postcondition
  The current field was consumed and there is no current field now.

uint64_t protozero::pbf_reader::get_fixed64 | ( |  | ) |

inline

Consume and return value of current "fixed64" field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "fixed64".

Postcondition
  The current field was consumed and there is no current field now.

float protozero::pbf_reader::get_float | ( |  | ) |

inline

Consume and return value of current "float" field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "float".

Postcondition
  The current field was consumed and there is no current field now.

int32_t protozero::pbf_reader::get_int32 | ( |  | ) |

inline

Consume and return value of current "int32" varint field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "int32".

Postcondition
  The current field was consumed and there is no current field now.

int64_t protozero::pbf_reader::get_int64 | ( |  | ) |

inline

Consume and return value of current "int64" varint field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "int64".

Postcondition
  The current field was consumed and there is no current field now.

pbf_reader protozero::pbf_reader::get_message | ( |  | ) |

inline

Consume and return value of current "message" field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "message".

Postcondition
  The current field was consumed and there is no current field now.

std::pair< pbf_reader::const_bool_iterator, pbf_reader::const_bool_iterator > protozero::pbf_reader::get_packed_bool | ( |  | ) |

inline

Consume current "repeated packed bool" field.

Returns
  a pair of iterators to the beginning and one past the end of the data.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "repeated packed bool".

Postcondition
  The current field was consumed and there is no current field now.

auto protozero::pbf_reader::get_packed_double | ( |  | ) | -> decltype(packed_fixed<double>())

inline

Consume current "repeated packed double" field.

Returns
  a pair of iterators to the beginning and one past the end of the data.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "repeated packed double".

Postcondition
  The current field was consumed and there is no current field now.

std::pair< pbf_reader::const_enum_iterator, pbf_reader::const_enum_iterator > protozero::pbf_reader::get_packed_enum | ( |  | ) |

inline

Consume current "repeated packed enum" field.

Returns
  a pair of iterators to the beginning and one past the end of the data.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "repeated packed enum".

Postcondition
  The current field was consumed and there is no current field now.

auto protozero::pbf_reader::get_packed_fixed32 | ( |  | ) | -> decltype(packed_fixed<uint32_t>())

inline

Consume current "repeated packed fixed32" field.

Returns
  a pair of iterators to the beginning and one past the end of the data.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "repeated packed fixed32".

Postcondition
  The current field was consumed and there is no current field now.

auto protozero::pbf_reader::get_packed_fixed64 | ( |  | ) | -> decltype(packed_fixed<uint64_t>())

inline

Consume current "repeated packed fixed64" field.

Returns
  a pair of iterators to the beginning and one past the end of the data.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "repeated packed fixed64".

Postcondition
  The current field was consumed and there is no current field now.

auto protozero::pbf_reader::get_packed_float | ( |  | ) | -> decltype(packed_fixed<float>())

inline

Consume current "repeated packed float" field.

Returns
  a pair of iterators to the beginning and one past the end of the data.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "repeated packed float".

Postcondition
  The current field was consumed and there is no current field now.

std::pair< pbf_reader::const_int32_iterator, pbf_reader::const_int32_iterator > protozero::pbf_reader::get_packed_int32 | ( |  | ) |

inline

Consume current "repeated packed int32" field.

Returns
  a pair of iterators to the beginning and one past the end of the data.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "repeated packed int32".

Postcondition
  The current field was consumed and there is no current field now.

std::pair< pbf_reader::const_int64_iterator, pbf_reader::const_int64_iterator > protozero::pbf_reader::get_packed_int64 | ( |  | ) |

inline

Consume current "repeated packed int64" field.

Returns
  a pair of iterators to the beginning and one past the end of the data.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "repeated packed int64".

Postcondition
  The current field was consumed and there is no current field now.

auto protozero::pbf_reader::get_packed_sfixed32 | ( |  | ) | -> decltype(packed_fixed<int32_t>())

inline

Consume current "repeated packed sfixed32" field.

Returns
  a pair of iterators to the beginning and one past the end of the data.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "repeated packed sfixed32".

Postcondition
  The current field was consumed and there is no current field now.

auto protozero::pbf_reader::get_packed_sfixed64 | ( |  | ) | -> decltype(packed_fixed<int64_t>())

inline

Consume current "repeated packed sfixed64" field.

Returns
  a pair of iterators to the beginning and one past the end of the data.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "repeated packed sfixed64".

Postcondition
  The current field was consumed and there is no current field now.

std::pair< pbf_reader::const_sint32_iterator, pbf_reader::const_sint32_iterator > protozero::pbf_reader::get_packed_sint32 | ( |  | ) |

inline

Consume current "repeated packed sint32" field.

Returns
  a pair of iterators to the beginning and one past the end of the data.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "repeated packed sint32".

Postcondition
  The current field was consumed and there is no current field now.

std::pair< pbf_reader::const_sint64_iterator, pbf_reader::const_sint64_iterator > protozero::pbf_reader::get_packed_sint64 | ( |  | ) |

inline

Consume current "repeated packed sint64" field.

Returns
  a pair of iterators to the beginning and one past the end of the data.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "repeated packed sint64".

Postcondition
  The current field was consumed and there is no current field now.

std::pair< pbf_reader::const_uint32_iterator, pbf_reader::const_uint32_iterator > protozero::pbf_reader::get_packed_uint32 | ( |  | ) |

inline

Consume current "repeated packed uint32" field.

Returns
  a pair of iterators to the beginning and one past the end of the data.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "repeated packed uint32".

Postcondition
  The current field was consumed and there is no current field now.

std::pair< pbf_reader::const_uint64_iterator, pbf_reader::const_uint64_iterator > protozero::pbf_reader::get_packed_uint64 | ( |  | ) |

inline

Consume current "repeated packed uint64" field.

Returns
  a pair of iterators to the beginning and one past the end of the data.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "repeated packed uint64".

Postcondition
  The current field was consumed and there is no current field now.

int32_t protozero::pbf_reader::get_sfixed32 | ( |  | ) |

inline

Consume and return value of current "sfixed32" field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "sfixed32".

Postcondition
  The current field was consumed and there is no current field now.

int64_t protozero::pbf_reader::get_sfixed64 | ( |  | ) |

inline

Consume and return value of current "sfixed64" field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "sfixed64".

Postcondition
  The current field was consumed and there is no current field now.

int32_t protozero::pbf_reader::get_sint32 | ( |  | ) |

inline

Consume and return value of current "sint32" varint field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "sint32".

Postcondition
  The current field was consumed and there is no current field now.

int64_t protozero::pbf_reader::get_sint64 | ( |  | ) |

inline

Consume and return value of current "sint64" varint field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "sint64".

Postcondition
  The current field was consumed and there is no current field now.

std::string protozero::pbf_reader::get_string | ( |  | ) |

inline

Consume and return value of current "string" field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "string".

Postcondition
  The current field was consumed and there is no current field now.

uint32_t protozero::pbf_reader::get_uint32 | ( |  | ) |

inline

Consume and return value of current "uint32" varint field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "uint32".

Postcondition
  The current field was consumed and there is no current field now.

uint64_t protozero::pbf_reader::get_uint64 | ( |  | ) |

inline

Consume and return value of current "uint64" varint field.

Precondition
  There must be a current field (ie. next() must have returned `true`).
  The current field must be of type "uint64".

Postcondition
  The current field was consumed and there is no current field now.

bool protozero::pbf_reader::has_wire_type | ( | pbf_wire_type | type | ) | const

inline noexcept

Check the wire type of the current field.

Returns
  `true` if the current field has the given wire type.

Precondition
  There must be a current field (ie. next() must have returned `true`).

size_t protozero::pbf_reader::length | ( |  | ) | const

inline noexcept

Return the length in bytes of the current message. If you have already called next() and/or any of the get_*() functions, this will return the remaining length.

This can, for instance, be used to estimate the space needed for a buffer. Of course you have to know reasonably well what data to expect and how it is encoded for this number to have any meaning.

bool protozero::pbf_reader::next | ( |  | ) |

inline

Set next field in the message as the current field. This is usually called in a while loop:

pbf_reader message(...);
while (message.next()) {
// handle field
}

Returns
  `true` if there is a next field, `false` if not.

Precondition
  There must be no current field.

Postcondition
  If it returns `true` there is a current field now.

bool protozero::pbf_reader::next | ( | pbf_tag_type | tag | ) |

inline

Set next field with given tag in the message as the current field. Fields with other tags are skipped. This is usually called in a while loop for repeated fields:

pbf_reader message(...);
while (message.next(17)) {
// handle field
}

or you can call it just once to get the one field with this tag:

pbf_reader message(...);
if (message.next(17)) {
// handle field
}

Returns
  `true` if there is a next field with this tag.

Precondition
  There must be no current field.

Postcondition
  If it returns `true` there is a current field now with the given tag.

protozero::pbf_reader::operator bool | ( |  | ) | const

inline noexcept

In a boolean context the pbf_reader class evaluates to `true` if there are still fields available and to `false` if the last field has been read.

void protozero::pbf_reader::skip | ( |  | ) |

inline

Consume the current field.

Precondition
  There must be a current field (ie. next() must have returned `true`).

Postcondition
  The current field was consumed and there is no current field now.

pbf_tag_type protozero::pbf_reader::tag | ( |  | ) | const

inline noexcept

The tag of the current field. The tag is the field number from the description in the .proto file.

Call next() before calling this function to set the current field.

Returns
  tag of the current field.

Precondition
  There must be a current field (ie. next() must have returned `true`).

pbf_wire_type protozero::pbf_reader::wire_type | ( |  | ) | const

inline noexcept

Get the wire type of the current field. The wire types are:

- 0 - varint
- 1 - 64 bit
- 2 - length-delimited
- 5 - 32 bit

All other types are illegal.

Call next() before calling this function to set the current field.

Returns
  wire type of the current field.

Precondition
  There must be a current field (ie. next() must have returned `true`).

The documentation for this class was generated from the following file:

- include/protozero/pbf_reader.hpp