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_types.hpp>
22 #include <protozero/pbf_writer.hpp>
23 
24 namespace protozero {
25 
37 template <typename T>
38 class pbf_builder : public pbf_writer {
39 
40  static_assert(std::is_same<pbf_tag_type, typename std::underlying_type<T>::type>::value,
41  "T must be enum with underlying type protozero::pbf_tag_type");
42 
43 public:
44 
45  using enum_type = T;
46 
47  pbf_builder(std::string& data) noexcept :
48  pbf_writer(data) {
49  }
50 
51  template <typename P>
52  pbf_builder(pbf_writer& parent_writer, P tag) noexcept :
53  pbf_writer(parent_writer, pbf_tag_type(tag)) {
54  }
55 
57 #define PROTOZERO_WRITER_WRAP_ADD_SCALAR(name, type) \
58  inline void add_##name(T tag, type value) { \
59  pbf_writer::add_##name(pbf_tag_type(tag), value); \
60  }
61 
62  PROTOZERO_WRITER_WRAP_ADD_SCALAR(bool, bool)
63  PROTOZERO_WRITER_WRAP_ADD_SCALAR(enum, int32_t)
64  PROTOZERO_WRITER_WRAP_ADD_SCALAR(int32, int32_t)
65  PROTOZERO_WRITER_WRAP_ADD_SCALAR(sint32, int32_t)
66  PROTOZERO_WRITER_WRAP_ADD_SCALAR(uint32, uint32_t)
67  PROTOZERO_WRITER_WRAP_ADD_SCALAR(int64, int64_t)
68  PROTOZERO_WRITER_WRAP_ADD_SCALAR(sint64, int64_t)
69  PROTOZERO_WRITER_WRAP_ADD_SCALAR(uint64, uint64_t)
70  PROTOZERO_WRITER_WRAP_ADD_SCALAR(fixed32, uint32_t)
71  PROTOZERO_WRITER_WRAP_ADD_SCALAR(sfixed32, int32_t)
72  PROTOZERO_WRITER_WRAP_ADD_SCALAR(fixed64, uint64_t)
73  PROTOZERO_WRITER_WRAP_ADD_SCALAR(sfixed64, int64_t)
74  PROTOZERO_WRITER_WRAP_ADD_SCALAR(float, float)
75  PROTOZERO_WRITER_WRAP_ADD_SCALAR(double, double)
76 
77 #undef PROTOZERO_WRITER_WRAP_ADD_SCALAR
78 
80  inline void add_bytes(T tag, const char* value, size_t size) {
81  pbf_writer::add_bytes(pbf_tag_type(tag), value, size);
82  }
83 
84  inline void add_bytes(T tag, const std::string& value) {
86  }
87 
88  inline void add_string(T tag, const char* value, size_t size) {
89  pbf_writer::add_string(pbf_tag_type(tag), value, size);
90  }
91 
92  inline void add_string(T tag, const std::string& value) {
94  }
95 
96  inline void add_string(T tag, const char* value) {
98  }
99 
100  inline void add_message(T tag, const char* value, size_t size) {
101  pbf_writer::add_message(pbf_tag_type(tag), value, size);
102  }
103 
104  inline void add_message(T tag, const std::string& value) {
106  }
107 
109 #define PROTOZERO_WRITER_WRAP_ADD_PACKED(name) \
110  template <typename InputIterator> \
111  inline void add_packed_##name(T tag, InputIterator first, InputIterator last) { \
112  pbf_writer::add_packed_##name(pbf_tag_type(tag), first, last); \
113  }
114 
115  PROTOZERO_WRITER_WRAP_ADD_PACKED(bool)
116  PROTOZERO_WRITER_WRAP_ADD_PACKED(enum)
117  PROTOZERO_WRITER_WRAP_ADD_PACKED(int32)
118  PROTOZERO_WRITER_WRAP_ADD_PACKED(sint32)
119  PROTOZERO_WRITER_WRAP_ADD_PACKED(uint32)
120  PROTOZERO_WRITER_WRAP_ADD_PACKED(int64)
121  PROTOZERO_WRITER_WRAP_ADD_PACKED(sint64)
122  PROTOZERO_WRITER_WRAP_ADD_PACKED(uint64)
123  PROTOZERO_WRITER_WRAP_ADD_PACKED(fixed32)
124  PROTOZERO_WRITER_WRAP_ADD_PACKED(sfixed32)
125  PROTOZERO_WRITER_WRAP_ADD_PACKED(fixed64)
126  PROTOZERO_WRITER_WRAP_ADD_PACKED(sfixed64)
127  PROTOZERO_WRITER_WRAP_ADD_PACKED(float)
128  PROTOZERO_WRITER_WRAP_ADD_PACKED(double)
129 
130 #undef PROTOZERO_WRITER_WRAP_ADD_PACKED
131 
133 };
134 
135 } // end namespace protozero
136 
137 #endif // PROTOZERO_PBF_BUILDER_HPP
Definition: pbf_writer.hpp:42
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:38
Contains the pbf_writer class.
pbf_writer() noexcept
Definition: pbf_writer.hpp:176
void add_bytes(pbf_tag_type tag, const char *value, size_t size)
Definition: pbf_writer.hpp:375
void add_string(pbf_tag_type tag, const char *value, size_t size)
Definition: pbf_writer.hpp:400
void add_message(pbf_tag_type tag, const char *value, size_t size)
Definition: pbf_writer.hpp:432
All parts of the protozero header-only library are in this namespace.
Definition: byteswap.hpp:24