protozero::pbf_builder< T > Class Template Reference

`#include <pbf_builder.hpp>`

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

## Public Types

using | enum_type = T
| The type of messages this class will build.

## Public Member Functions

| pbf_builder (std::string &data) noexcept
template<typename P >
| pbf_builder (pbf_writer &parent_writer, P tag) noexcept
void | add_bytes (T tag, const char *value, std::size_t size)
void | add_bytes (T tag, const data_view &value)
void | add_bytes (T tag, const std::string &value)
void | add_bytes (T tag, const char *value)
template<typename... Ts>
void | add_bytes_vectored (T tag, Ts &&... values)
void | add_string (T tag, const char *value, std::size_t size)
void | add_string (T tag, const data_view &value)
void | add_string (T tag, const std::string &value)
void | add_string (T tag, const char *value)
void | add_message (T tag, const char *value, std::size_t size)
void | add_message (T tag, const data_view &value)
void | add_message (T tag, const std::string &value)
Public Member Functions inherited from protozero::pbf_writer
| pbf_writer (std::string &data) noexcept
| pbf_writer () noexcept=default
| pbf_writer (pbf_writer &parent_writer, pbf_tag_type tag, std::size_t size=0)
| pbf_writer (const pbf_writer &)=delete
| A pbf_writer object can not be copied.
pbf_writer & | operator= (const pbf_writer &)=delete
| A pbf_writer object can not be copied.
| pbf_writer (pbf_writer &&other) noexcept
pbf_writer & | operator= (pbf_writer &&other) noexcept
bool | valid () const noexcept
void | swap (pbf_writer &other) noexcept
void | reserve (std::size_t size)
void | commit ()
void | rollback ()
void | add_bool (pbf_tag_type tag, bool value)
void | add_enum (pbf_tag_type tag, int32_t value)
void | add_int32 (pbf_tag_type tag, int32_t value)
void | add_sint32 (pbf_tag_type tag, int32_t value)
void | add_uint32 (pbf_tag_type tag, uint32_t value)
void | add_int64 (pbf_tag_type tag, int64_t value)
void | add_sint64 (pbf_tag_type tag, int64_t value)
void | add_uint64 (pbf_tag_type tag, uint64_t value)
void | add_fixed32 (pbf_tag_type tag, uint32_t value)
void | add_sfixed32 (pbf_tag_type tag, int32_t value)
void | add_fixed64 (pbf_tag_type tag, uint64_t value)
void | add_sfixed64 (pbf_tag_type tag, int64_t value)
void | add_float (pbf_tag_type tag, float value)
void | add_double (pbf_tag_type tag, double value)
void | add_bytes (pbf_tag_type tag, const char *value, std::size_t size)
void | add_bytes (pbf_tag_type tag, const data_view &value)
void | add_bytes (pbf_tag_type tag, const std::string &value)
void | add_bytes (pbf_tag_type tag, const char *value)
template<typename... Ts>
void | add_bytes_vectored (pbf_tag_type tag, Ts &&... values)
void | add_string (pbf_tag_type tag, const char *value, std::size_t size)
void | add_string (pbf_tag_type tag, const data_view &value)
void | add_string (pbf_tag_type tag, const std::string &value)
void | add_string (pbf_tag_type tag, const char *value)
void | add_message (pbf_tag_type tag, const char *value, std::size_t size)
void | add_message (pbf_tag_type tag, const data_view &value)
void | add_message (pbf_tag_type tag, const std::string &value)
template<typename InputIterator >
void | add_packed_bool (pbf_tag_type tag, InputIterator first, InputIterator last)
template<typename InputIterator >
void | add_packed_enum (pbf_tag_type tag, InputIterator first, InputIterator last)
template<typename InputIterator >
void | add_packed_int32 (pbf_tag_type tag, InputIterator first, InputIterator last)
template<typename InputIterator >
void | add_packed_sint32 (pbf_tag_type tag, InputIterator first, InputIterator last)
template<typename InputIterator >
void | add_packed_uint32 (pbf_tag_type tag, InputIterator first, InputIterator last)
template<typename InputIterator >
void | add_packed_int64 (pbf_tag_type tag, InputIterator first, InputIterator last)
template<typename InputIterator >
void | add_packed_sint64 (pbf_tag_type tag, InputIterator first, InputIterator last)
template<typename InputIterator >
void | add_packed_uint64 (pbf_tag_type tag, InputIterator first, InputIterator last)
template<typename ValueType , typename InputIterator >
void | add_packed_fixed (pbf_tag_type tag, InputIterator first, InputIterator last)
template<typename InputIterator >
void | add_packed_fixed32 (pbf_tag_type tag, InputIterator first, InputIterator last)
template<typename InputIterator >
void | add_packed_sfixed32 (pbf_tag_type tag, InputIterator first, InputIterator last)
template<typename InputIterator >
void | add_packed_fixed64 (pbf_tag_type tag, InputIterator first, InputIterator last)
template<typename InputIterator >
void | add_packed_sfixed64 (pbf_tag_type tag, InputIterator first, InputIterator last)
template<typename InputIterator >
void | add_packed_float (pbf_tag_type tag, InputIterator first, InputIterator last)
template<typename InputIterator >
void | add_packed_double (pbf_tag_type tag, InputIterator first, InputIterator last)

## Detailed Description

