protozero  1.6.3
Minimalistic protocol buffer decoder and encoder in C++.
Public Types | Public Member Functions | List of all members
protozero::pbf_message< T > Class Template Reference

#include <pbf_message.hpp>

Inheritance diagram for protozero::pbf_message< T >:
Inheritance graph
[legend]
Collaboration diagram for protozero::pbf_message< T >:
Collaboration graph
[legend]

Public Types

using enum_type = T
 The type of messages this class will read.
 
- Public Types inherited from protozero::pbf_reader
using const_bool_iterator = const_varint_iterator< int32_t >
 Forward iterator for iterating over bool (int32 varint) values.
 
using const_enum_iterator = const_varint_iterator< int32_t >
 Forward iterator for iterating over enum (int32 varint) values.
 
using const_int32_iterator = const_varint_iterator< int32_t >
 Forward iterator for iterating over int32 (varint) values.
 
using const_sint32_iterator = const_svarint_iterator< int32_t >
 Forward iterator for iterating over sint32 (varint) values.
 
using const_uint32_iterator = const_varint_iterator< uint32_t >
 Forward iterator for iterating over uint32 (varint) values.
 
using const_int64_iterator = const_varint_iterator< int64_t >
 Forward iterator for iterating over int64 (varint) values.
 
using const_sint64_iterator = const_svarint_iterator< int64_t >
 Forward iterator for iterating over sint64 (varint) values.
 
using const_uint64_iterator = const_varint_iterator< uint64_t >
 Forward iterator for iterating over uint64 (varint) values.
 
using const_fixed32_iterator = const_fixed_iterator< uint32_t >
 Forward iterator for iterating over fixed32 values.
 
using const_sfixed32_iterator = const_fixed_iterator< int32_t >
 Forward iterator for iterating over sfixed32 values.
 
using const_fixed64_iterator = const_fixed_iterator< uint64_t >
 Forward iterator for iterating over fixed64 values.
 
using const_sfixed64_iterator = const_fixed_iterator< int64_t >
 Forward iterator for iterating over sfixed64 values.
 
using const_float_iterator = const_fixed_iterator< float >
 Forward iterator for iterating over float values.
 
using const_double_iterator = const_fixed_iterator< double >
 Forward iterator for iterating over double values.
 

Public Member Functions

template<typename... Args>
 pbf_message (Args &&... args) noexcept
 
bool next ()
 
bool next (T next_tag)
 
bool next (T next_tag, pbf_wire_type type)
 
tag () const noexcept
 
- Public Member Functions inherited from protozero::pbf_reader
 pbf_reader (const data_view &view) noexcept
 
 pbf_reader (const char *data, std::size_t size) noexcept
 
 pbf_reader (const std::pair< const char *, std::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_readeroperator= (const pbf_reader &other) noexcept=default
 pbf_reader messages can be copied trivially.
 
pbf_readeroperator= (pbf_reader &&other) noexcept=default
 pbf_reader messages can be moved trivially.
 
void swap (pbf_reader &other) noexcept
 
 operator bool () const noexcept
 
std::size_t length () const noexcept
 
bool next ()
 
bool next (pbf_tag_type next_tag)
 
bool next (pbf_tag_type next_tag, pbf_wire_type type)
 
pbf_tag_type tag () const noexcept
 
pbf_wire_type wire_type () const noexcept
 
uint32_t tag_and_type () const noexcept
 
bool has_wire_type (pbf_wire_type type) const noexcept
 
void skip ()
 
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 ()
 
data_view get_view ()
 
std::pair< const char *, pbf_length_typeget_data ()
 
std::string get_bytes ()
 
std::string get_string ()
 
pbf_reader get_message ()
 
iterator_range< pbf_reader::const_bool_iteratorget_packed_bool ()
 
iterator_range< pbf_reader::const_enum_iteratorget_packed_enum ()
 
iterator_range< pbf_reader::const_int32_iteratorget_packed_int32 ()
 
iterator_range< pbf_reader::const_sint32_iteratorget_packed_sint32 ()
 
iterator_range< pbf_reader::const_uint32_iteratorget_packed_uint32 ()
 
iterator_range< pbf_reader::const_int64_iteratorget_packed_int64 ()
 
iterator_range< pbf_reader::const_sint64_iteratorget_packed_sint64 ()
 
iterator_range< pbf_reader::const_uint64_iteratorget_packed_uint64 ()
 
iterator_range< pbf_reader::const_fixed32_iteratorget_packed_fixed32 ()
 
iterator_range< pbf_reader::const_sfixed32_iteratorget_packed_sfixed32 ()
 
iterator_range< pbf_reader::const_fixed64_iteratorget_packed_fixed64 ()
 
iterator_range< pbf_reader::const_sfixed64_iteratorget_packed_sfixed64 ()
 
iterator_range< pbf_reader::const_float_iteratorget_packed_float ()
 
iterator_range< pbf_reader::const_double_iteratorget_packed_double ()
 

Detailed Description

template<typename T>
class protozero::pbf_message< T >

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:

enum class Message : protozero::pbf_tag_type {
...
};
std::string buffer;
// fill buffer...
pbf_message<Message> message{buffer.data(), buffer.size()};

Sub-messages are created using get_message():

enum class SubMessage : protozero::pbf_tag_type {
...
};
pbf_message<Message> message{...};
message.next();
pbf_message<SubMessage> submessage = message.get_message();

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

This template class is based on the pbf_reader class and has all the same methods. The difference is that whereever the pbf_reader class takes an integer tag, this template class takes a tag of the template type T.

Read the tutorial to understand how this class is used.

Constructor & Destructor Documentation

◆ pbf_message()

template<typename T >
template<typename... Args>
protozero::pbf_message< T >::pbf_message ( Args &&...  args)
inlinenoexcept

Construct a pbf_message. All arguments are forwarded to the pbf_reader parent class.

Member Function Documentation

◆ next() [1/3]

template<typename T >
bool protozero::pbf_message< T >::next ( )
inline

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

pbf_message<...> 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.

◆ next() [2/3]

template<typename T >
bool protozero::pbf_message< T >::next ( next_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_message<Example1> message{...};
while (message.next(Example1::repeated_fixed64_r)) {
// handle field
}

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

pbf_message<Example1> message{...};
if (message.next(Example1::required_uint32_x)) {
// handle field
}

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.

◆ next() [3/3]

template<typename T >
bool protozero::pbf_message< T >::next ( next_tag,
pbf_wire_type  type 
)
inline

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:

pbf_message<Example1> message{...};
while (message.next(Example1::repeated_fixed64_r, pbf_wire_type::varint)) {
// handle field
}

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

pbf_message<Example1> message{...};
if (message.next(Example1::required_uint32_x, pbf_wire_type::varint)) {
// handle field
}

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.

◆ tag()

template<typename T >
T protozero::pbf_message< T >::tag ( ) const
inlinenoexcept

The tag of the current field. The tag is the enum value for 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).

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