protozero
Minimalistic protocol buffer decoder and encoder in C++.
pbf_builder.hpp
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 
13 #include <type_traits>
14 
15 #include <protozero/pbf_types.hpp>
16 #include <protozero/pbf_writer.hpp>
17 
18 namespace protozero {
19 
20 template <typename T>
21 class pbf_builder : public pbf_writer {
22 
23  static_assert(std::is_same<pbf_tag_type, typename std::underlying_type<T>::type>::value, "T must be enum with underlying type protozero::pbf_tag_type");
24 
25 public:
26 
27  using enum_type = T;
28 
29  pbf_builder(std::string& data) noexcept :
30  pbf_writer(data) {
31  }
32 
33  template <typename P>
34  pbf_builder(pbf_writer& parent_writer, P tag) noexcept :
35  pbf_writer(parent_writer, pbf_tag_type(tag)) {
36  }
37 
38 #define PROTOZERO_WRITER_WRAP_ADD_SCALAR(name, type) \
39  inline void add_##name(T tag, type value) { \
40  pbf_writer::add_##name(pbf_tag_type(tag), value); \
41  }
42 
43  PROTOZERO_WRITER_WRAP_ADD_SCALAR(bool, bool)
44  PROTOZERO_WRITER_WRAP_ADD_SCALAR(enum, int32_t)
45  PROTOZERO_WRITER_WRAP_ADD_SCALAR(int32, int32_t)
46  PROTOZERO_WRITER_WRAP_ADD_SCALAR(sint32, int32_t)
47  PROTOZERO_WRITER_WRAP_ADD_SCALAR(uint32, uint32_t)
48  PROTOZERO_WRITER_WRAP_ADD_SCALAR(int64, int64_t)
49  PROTOZERO_WRITER_WRAP_ADD_SCALAR(sint64, int64_t)
50  PROTOZERO_WRITER_WRAP_ADD_SCALAR(uint64, uint64_t)
51  PROTOZERO_WRITER_WRAP_ADD_SCALAR(fixed32, uint32_t)
52  PROTOZERO_WRITER_WRAP_ADD_SCALAR(sfixed32, int32_t)
53  PROTOZERO_WRITER_WRAP_ADD_SCALAR(fixed64, uint64_t)
54  PROTOZERO_WRITER_WRAP_ADD_SCALAR(sfixed64, int64_t)
55  PROTOZERO_WRITER_WRAP_ADD_SCALAR(float, float)
56  PROTOZERO_WRITER_WRAP_ADD_SCALAR(double, double)
57 
58  inline void add_bytes(T tag, const char* value, size_t size) {
59  pbf_writer::add_bytes(pbf_tag_type(tag), value, size);
60  }
61 
62  inline void add_bytes(T tag, const std::string& value) {
64  }
65 
66  inline void add_string(T tag, const char* value, size_t size) {
67  pbf_writer::add_string(pbf_tag_type(tag), value, size);
68  }
69 
70  inline void add_string(T tag, const std::string& value) {
72  }
73 
74  inline void add_string(T tag, const char* value) {
76  }
77 
78  inline void add_message(T tag, const char* value, size_t size) {
79  pbf_writer::add_message(pbf_tag_type(tag), value, size);
80  }
81 
82  inline void add_message(T tag, const std::string& value) {
84  }
85 
86 #define PROTOZERO_WRITER_WRAP_ADD_PACKED(name) \
87  template <typename InputIterator> \
88  inline void add_packed_##name(T tag, InputIterator first, InputIterator last) { \
89  pbf_writer::add_packed_##name(pbf_tag_type(tag), first, last); \
90  }
91 
92  PROTOZERO_WRITER_WRAP_ADD_PACKED(bool)
93  PROTOZERO_WRITER_WRAP_ADD_PACKED(enum)
94  PROTOZERO_WRITER_WRAP_ADD_PACKED(int32)
95  PROTOZERO_WRITER_WRAP_ADD_PACKED(sint32)
96  PROTOZERO_WRITER_WRAP_ADD_PACKED(uint32)
97  PROTOZERO_WRITER_WRAP_ADD_PACKED(int64)
98  PROTOZERO_WRITER_WRAP_ADD_PACKED(sint64)
99  PROTOZERO_WRITER_WRAP_ADD_PACKED(uint64)
100  PROTOZERO_WRITER_WRAP_ADD_PACKED(fixed32)
101  PROTOZERO_WRITER_WRAP_ADD_PACKED(sfixed32)
102  PROTOZERO_WRITER_WRAP_ADD_PACKED(fixed64)
103  PROTOZERO_WRITER_WRAP_ADD_PACKED(sfixed64)
104  PROTOZERO_WRITER_WRAP_ADD_PACKED(float)
105  PROTOZERO_WRITER_WRAP_ADD_PACKED(double)
106 
107 };
108 
109 } // end namespace protozero
110 
111 #endif // PROTOZERO_PBF_BUILDER_HPP
Definition: pbf_writer.hpp:47
Contains the declaration of low-level types used in the pbf format.
uint32_t pbf_tag_type
Definition: pbf_types.hpp:26
Definition: pbf_builder.hpp:21
Contains the pbf_writer class.
pbf_writer() noexcept
Definition: pbf_writer.hpp:181
void add_bytes(pbf_tag_type tag, const char *value, size_t size)
Definition: pbf_writer.hpp:378
void add_string(pbf_tag_type tag, const char *value, size_t size)
Definition: pbf_writer.hpp:403
void add_message(pbf_tag_type tag, const char *value, size_t size)
Definition: pbf_writer.hpp:435
All parts of the protozero header-only library are in this namespace.
Definition: byteswap.hpp:15