#include <pbf_reader.hpp>
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:
Sub-messages are created using 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_view() method instead of get_bytes() or get_string(), if you need this guarantee.
protozero::pbf_reader::pbf_reader |
( |
const data_view & |
view | ) |
|
|
inlineexplicitnoexcept |
Construct a pbf_reader message from a data_view. The pointer from the data_view will be stored inside the pbf_reader object, no data is copied. So you must make sure the view 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 char * |
data, |
|
|
std::size_t |
size |
|
) |
| |
|
inlinenoexcept |
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::pair< const char *, std::size_t > & |
data | ) |
|
|
inlinenoexcept |
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 | ) |
|
|
inlinenoexcept |
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 |
( |
| ) |
|
|
defaultnoexcept |
pbf_reader can be default constructed and behaves like it has an empty buffer.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
data_view protozero::pbf_reader::get_view |
( |
| ) |
|
|
inline |
Consume and return value of current "bytes", "string", or "message" field.
- Returns
- A data_view object.
- Precondition
- There must be a current field (ie. next() must have returned
true
).
-
The current field must be of type "bytes", "string", or "message".
- 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 |
|
inlinenoexcept |
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
).
std::size_t protozero::pbf_reader::length |
( |
| ) |
const |
|
inlinenoexcept |
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:
while (message.next()) {
}
- 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.
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:
while (message.next(17)) {
}
or you can call it just once to get the one field with this tag:
if (message.next(17)) {
}
Note that this will not check the wire type. The two-argument version of this function will also check the wire type.
- 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.
Set next field with given tag and wire type in the message as the current field. Fields with other tags are skipped. This is usually called in a while loop for repeated fields:
while (message.next(17, pbf_wire_type::varint)) {
}
or you can call it just once to get the one field with this tag:
if (message.next(17, pbf_wire_type::varint)) {
}
Note that this will also check the wire type. The one-argument version of this function will not check the wire type.
- 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 |
|
inlinenoexcept |
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.
void protozero::pbf_reader::swap |
( |
pbf_reader & |
other | ) |
|
|
inlinenoexcept |
Swap the contents of this object with the other.
- Parameters
-
other | Other object to swap data with. |
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
).
uint32_t protozero::pbf_reader::tag_and_type |
( |
| ) |
const |
|
inlinenoexcept |
Get the tag and wire type of the current field in one integer suitable for comparison with a switch statement.
Use it like this:
while (message.next()) {
switch (message.tag_and_type()) {
....
break;
....
break;
default:
message.skip();
}
}
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: