protozero
Minimalistic protocol buffer decoder and encoder in C++.
types.hpp
Go to the documentation of this file.
1 #ifndef PROTOZERO_TYPES_HPP
2 #define PROTOZERO_TYPES_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 <cstddef>
20 #include <cstdint>
21 #include <cstring>
22 #include <string>
23 #include <utility>
24 
25 #include <protozero/config.hpp>
26 
27 namespace protozero {
28 
32 using pbf_tag_type = uint32_t;
33 
39 enum class pbf_wire_type : uint32_t {
40  varint = 0, // int32/64, uint32/64, sint32/64, bool, enum
41  fixed64 = 1, // fixed64, sfixed64, double
42  length_delimited = 2, // string, bytes, embedded messages,
43  // packed repeated fields
44  fixed32 = 5, // fixed32, sfixed32, float
45  unknown = 99 // used for default setting in this library
46 };
47 
51 using pbf_length_type = uint32_t;
52 
53 #ifdef PROTOZERO_USE_VIEW
54 using data_view = PROTOZERO_USE_VIEW;
55 #else
56 
63 class data_view {
64 
65  const char* m_data;
66  std::size_t m_size;
67 
68 public:
69 
73  constexpr data_view() noexcept
74  : m_data(nullptr),
75  m_size(0) {
76  }
77 
84  constexpr data_view(const char* data, std::size_t size) noexcept
85  : m_data(data),
86  m_size(size) {
87  }
88 
94  data_view(const std::string& str) noexcept
95  : m_data(str.data()),
96  m_size(str.size()) {
97  }
98 
104  data_view(const char* data) noexcept
105  : m_data(data),
106  m_size(std::strlen(data)) {
107  }
108 
114  void swap(data_view& other) noexcept {
115  using std::swap;
116  swap(m_data, other.m_data);
117  swap(m_size, other.m_size);
118  }
119 
121  constexpr const char* data() const noexcept {
122  return m_data;
123  }
124 
126  constexpr std::size_t size() const noexcept {
127  return m_size;
128  }
129 
135  std::string to_string() const {
136  protozero_assert(m_data);
137  return std::string{m_data, m_size};
138  }
139 
145  explicit operator std::string() const {
146  protozero_assert(m_data);
147  return std::string{m_data, m_size};
148  }
149 
150 }; // class data_view
151 
158 inline void swap(data_view& lhs, data_view& rhs) noexcept {
159  lhs.swap(rhs);
160 }
161 
169 inline bool operator==(data_view& lhs, data_view& rhs) noexcept {
170  return lhs.size() == rhs.size() && !std::strcmp(lhs.data(), rhs.data());
171 }
172 
180 inline bool operator!=(data_view& lhs, data_view& rhs) noexcept {
181  return !(lhs == rhs);
182 }
183 
184 #endif
185 
186 
187 } // end namespace protozero
188 
189 #endif // PROTOZERO_TYPES_HPP
data_view(const std::string &str) noexcept
Definition: types.hpp:94
std::string to_string() const
Definition: types.hpp:135
Contains macro checks for different configurations.
bool operator!=(data_view &lhs, data_view &rhs) noexcept
Definition: types.hpp:180
void swap(iterator_range< T > &lhs, iterator_range< T > &rhs) noexcept
Definition: iterators.hpp:152
data_view(const char *data) noexcept
Definition: types.hpp:104
constexpr std::size_t size() const noexcept
Return length of data in bytes.
Definition: types.hpp:126
pbf_wire_type
Definition: types.hpp:39
constexpr data_view() noexcept
Definition: types.hpp:73
void swap(data_view &other) noexcept
Definition: types.hpp:114
uint32_t pbf_length_type
Definition: types.hpp:51
constexpr data_view(const char *data, std::size_t size) noexcept
Definition: types.hpp:84
uint32_t pbf_tag_type
Definition: types.hpp:32
Definition: types.hpp:63
constexpr const char * data() const noexcept
Return pointer to data.
Definition: types.hpp:121
bool operator==(data_view &lhs, data_view &rhs) noexcept
Definition: types.hpp:169
All parts of the protozero header-only library are in this namespace.
Definition: byteswap.hpp:24
void swap(data_view &lhs, data_view &rhs) noexcept
Definition: types.hpp:158