protozero
Minimalistic protocol buffer decoder and encoder in C++.
pbf_builder.hpp
Go to the documentation of this file.
1 #ifndef PROTOZERO_PBF_BUILDER_HPP
2 #define PROTOZERO_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 <type_traits>
20 
21 #include <protozero/pbf_writer.hpp>
22 #include <protozero/types.hpp>
23 
24 namespace protozero {
25 
39 template <typename T>
40 class pbf_builder : public pbf_writer {
41 
42  static_assert(std::is_same<pbf_tag_type, typename std::underlying_type<T>::type>::value,
43  "T must be enum with underlying type protozero::pbf_tag_type");
44 
45 public:
46 
47  using enum_type = T;
48 
49  explicit pbf_builder(std::string& data) noexcept :
50  pbf_writer(data) {
51  }
52 
53  template <typename P>
54  pbf_builder(pbf_writer& parent_writer, P tag) noexcept :
55  pbf_writer(parent_writer, pbf_tag_type(tag)) {
56  }
57 
59 #define PROTOZERO_WRITER_WRAP_ADD_SCALAR(name, type) \
60  void add_##name(T tag, type value) { \
61  pbf_writer::add_##name(pbf_tag_type(tag), value); \
62  }
63 
64  PROTOZERO_WRITER_WRAP_ADD_SCALAR(bool, bool)
65  PROTOZERO_WRITER_WRAP_ADD_SCALAR(enum, int32_t)
66  PROTOZERO_WRITER_WRAP_ADD_SCALAR(int32, int32_t)
67  PROTOZERO_WRITER_WRAP_ADD_SCALAR(sint32, int32_t)
68  PROTOZERO_WRITER_WRAP_ADD_SCALAR(uint32, uint32_t)
69  PROTOZERO_WRITER_WRAP_ADD_SCALAR(int64, int64_t)
70  PROTOZERO_WRITER_WRAP_ADD_SCALAR(sint64, int64_t)
71  PROTOZERO_WRITER_WRAP_ADD_SCALAR(uint64, uint64_t)
72  PROTOZERO_WRITER_WRAP_ADD_SCALAR(fixed32, uint32_t)
73  PROTOZERO_WRITER_WRAP_ADD_SCALAR(sfixed32, int32_t)
74  PROTOZERO_WRITER_WRAP_ADD_SCALAR(fixed64, uint64_t)
75  PROTOZERO_WRITER_WRAP_ADD_SCALAR(sfixed64, int64_t)
76  PROTOZERO_WRITER_WRAP_ADD_SCALAR(float, float)
77  PROTOZERO_WRITER_WRAP_ADD_SCALAR(double, double)
78 
79 #undef PROTOZERO_WRITER_WRAP_ADD_SCALAR
80 
82  void add_bytes(T tag, const char* value, std::size_t size) {
83  pbf_writer::add_bytes(pbf_tag_type(tag), value, size);
84  }
85 
86  void add_bytes(T tag, const data_view& value) {
88  }
89 
90  void add_bytes(T tag, const std::string& value) {
92  }
93 
94  void add_bytes(T tag, const char* value) {
96  }
97 
98  template <typename... Ts>
99  void add_bytes_vectored(T tag, Ts&&... values) {
100  pbf_writer::add_bytes_vectored(pbf_tag_type(tag), std::forward<Ts>(values)...);
101  }
102 
103  void add_string(T tag, const char* value, std::size_t size) {
104  pbf_writer::add_string(pbf_tag_type(tag), value, size);
105  }
106 
107  void add_string(T tag, const data_view& value) {
109  }
110 
111  void add_string(T tag, const std::string& value) {
113  }
114 
115  void add_string(T tag, const char* value) {
117  }
118 
119  void add_message(T tag, const char* value, std::size_t size) {
120  pbf_writer::add_message(pbf_tag_type(tag), value, size);
121  }
122 
123  void add_message(T tag, const data_view& value) {
125  }
126 
127  void add_message(T tag, const std::string& value) {
129  }
130 
132 #define PROTOZERO_WRITER_WRAP_ADD_PACKED(name) \
133  template <typename InputIterator> \
134  void add_packed_##name(T tag, InputIterator first, InputIterator last) { \
135  pbf_writer::add_packed_##name(pbf_tag_type(tag), first, last); \
136  }
137 
138  PROTOZERO_WRITER_WRAP_ADD_PACKED(bool)
139  PROTOZERO_WRITER_WRAP_ADD_PACKED(enum)
140  PROTOZERO_WRITER_WRAP_ADD_PACKED(int32)
141  PROTOZERO_WRITER_WRAP_ADD_PACKED(sint32)
142  PROTOZERO_WRITER_WRAP_ADD_PACKED(uint32)
143  PROTOZERO_WRITER_WRAP_ADD_PACKED(int64)
144  PROTOZERO_WRITER_WRAP_ADD_PACKED(sint64)
145  PROTOZERO_WRITER_WRAP_ADD_PACKED(uint64)
146  PROTOZERO_WRITER_WRAP_ADD_PACKED(fixed32)
147  PROTOZERO_WRITER_WRAP_ADD_PACKED(sfixed32)
148  PROTOZERO_WRITER_WRAP_ADD_PACKED(fixed64)
149  PROTOZERO_WRITER_WRAP_ADD_PACKED(sfixed64)
150  PROTOZERO_WRITER_WRAP_ADD_PACKED(float)
151  PROTOZERO_WRITER_WRAP_ADD_PACKED(double)
152 
153 #undef PROTOZERO_WRITER_WRAP_ADD_PACKED
154 
156 }; // class pbf_builder
157 
158 } // end namespace protozero
159 
160 #endif // PROTOZERO_PBF_BUILDER_HPP
void add_message(pbf_tag_type tag, const char *value, std::size_t size)
Definition: pbf_writer.hpp:595
void add_string(pbf_tag_type tag, const char *value, std::size_t size)
Definition: pbf_writer.hpp:553
Definition: pbf_writer.hpp:51
Contains the declaration of low-level types used in the pbf format.
Definition: pbf_builder.hpp:40
Contains the pbf_writer class.
pbf_writer() noexcept
Definition: pbf_writer.hpp:236
void add_bytes_vectored(pbf_tag_type tag, Ts &&... values)
Definition: pbf_writer.hpp:535
uint32_t pbf_tag_type
Definition: types.hpp:33
Definition: types.hpp:75
void add_bytes(pbf_tag_type tag, const char *value, std::size_t size)
Definition: pbf_writer.hpp:476
All parts of the protozero header-only library are in this namespace.
Definition: byteswap.hpp:24