### template<typename T>
class protozero::pbf_builder< T >

The pbf_builder is used to write PBF formatted messages into a buffer. It is based on the pbf_writer class and has all the same methods. The difference is that while the pbf_writer class takes an integer tag, this template class takes a tag of the template type T. The idea is that T will be an enumeration value and this helps reduce the possibility of programming errors.

Almost all methods in this class can throw an std::bad_alloc exception if the std::string used as a buffer wants to resize.

Read the tutorial to understand how this class is used.

## Constructor & Destructor Documentation

## pbf_builder() [1/2]

template<typename T >

protozero::pbf_builder< T >::pbf_builder | ( | std::string & | data | ) |

inline explicit noexcept

Create a builder using the given string as a data store. The object stores a reference to that string and adds all data to it. The string doesn't have to be empty. The pbf_message object will just append data.

## pbf_builder() [2/2]

template<typename T >
template<typename P >

protozero::pbf_builder< T >::pbf_builder | ( | pbf_writer & | parent_writer,
|  | P | tag
| ) |  |

inline noexcept

Construct a pbf_builder for a submessage from the pbf_message or pbf_writer of the parent message.

Parameters

  parent_writer | The parent pbf_message or pbf_writer
  tag | Tag of the field that will be written

## Member Function Documentation

## add_bytes() [1/4]

template<typename T >

void protozero::pbf_builder< T >::add_bytes | ( | T | tag,
|  | const char * | value,
|  | std::size_t | size
| ) |  |

inline

Add "bytes" field to data.

Parameters

  tag | Tag of the field
  value | Pointer to value to be written
  size | Number of bytes to be written

## add_bytes() [2/4]

template<typename T >

void protozero::pbf_builder< T >::add_bytes | ( | T | tag,
|  | const data_view & | value
| ) |  |

inline

Add "bytes" field to data.

Parameters

  tag | Tag of the field
  value | Value to be written

## add_bytes() [3/4]

template<typename T >

void protozero::pbf_builder< T >::add_bytes | ( | T | tag,
|  | const std::string & | value
| ) |  |

inline

Add "bytes" field to data.

Parameters

  tag | Tag of the field
  value | Value to be written

## add_bytes() [4/4]

template<typename T >

void protozero::pbf_builder< T >::add_bytes | ( | T | tag,
|  | const char * | value
| ) |  |

inline

Add "bytes" field to data. Bytes from the value are written until a null byte is encountered. The null byte is not added.

Parameters

  tag | Tag of the field
  value | Pointer to zero-delimited value to be written

## add_bytes_vectored()

template<typename T >
template<typename... Ts>

void protozero::pbf_builder< T >::add_bytes_vectored | ( | T | tag,
|  | Ts &&... | values
| ) |  |

inline

Add "bytes" field to data using vectored input. All the data in the 2nd and further arguments is "concatenated" with only a single copy into the final buffer.

This will work with objects of any type supporting the data() and size() methods like std::string or protozero::data_view.

Example:

std::string data1 = "abc";
std::string data2 = "xyz";
builder.add_bytes_vectored(1, data1, data2);

Template Parameters

  Ts | List of types supporting data() and size() methods.

Parameters

  tag | Tag of the field
  values | List of objects of types Ts with data to be appended.

## add_message() [1/3]

template<typename T >

void protozero::pbf_builder< T >::add_message | ( | T | tag,
|  | const char * | value,
|  | std::size_t | size
| ) |  |

inline

Add "message" field to data.

Parameters

  tag | Tag of the field
  value | Pointer to message to be written
  size | Length of the message

## add_message() [2/3]

template<typename T >

void protozero::pbf_builder< T >::add_message | ( | T | tag,
|  | const data_view & | value
| ) |  |

inline

Add "message" field to data.

Parameters

  tag | Tag of the field
  value | Value to be written. The value must be a complete message.

## add_message() [3/3]

template<typename T >

void protozero::pbf_builder< T >::add_message | ( | T | tag,
|  | const std::string & | value
| ) |  |

inline

Add "message" field to data.

Parameters

  tag | Tag of the field
  value | Value to be written. The value must be a complete message.

## add_string() [1/4]

template<typename T >

void protozero::pbf_builder< T >::add_string | ( | T | tag,
|  | const char * | value,
|  | std::size_t | size
| ) |  |

inline

Add "string" field to data.

Parameters

  tag | Tag of the field
  value | Pointer to value to be written
  size | Number of bytes to be written

## add_string() [2/4]

template<typename T >

void protozero::pbf_builder< T >::add_string | ( | T | tag,
|  | const data_view & | value
| ) |  |

inline

Add "string" field to data.

Parameters

  tag | Tag of the field
  value | Value to be written

## add_string() [3/4]

template<typename T >

void protozero::pbf_builder< T >::add_string | ( | T | tag,
|  | const std::string & | value
| ) |  |

inline

Add "string" field to data.

Parameters

  tag | Tag of the field
  value | Value to be written

## add_string() [4/4]

template<typename T >

void protozero::pbf_builder< T >::add_string | ( | T | tag,
|  | const char * | value
| ) |  |

inline

Add "string" field to data. Bytes from the value are written until a null byte is encountered. The null byte is not added.

Parameters

  tag | Tag of the field
  value | Pointer to value to be written

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

- include/protozero/pbf_builder.hpp