protozero  1.6.3
Minimalistic protocol buffer decoder and encoder in C++.
data_view.hpp
Go to the documentation of this file.
1 #ifndef PROTOZERO_DATA_VIEW_HPP
2 #define PROTOZERO_DATA_VIEW_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 <protozero/config.hpp>
20 
21 #include <algorithm>
22 #include <cstddef>
23 #include <cstring>
24 #include <string>
25 #include <utility>
26 
27 namespace protozero {
28 
29 #ifdef PROTOZERO_USE_VIEW
30 using data_view = PROTOZERO_USE_VIEW;
31 #else
32 
39 class data_view {
40 
41  const char* m_data = nullptr;
42  std::size_t m_size = 0;
43 
44 public:
45 
49  constexpr data_view() noexcept = default;
50 
57  constexpr data_view(const char* ptr, std::size_t length) noexcept
58  : m_data(ptr),
59  m_size(length) {
60  }
61 
67  data_view(const std::string& str) noexcept // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
68  : m_data(str.data()),
69  m_size(str.size()) {
70  }
71 
77  data_view(const char* ptr) noexcept // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
78  : m_data(ptr),
79  m_size(std::strlen(ptr)) {
80  }
81 
87  void swap(data_view& other) noexcept {
88  using std::swap;
89  swap(m_data, other.m_data);
90  swap(m_size, other.m_size);
91  }
92 
94  constexpr const char* data() const noexcept {
95  return m_data;
96  }
97 
99  constexpr std::size_t size() const noexcept {
100  return m_size;
101  }
102 
104  constexpr bool empty() const noexcept {
105  return m_size == 0;
106  }
107 
108 #ifndef PROTOZERO_STRICT_API
109 
118  std::string to_string() const {
119  protozero_assert(m_data);
120  return {m_data, m_size};
121  }
122 #endif
123 
129  explicit operator std::string() const {
130  protozero_assert(m_data);
131  return {m_data, m_size};
132  }
133 
144  int compare(data_view other) const {
145  protozero_assert(m_data && other.m_data);
146  const int cmp = std::memcmp(data(), other.data(),
147  std::min(size(), other.size()));
148  if (cmp == 0) {
149  if (size() == other.size()) {
150  return 0;
151  }
152  return size() < other.size() ? -1 : 1;
153  }
154  return cmp;
155  }
156 
157 }; // class data_view
158 
165 inline void swap(data_view& lhs, data_view& rhs) noexcept {
166  lhs.swap(rhs);
167 }
168 
176 inline constexpr bool operator==(const data_view lhs, const data_view rhs) noexcept {
177  return lhs.size() == rhs.size() &&
178  std::equal(lhs.data(), lhs.data() + lhs.size(), rhs.data());
179 }
180 
188 inline constexpr bool operator!=(const data_view lhs, const data_view rhs) noexcept {
189  return !(lhs == rhs);
190 }
191 
198 inline bool operator<(const data_view lhs, const data_view rhs) noexcept {
199  return lhs.compare(rhs) < 0;
200 }
201 
208 inline bool operator<=(const data_view lhs, const data_view rhs) noexcept {
209  return lhs.compare(rhs) <= 0;
210 }
211 
218 inline bool operator>(const data_view lhs, const data_view rhs) noexcept {
219  return lhs.compare(rhs) > 0;
220 }
221 
228 inline bool operator>=(const data_view lhs, const data_view rhs) noexcept {
229  return lhs.compare(rhs) >= 0;
230 }
231 
232 #endif
233 
234 } // end namespace protozero
235 
236 #endif // PROTOZERO_DATA_VIEW_HPP
int compare(data_view other) const
Definition: data_view.hpp:144
constexpr bool operator==(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:176
constexpr bool empty() const noexcept
Returns true if size is 0.
Definition: data_view.hpp:104
data_view(const std::string &str) noexcept
Definition: data_view.hpp:67
std::string to_string() const
Definition: data_view.hpp:118
Contains macro checks for different configurations.
bool operator<=(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:208
bool operator>=(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:228
constexpr std::size_t size() const noexcept
Return length of data in bytes.
Definition: data_view.hpp:99
data_view(const char *ptr) noexcept
Definition: data_view.hpp:77
constexpr data_view() noexcept=default
bool operator<(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:198
bool operator>(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:218
void swap(data_view &other) noexcept
Definition: data_view.hpp:87
Definition: data_view.hpp:39
constexpr bool operator!=(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:188
constexpr const char * data() const noexcept
Return pointer to data.
Definition: data_view.hpp:94
constexpr data_view(const char *ptr, std::size_t length) noexcept
Definition: data_view.hpp:57
All parts of the protozero header-only library are in this namespace.
Definition: byteswap.hpp:23
void swap(data_view &lhs, data_view &rhs) noexcept
Definition: data_view.hpp:165