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 <algorithm>
20 #include <cstddef>
21 #include <cstdint>
22 #include <cstring>
23 #include <string>
24 #include <utility>
25 
26 #include <protozero/config.hpp>
27 
28 namespace protozero {
29 
33 using pbf_tag_type = uint32_t;
34 
40 enum class pbf_wire_type : uint32_t {
41  varint = 0, // int32/64, uint32/64, sint32/64, bool, enum
42  fixed64 = 1, // fixed64, sfixed64, double
43  length_delimited = 2, // string, bytes, embedded messages,
44  // packed repeated fields
45  fixed32 = 5, // fixed32, sfixed32, float
46  unknown = 99 // used for default setting in this library
47 };
48 
55 template <typename T>
56 constexpr inline uint32_t tag_and_type(T tag, pbf_wire_type wire_type) noexcept {
57  return (static_cast<uint32_t>(static_cast<pbf_tag_type>(tag)) << 3) | static_cast<uint32_t>(wire_type);
58 }
59 
63 using pbf_length_type = uint32_t;
64 
65 #ifdef PROTOZERO_USE_VIEW
66 using data_view = PROTOZERO_USE_VIEW;
67 #else
68 
75 class data_view {
76 
77  const char* m_data = nullptr;
78  std::size_t m_size = 0;
79 
80 public:
81 
85  constexpr data_view() noexcept = default;
86 
93  constexpr data_view(const char* ptr, std::size_t length) noexcept
94  : m_data(ptr),
95  m_size(length) {
96  }
97 
103  data_view(const std::string& str) noexcept // NOLINT clang-tidy: google-explicit-constructor
104  : m_data(str.data()),
105  m_size(str.size()) {
106  }
107 
113  data_view(const char* ptr) noexcept // NOLINT clang-tidy: google-explicit-constructor
114  : m_data(ptr),
115  m_size(std::strlen(ptr)) {
116  }
117 
123  void swap(data_view& other) noexcept {
124  using std::swap;
125  swap(m_data, other.m_data);
126  swap(m_size, other.m_size);
127  }
128 
130  constexpr const char* data() const noexcept {
131  return m_data;
132  }
133 
135  constexpr std::size_t size() const noexcept {
136  return m_size;
137  }
138 
140  constexpr bool empty() const noexcept {
141  return m_size == 0;
142  }
143 
153  std::string to_string() const {
154  protozero_assert(m_data);
155  return {m_data, m_size};
156  }
157 
163  explicit operator std::string() const {
164  protozero_assert(m_data);
165  return {m_data, m_size};
166  }
167 
168 }; // class data_view
169 
176 inline void swap(data_view& lhs, data_view& rhs) noexcept {
177  lhs.swap(rhs);
178 }
179 
187 inline bool operator==(const data_view& lhs, const data_view& rhs) noexcept {
188  return lhs.size() == rhs.size() && std::equal(lhs.data(), lhs.data() + lhs.size(), rhs.data());
189 }
190 
198 inline bool operator!=(const data_view& lhs, const data_view& rhs) noexcept {
199  return !(lhs == rhs);
200 }
201 
202 #endif
203 
204 
205 } // end namespace protozero
206 
207 #endif // PROTOZERO_TYPES_HPP
constexpr bool empty() const noexcept
Returns true if size is 0.
Definition: types.hpp:140
data_view(const std::string &str) noexcept
Definition: types.hpp:103
constexpr uint32_t tag_and_type(T tag, pbf_wire_type wire_type) noexcept
Definition: types.hpp:56
std::string to_string() const
Definition: types.hpp:153
Contains macro checks for different configurations.
void swap(iterator_range< T > &lhs, iterator_range< T > &rhs) noexcept
Definition: iterators.hpp:151
constexpr std::size_t size() const noexcept
Return length of data in bytes.
Definition: types.hpp:135
pbf_wire_type
Definition: types.hpp:40
data_view(const char *ptr) noexcept
Definition: types.hpp:113
void swap(data_view &other) noexcept
Definition: types.hpp:123
uint32_t pbf_length_type
Definition: types.hpp:63
uint32_t pbf_tag_type
Definition: types.hpp:33
Definition: types.hpp:75
bool operator==(const data_view &lhs, const data_view &rhs) noexcept
Definition: types.hpp:187
bool operator!=(const data_view &lhs, const data_view &rhs) noexcept
Definition: types.hpp:198
constexpr const char * data() const noexcept
Return pointer to data.
Definition: types.hpp:130
constexpr data_view(const char *ptr, std::size_t length) noexcept
Definition: types.hpp:93
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:176