data_view.hpp
Go to the documentation of this file.
#ifndef PROTOZERO_DATA_VIEW_HPP
#define PROTOZERO_DATA_VIEW_HPP
/*****************************************************************************
protozero - Minimalistic protocol buffer decoder and encoder in C++.
This file is from https://github.com/mapbox/protozero where you can find more
documentation.
*****************************************************************************/
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <string>
#include <utility>
#include <protozero/config.hpp>
namespace protozero {
#ifdef PROTOZERO_USE_VIEW
using data_view = PROTOZERO_USE_VIEW;
#else
class data_view {
const char* m_data = nullptr;
std::size_t m_size = 0;
public:
constexpr data_view() noexcept = default;
constexpr data_view(const char* ptr, std::size_t length) noexcept
: m_data(ptr),
m_size(length) {
}
data_view(const std::string& str) noexcept // NOLINT clang-tidy: google-explicit-constructor
: m_data(str.data()),
m_size(str.size()) {
}
data_view(const char* ptr) noexcept // NOLINT clang-tidy: google-explicit-constructor
: m_data(ptr),
m_size(std::strlen(ptr)) {
}
void swap(data_view& other) noexcept {
using std::swap;
swap(m_data, other.m_data);
swap(m_size, other.m_size);
}
constexpr const char* data() const noexcept {
return m_data;
}
constexpr std::size_t size() const noexcept {
return m_size;
}
constexpr bool empty() const noexcept {
return m_size == 0;
}
#ifndef PROTOZERO_STRICT_API
std::string to_string() const {
protozero_assert(m_data);
return {m_data, m_size};
}
#endif
explicit operator std::string() const {
protozero_assert(m_data);
return {m_data, m_size};
}
int compare(data_view other) const {
protozero_assert(m_data && other.m_data);
const int cmp = std::memcmp(data(), other.data(),
std::min(size(), other.size()));
if (cmp == 0) {
if (size() == other.size()) {
return 0;
}
return size() < other.size() ? -1 : 1;
}
return cmp;
}
}; // class data_view
inline void swap(data_view& lhs, data_view& rhs) noexcept {
lhs.swap(rhs);
}
inline constexpr bool operator==(const data_view lhs, const data_view rhs) noexcept {
return lhs.size() == rhs.size() &&
std::equal(lhs.data(), lhs.data() + lhs.size(), rhs.data());
}
inline constexpr bool operator!=(const data_view lhs, const data_view rhs) noexcept {
return !(lhs == rhs);
}
inline bool operator<(const data_view lhs, const data_view rhs) noexcept {
return lhs.compare(rhs) < 0;
}
inline bool operator<=(const data_view lhs, const data_view rhs) noexcept {
return lhs.compare(rhs) <= 0;
}
inline bool operator>(const data_view lhs, const data_view rhs) noexcept {
return lhs.compare(rhs) > 0;
}
inline bool operator>=(const data_view lhs, const data_view rhs) noexcept {
return lhs.compare(rhs) >= 0;
}
#endif
} // end namespace protozero
#endif // PROTOZERO_DATA_VIEW_HPP
protozero::data_view::compare
int compare(data_view other) const
Definition: data_view.hpp:144
protozero::operator==
constexpr bool operator==(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:176
protozero::data_view::empty
constexpr bool empty() const noexcept
Returns true if size is 0.
Definition: data_view.hpp:104
protozero::data_view::data_view
data_view(const std::string &str) noexcept
Definition: data_view.hpp:67
protozero::data_view::to_string
std::string to_string() const
Definition: data_view.hpp:118
config.hpp
Contains macro checks for different configurations.
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:228
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_view
data_view(const char *ptr) noexcept
Definition: data_view.hpp:77
protozero::data_view::data_view
constexpr data_view() noexcept=default
protozero::operator<
bool operator<(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:198
protozero::operator>
bool operator>(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:218
protozero::data_view::swap
void swap(data_view &other) noexcept
Definition: data_view.hpp:87
protozero::data_view
Definition: data_view.hpp:39
protozero::operator!=
constexpr bool operator!=(const data_view lhs, const data_view rhs) noexcept
Definition: data_view.hpp:188
protozero::data_view::data
constexpr const char * data() const noexcept
Return pointer to data.
Definition: data_view.hpp:94
protozero::data_view::data_view
constexpr data_view(const char *ptr, std::size_t length) noexcept
Definition: data_view.hpp:57
protozero
All parts of the protozero header-only library are in this namespace.
Definition: byteswap.hpp:24
protozero::swap
void swap(data_view &lhs, data_view &rhs) noexcept
Definition: data_view.hpp:165