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/types.hpp>
22 #include <protozero/pbf_writer.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  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 std::string& value) {
88  }
89 
90  void add_string(T tag, const char* value, std::size_t size) {
91  pbf_writer::add_string(pbf_tag_type(tag), value, size);
92  }
93 
94  void add_string(T tag, const std::string& value) {
96  }
97 
98  void add_string(T tag, const char* value) {
100  }
101 
102  void add_message(T tag, const char* value, std::size_t size) {
103  pbf_writer::add_message(pbf_tag_type(tag), value, size);
104  }
105 
106  void add_message(T tag, const std::string& value) {
108  }
109 
111 #define PROTOZERO_WRITER_WRAP_ADD_PACKED(name) \
112  template <typename InputIterator> \
113  void add_packed_##name(T tag, InputIterator first, InputIterator last) { \
114  pbf_writer::add_packed_##name(pbf_tag_type(tag), first, last); \
115  }
116 
117  PROTOZERO_WRITER_WRAP_ADD_PACKED(bool)
118  PROTOZERO_WRITER_WRAP_ADD_PACKED(enum)
119  PROTOZERO_WRITER_WRAP_ADD_PACKED(int32)
120  PROTOZERO_WRITER_WRAP_ADD_PACKED(sint32)
121  PROTOZERO_WRITER_WRAP_ADD_PACKED(uint32)
122  PROTOZERO_WRITER_WRAP_ADD_PACKED(int64)
123  PROTOZERO_WRITER_WRAP_ADD_PACKED(sint64)
124  PROTOZERO_WRITER_WRAP_ADD_PACKED(uint64)
125  PROTOZERO_WRITER_WRAP_ADD_PACKED(fixed32)
126  PROTOZERO_WRITER_WRAP_ADD_PACKED(sfixed32)
127  PROTOZERO_WRITER_WRAP_ADD_PACKED(fixed64)
128  PROTOZERO_WRITER_WRAP_ADD_PACKED(sfixed64)
129  PROTOZERO_WRITER_WRAP_ADD_PACKED(float)
130  PROTOZERO_WRITER_WRAP_ADD_PACKED(double)
131 
132 #undef PROTOZERO_WRITER_WRAP_ADD_PACKED
133 
135 }; // class pbf_builder
136 
137 } // end namespace protozero
138 
139 #endif // PROTOZERO_PBF_BUILDER_HPP
void add_message(pbf_tag_type tag, const char *value, std::size_t size)
Definition: pbf_writer.hpp:559
void add_string(pbf_tag_type tag, const char *value, std::size_t size)
Definition: pbf_writer.hpp:517
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:240
uint32_t pbf_tag_type
Definition: types.hpp:32
void add_bytes(pbf_tag_type tag, const char *value, std::size_t size)
Definition: pbf_writer.hpp:482
All parts of the protozero header-only library are in this namespace.
Definition: byteswap.hpp:24