protozero
1.6.8
Minimalistic protocol buffer decoder and encoder in C++.
|
Go to the documentation of this file. 1 #ifndef PROTOZERO_DATA_VIEW_HPP
2 #define PROTOZERO_DATA_VIEW_HPP
29 #ifdef PROTOZERO_USE_VIEW
30 using data_view = PROTOZERO_USE_VIEW;
41 const char* m_data =
nullptr;
42 std::size_t m_size = 0;
57 constexpr
data_view(const
char* ptr, std::
size_t length) noexcept
67 data_view(
const std::string& str) noexcept
79 m_size{std::strlen(ptr)} {
89 swap(m_data, other.m_data);
90 swap(m_size, other.m_size);
94 constexpr
const char*
data() const noexcept {
99 constexpr std::size_t
size() const noexcept {
104 constexpr
bool empty() const noexcept {
108 #ifndef PROTOZERO_STRICT_API
119 protozero_assert(m_data);
120 return {m_data, m_size};
129 explicit operator std::string()
const {
130 protozero_assert(m_data);
131 return {m_data, m_size};
145 assert(m_data && other.m_data);
146 const int cmp = std::memcmp(
data(), other.data(),
147 std::min(
size(), other.size()));
149 if (
size() == other.size()) {
152 return size() < other.size() ? -1 : 1;
165 inline void swap(data_view& lhs, data_view& rhs) noexcept {
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());
188 inline constexpr
bool operator!=(
const data_view lhs,
const data_view rhs) noexcept {
189 return !(lhs == rhs);
198 inline bool operator<(
const data_view lhs,
const data_view rhs) noexcept {
199 return lhs.compare(rhs) < 0;
208 inline bool operator<=(
const data_view lhs,
const data_view rhs) noexcept {
209 return lhs.compare(rhs) <= 0;
218 inline bool operator>(
const data_view lhs,
const data_view rhs) noexcept {
219 return lhs.compare(rhs) > 0;
228 inline bool operator>=(
const data_view lhs,
const data_view rhs) noexcept {
229 return lhs.compare(rhs) >= 0;
236 #endif // PROTOZERO_DATA_VIEW_HPP
bool operator>=(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:227
constexpr bool empty() const noexcept
Returns true if size is 0.
Definition: data_view.hpp:103
Contains macro checks for different configurations.
All parts of the protozero header-only library are in this namespace.
Definition: byteswap.hpp:22
std::string to_string() const
Definition: data_view.hpp:117
int compare(data_view other) const noexcept
Definition: data_view.hpp:143
constexpr std::size_t size() const noexcept
Return length of data in bytes.
Definition: data_view.hpp:98
bool operator<(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:197
constexpr bool operator==(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:175
bool operator>(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:217
void swap(data_view &lhs, data_view &rhs) noexcept
Definition: data_view.hpp:164
constexpr data_view() noexcept=default
bool operator<=(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:207
void swap(data_view &other) noexcept
Definition: data_view.hpp:86
constexpr bool operator!=(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:187
constexpr const char * data() const noexcept
Return pointer to data.
Definition: data_view.hpp:93