protozero 1.7.1 Minimalistic protocol buffer decoder and encoder in C++.

Loading...
Searching...
No Matches
basic_pbf_builder.hpp
Go to the documentation of this file.
1#ifndef PROTOZERO_BASIC_PBF_BUILDER_HPP
2#define PROTOZERO_BASIC_PBF_BUILDER_HPP
3
4/*****************************************************************************
5
6 protozero - Minimalistic protocol buffer decoder and encoder in C++.
7
8 This file is from https://github.com/mapbox/protozero where you can find more
9 documentation.
10
11*****************************************************************************/
12
19#include "basic_pbf_writer.hpp"
20#include "types.hpp"
21
22#include <type_traits>
23
24 namespace protozero {
25
40 template <typename TBuffer, typename T>
41 class basic_pbf_builder : public basic_pbf_writer<TBuffer> {
42
43 static_assert(std::is_same<pbf_tag_type, typename std::underlying_type<T>::type>::value,
44 "T must be enum with underlying type protozero::pbf_tag_type");
45
46 public:
47
49 using enum_type = T;
50
51 basic_pbf_builder() = default;
52
58 explicit basic_pbf_builder(TBuffer& data) noexcept :
59 basic_pbf_writer<TBuffer>{data} {
60 }
61
69 template <typename P>
70 basic_pbf_builder(basic_pbf_writer<TBuffer>& parent_writer, P tag) noexcept :
71 basic_pbf_writer<TBuffer>{parent_writer, pbf_tag_type(tag)} {
72 }
73
75#define PROTOZERO_WRITER_WRAP_ADD_SCALAR(name, type) \
76 void add_##name(T tag, type value) { \
77 basic_pbf_writer<TBuffer>::add_##name(pbf_tag_type(tag), value); \
78 }
79
80 PROTOZERO_WRITER_WRAP_ADD_SCALAR(bool, bool)
81 PROTOZERO_WRITER_WRAP_ADD_SCALAR(enum, int32_t)
82 PROTOZERO_WRITER_WRAP_ADD_SCALAR(int32, int32_t)
83 PROTOZERO_WRITER_WRAP_ADD_SCALAR(sint32, int32_t)
84 PROTOZERO_WRITER_WRAP_ADD_SCALAR(uint32, uint32_t)
85 PROTOZERO_WRITER_WRAP_ADD_SCALAR(int64, int64_t)
86 PROTOZERO_WRITER_WRAP_ADD_SCALAR(sint64, int64_t)
87 PROTOZERO_WRITER_WRAP_ADD_SCALAR(uint64, uint64_t)
88 PROTOZERO_WRITER_WRAP_ADD_SCALAR(fixed32, uint32_t)
89 PROTOZERO_WRITER_WRAP_ADD_SCALAR(sfixed32, int32_t)
90 PROTOZERO_WRITER_WRAP_ADD_SCALAR(fixed64, uint64_t)
91 PROTOZERO_WRITER_WRAP_ADD_SCALAR(sfixed64, int64_t)
92 PROTOZERO_WRITER_WRAP_ADD_SCALAR(float, float)
93 PROTOZERO_WRITER_WRAP_ADD_SCALAR(double, double)
94
95#undef PROTOZERO_WRITER_WRAP_ADD_SCALAR
97
105 void add_bytes(T tag, const char* value, std::size_t size) {
106 basic_pbf_writer<TBuffer>::add_bytes(pbf_tag_type(tag), value, size);
107 }
108
115 void add_bytes(T tag, const data_view& value) {
116 basic_pbf_writer<TBuffer>::add_bytes(pbf_tag_type(tag), value);
117 }
118
125 void add_bytes(T tag, const std::string& value) {
126 basic_pbf_writer<TBuffer>::add_bytes(pbf_tag_type(tag), value);
127 }
128
136 void add_bytes(T tag, const char* value) {
137 basic_pbf_writer<TBuffer>::add_bytes(pbf_tag_type(tag), value);
138 }
139
159 template <typename... Ts>
160 void add_bytes_vectored(T tag, Ts&&... values) {
161 basic_pbf_writer<TBuffer>::add_bytes_vectored(pbf_tag_type(tag), std::forward<Ts>(values)...);
162 }
163
171 void add_string(T tag, const char* value, std::size_t size) {
172 basic_pbf_writer<TBuffer>::add_string(pbf_tag_type(tag), value, size);
173 }
174
181 void add_string(T tag, const data_view& value) {
182 basic_pbf_writer<TBuffer>::add_string(pbf_tag_type(tag), value);
183 }
184
191 void add_string(T tag, const std::string& value) {
192 basic_pbf_writer<TBuffer>::add_string(pbf_tag_type(tag), value);
193 }
194
202 void add_string(T tag, const char* value) {
203 basic_pbf_writer<TBuffer>::add_string(pbf_tag_type(tag), value);
204 }
205
213 void add_message(T tag, const char* value, std::size_t size) {
214 basic_pbf_writer<TBuffer>::add_message(pbf_tag_type(tag), value, size);
215 }
216
223 void add_message(T tag, const data_view& value) {
224 basic_pbf_writer<TBuffer>::add_message(pbf_tag_type(tag), value);
225 }
226
233 void add_message(T tag, const std::string& value) {
234 basic_pbf_writer<TBuffer>::add_message(pbf_tag_type(tag), value);
235 }
236
238#define PROTOZERO_WRITER_WRAP_ADD_PACKED(name) \
239 template <typename InputIterator> \
240 void add_packed_##name(T tag, InputIterator first, InputIterator last) { \
241 basic_pbf_writer<TBuffer>::add_packed_##name(pbf_tag_type(tag), first, last); \
242 }
243
244 PROTOZERO_WRITER_WRAP_ADD_PACKED(bool)
245 PROTOZERO_WRITER_WRAP_ADD_PACKED(enum)
246 PROTOZERO_WRITER_WRAP_ADD_PACKED(int32)
247 PROTOZERO_WRITER_WRAP_ADD_PACKED(sint32)
248 PROTOZERO_WRITER_WRAP_ADD_PACKED(uint32)
249 PROTOZERO_WRITER_WRAP_ADD_PACKED(int64)
250 PROTOZERO_WRITER_WRAP_ADD_PACKED(sint64)
251 PROTOZERO_WRITER_WRAP_ADD_PACKED(uint64)
252 PROTOZERO_WRITER_WRAP_ADD_PACKED(fixed32)
253 PROTOZERO_WRITER_WRAP_ADD_PACKED(sfixed32)
254 PROTOZERO_WRITER_WRAP_ADD_PACKED(fixed64)
255 PROTOZERO_WRITER_WRAP_ADD_PACKED(sfixed64)
256 PROTOZERO_WRITER_WRAP_ADD_PACKED(float)
257 PROTOZERO_WRITER_WRAP_ADD_PACKED(double)
258
259#undef PROTOZERO_WRITER_WRAP_ADD_PACKED
261
262}; // class basic_pbf_builder
263
264} // end namespace protozero
265
266#endif // PROTOZERO_BASIC_PBF_BUILDER_HPP
basic_pbf_writer.hpp
Contains the basic_pbf_writer template class.
protozero::basic_pbf_builder
Definition basic_pbf_builder.hpp:41
protozero::basic_pbf_builder::basic_pbf_builder
basic_pbf_builder(basic_pbf_writer< TBuffer > &parent_writer, P tag) noexcept
Definition basic_pbf_builder.hpp:70
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::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
Definition basic_pbf_writer.hpp:59
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::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.