protozero: include/protozero/data_view.hpp Source File

protozero 1.8.0 Minimalistic protocol buffer decoder and encoder in C++.

Loading...
Searching...
No Matches
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
6protozero - Minimalistic protocol buffer decoder and encoder in C++.
7
8This file is from https://github.com/mapbox/protozero where you can find more
9documentation.
10
11*****************************************************************************/
12
18
19#include "config.hpp"
20
21#include <algorithm>
22#include <cstddef>
23#include <cstring>
24#include <string>
25#include <utility>
26
27namespace protozero {
28
29#ifdef PROTOZERO_USE_VIEW
30using 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
44public:
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
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 noexcept {
145 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 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 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
protozero::data_view
Definition data_view.hpp:39
protozero::data_view::data_view
constexpr data_view() noexcept=default
protozero::data_view::empty
constexpr bool empty() const noexcept
Returns true if size is 0.
Definition data_view.hpp:104
protozero::data_view::to_string
std::string to_string() const
Definition data_view.hpp:118
protozero::data_view::compare
int compare(data_view other) const noexcept
Definition data_view.hpp:144
protozero::data_view::swap
void swap(data_view &other) noexcept
Definition data_view.hpp:87
protozero::data_view::data_view
data_view(const char *ptr) noexcept
Definition data_view.hpp:77
protozero::data_view::data_view
data_view(const std::string &str) noexcept
Definition data_view.hpp:67
protozero::data_view::size
constexpr std::size_t size() const noexcept
Return length of data in bytes.
Definition data_view.hpp:99
protozero::data_view::data
constexpr const char * data() const noexcept
Return pointer to data.
Definition data_view.hpp:94
config.hpp
Contains macro checks for different configurations.
protozero
All parts of the protozero header-only library are in this namespace.
Definition basic_pbf_builder.hpp:24
protozero::operator<
bool operator<(const data_view lhs, const data_view rhs) noexcept
Definition data_view.hpp:198
protozero::swap
void swap(basic_pbf_writer< TBuffer > &lhs, basic_pbf_writer< TBuffer > &rhs) noexcept
Definition basic_pbf_writer.hpp:936
protozero::operator!=
constexpr bool operator!=(const data_view lhs, const data_view rhs) noexcept
Definition data_view.hpp:188
protozero::operator==
constexpr bool operator==(const data_view lhs, const data_view rhs) noexcept
Definition data_view.hpp:176
protozero::operator>=
bool operator>=(const data_view lhs, const data_view rhs) noexcept
Definition data_view.hpp:228
protozero::operator<=
bool operator<=(const data_view lhs, const data_view rhs) noexcept
Definition data_view.hpp:208
protozero::operator>
bool operator>(const data_view lhs, const data_view rhs) noexcept
Definition data_view.hpp:218