Loading...
Searching...
No Matches
basic_pbf_builder.hpp
Go to the documentation of this file.
#ifndef PROTOZERO_BASIC_PBF_BUILDER_HPP
#define PROTOZERO_BASIC_PBF_BUILDER_HPP
/*****************************************************************************
protozero - Minimalistic protocol buffer decoder and encoder in C++.
This file is from https://github.com/mapbox/protozero where you can find more
documentation.
*****************************************************************************/
#include "basic_pbf_writer.hpp"
#include "types.hpp"
#include <type_traits>
namespace protozero {
template <typename TBuffer, typename T>
class basic_pbf_builder : public basic_pbf_writer<TBuffer> {
static_assert(std::is_same<pbf_tag_type, std::underlying_type_t<T>>::value,
"T must be enum with underlying type protozero::pbf_tag_type");
public:
using enum_type = T;
basic_pbf_builder() = default;
explicit basic_pbf_builder(TBuffer& data) noexcept :
basic_pbf_writer<TBuffer>{data} {
}
template <typename P>
basic_pbf_builder(basic_pbf_writer<TBuffer>& parent_writer, P tag) :
basic_pbf_writer<TBuffer>{parent_writer, pbf_tag_type(tag)} {
}
#define PROTOZERO_WRITER_WRAP_ADD_SCALAR(name, type) \
void add_##name(T tag, type value) { \
basic_pbf_writer<TBuffer>::add_##name(pbf_tag_type(tag), value); \
}
PROTOZERO_WRITER_WRAP_ADD_SCALAR(bool, bool)
PROTOZERO_WRITER_WRAP_ADD_SCALAR(enum, int32_t)
PROTOZERO_WRITER_WRAP_ADD_SCALAR(int32, int32_t)
PROTOZERO_WRITER_WRAP_ADD_SCALAR(sint32, int32_t)
PROTOZERO_WRITER_WRAP_ADD_SCALAR(uint32, uint32_t)
PROTOZERO_WRITER_WRAP_ADD_SCALAR(int64, int64_t)
PROTOZERO_WRITER_WRAP_ADD_SCALAR(sint64, int64_t)
PROTOZERO_WRITER_WRAP_ADD_SCALAR(uint64, uint64_t)
PROTOZERO_WRITER_WRAP_ADD_SCALAR(fixed32, uint32_t)
PROTOZERO_WRITER_WRAP_ADD_SCALAR(sfixed32, int32_t)
PROTOZERO_WRITER_WRAP_ADD_SCALAR(fixed64, uint64_t)
PROTOZERO_WRITER_WRAP_ADD_SCALAR(sfixed64, int64_t)
PROTOZERO_WRITER_WRAP_ADD_SCALAR(float, float)
PROTOZERO_WRITER_WRAP_ADD_SCALAR(double, double)
#undef PROTOZERO_WRITER_WRAP_ADD_SCALAR
void add_bytes(T tag, const char* value, std::size_t size) {
basic_pbf_writer<TBuffer>::add_bytes(pbf_tag_type(tag), value, size);
}
void add_bytes(T tag, const data_view& value) {
basic_pbf_writer<TBuffer>::add_bytes(pbf_tag_type(tag), value);
}
void add_bytes(T tag, const std::string& value) {
basic_pbf_writer<TBuffer>::add_bytes(pbf_tag_type(tag), value);
}
void add_bytes(T tag, const char* value) {
basic_pbf_writer<TBuffer>::add_bytes(pbf_tag_type(tag), value);
}
template <typename... Ts>
void add_bytes_vectored(T tag, Ts&&... values) {
basic_pbf_writer<TBuffer>::add_bytes_vectored(pbf_tag_type(tag), std::forward<Ts>(values)...);
}
void add_string(T tag, const char* value, std::size_t size) {
basic_pbf_writer<TBuffer>::add_string(pbf_tag_type(tag), value, size);
}
void add_string(T tag, const data_view& value) {
basic_pbf_writer<TBuffer>::add_string(pbf_tag_type(tag), value);
}
void add_string(T tag, const std::string& value) {
basic_pbf_writer<TBuffer>::add_string(pbf_tag_type(tag), value);
}
void add_string(T tag, const char* value) {
basic_pbf_writer<TBuffer>::add_string(pbf_tag_type(tag), value);
}
void add_message(T tag, const char* value, std::size_t size) {
basic_pbf_writer<TBuffer>::add_message(pbf_tag_type(tag), value, size);
}
void add_message(T tag, const data_view& value) {
basic_pbf_writer<TBuffer>::add_message(pbf_tag_type(tag), value);
}
void add_message(T tag, const std::string& value) {
basic_pbf_writer<TBuffer>::add_message(pbf_tag_type(tag), value);
}
#define PROTOZERO_WRITER_WRAP_ADD_PACKED(name) \
template <typename InputIterator> \
void add_packed_##name(T tag, InputIterator first, InputIterator last) { \
basic_pbf_writer<TBuffer>::add_packed_##name(pbf_tag_type(tag), first, last); \
}
PROTOZERO_WRITER_WRAP_ADD_PACKED(bool)
PROTOZERO_WRITER_WRAP_ADD_PACKED(enum)
PROTOZERO_WRITER_WRAP_ADD_PACKED(int32)
PROTOZERO_WRITER_WRAP_ADD_PACKED(sint32)
PROTOZERO_WRITER_WRAP_ADD_PACKED(uint32)
PROTOZERO_WRITER_WRAP_ADD_PACKED(int64)
PROTOZERO_WRITER_WRAP_ADD_PACKED(sint64)
PROTOZERO_WRITER_WRAP_ADD_PACKED(uint64)
PROTOZERO_WRITER_WRAP_ADD_PACKED(fixed32)
PROTOZERO_WRITER_WRAP_ADD_PACKED(sfixed32)
PROTOZERO_WRITER_WRAP_ADD_PACKED(fixed64)
PROTOZERO_WRITER_WRAP_ADD_PACKED(sfixed64)
PROTOZERO_WRITER_WRAP_ADD_PACKED(float)
PROTOZERO_WRITER_WRAP_ADD_PACKED(double)
#undef PROTOZERO_WRITER_WRAP_ADD_PACKED
}; // class basic_pbf_builder
} // end namespace protozero
#endif // PROTOZERO_BASIC_PBF_BUILDER_HPP
basic_pbf_writer.hpp
Contains the basic_pbf_writer template class.
protozero::basic_pbf_builder::add_bytes_vectored
void add_bytes_vectored(T tag, Ts &&... values)
Definition basic_pbf_builder.hpp:160
protozero::basic_pbf_builder::add_string
void add_string(T tag, const char *value)
Definition basic_pbf_builder.hpp:202
protozero::basic_pbf_builder::add_message
void add_message(T tag, const char *value, std::size_t size)
Definition basic_pbf_builder.hpp:213
protozero::basic_pbf_builder::enum_type
T enum_type
The type of messages this class will build.
Definition basic_pbf_builder.hpp:49
protozero::basic_pbf_builder::add_bytes
void add_bytes(T tag, const char *value)
Definition basic_pbf_builder.hpp:136
protozero::basic_pbf_builder::add_message
void add_message(T tag, const data_view &value)
Definition basic_pbf_builder.hpp:223
protozero::basic_pbf_builder::add_string
void add_string(T tag, const char *value, std::size_t size)
Definition basic_pbf_builder.hpp:171
protozero::basic_pbf_builder::add_string
void add_string(T tag, const std::string &value)
Definition basic_pbf_builder.hpp:191
protozero::basic_pbf_builder::add_string
void add_string(T tag, const data_view &value)
Definition basic_pbf_builder.hpp:181
protozero::basic_pbf_builder::add_bytes
void add_bytes(T tag, const char *value, std::size_t size)
Definition basic_pbf_builder.hpp:105
protozero::basic_pbf_builder::add_bytes
void add_bytes(T tag, const data_view &value)
Definition basic_pbf_builder.hpp:115
protozero::basic_pbf_builder::basic_pbf_builder
basic_pbf_builder(basic_pbf_writer< TBuffer > &parent_writer, P tag)
Definition basic_pbf_builder.hpp:70
protozero::basic_pbf_builder::add_message
void add_message(T tag, const std::string &value)
Definition basic_pbf_builder.hpp:233
protozero::basic_pbf_builder::add_bytes
void add_bytes(T tag, const std::string &value)
Definition basic_pbf_builder.hpp:125
protozero::basic_pbf_builder::basic_pbf_builder
basic_pbf_builder(TBuffer &data) noexcept
Definition basic_pbf_builder.hpp:58
protozero::basic_pbf_writer::add_string
void add_string(pbf_tag_type tag, const char *value, std::size_t size)
Definition basic_pbf_writer.hpp:617
protozero::basic_pbf_writer::add_bytes
void add_bytes(pbf_tag_type tag, const char *value, std::size_t size)
Definition basic_pbf_writer.hpp:540
protozero::basic_pbf_writer::add_bytes_vectored
void add_bytes_vectored(pbf_tag_type tag, Ts &&... values)
Definition basic_pbf_writer.hpp:599
protozero::basic_pbf_writer::basic_pbf_writer
basic_pbf_writer(TBuffer &buffer) noexcept
Definition basic_pbf_writer.hpp:236
protozero::basic_pbf_writer::add_message
void add_message(pbf_tag_type tag, const char *value, std::size_t size)
Definition basic_pbf_writer.hpp:659
protozero::data_view
Definition data_view.hpp:39
protozero
All parts of the protozero header-only library are in this namespace.
Definition basic_pbf_builder.hpp:24
protozero::pbf_tag_type
uint32_t pbf_tag_type
Definition types.hpp:33
types.hpp
Contains the declaration of low-level types used in the pbf format.