7#if !defined(ASYNC_MQTT_UTIL_BUFFER_HPP)
8#define ASYNC_MQTT_UTIL_BUFFER_HPP
14#include <boost/asio/buffer.hpp>
15#include <boost/container_hash/hash.hpp>
17#include <async_mqtt/util/move.hpp>
18#include <async_mqtt/util/detail/is_iterator.hpp>
27namespace as = boost::asio;
48 using life_type = std::shared_ptr<void>;
49 using traits_type = std::string_view::traits_type;
50 using value_type = std::string_view::value_type;
51 using pointer = std::string_view::pointer;
52 using const_pointer = std::string_view::const_pointer;
53 using reference = std::string_view::reference;
54 using const_reference = std::string_view::const_reference;
55 using iterator = std::string_view::iterator;
56 using const_iterator = std::string_view::const_iterator;
57 using const_reverse_iterator = std::string_view::const_reverse_iterator;
58 using reverse_iterator = std::string_view::reverse_iterator;
59 using size_type = std::string_view::size_type;
60 using difference_type = std::string_view::difference_type;
62 static constexpr size_type npos = std::string_view::npos;
67 constexpr buffer() noexcept = default;
95 constexpr
buffer(
char const* s, std::
size_t count)
119 typename std::enable_if_t<
120 detail::is_input_iterator<It>::value &&
121 detail::is_input_iterator<End>::value
125 :view_{&*first, &*first + std::distance(first, last)}
134 explicit constexpr buffer(std::string_view sv)
135 : view_{force_move(sv)}
143 auto str = std::make_shared<std::string>(force_move(s));
145 life_ = force_move(str);
154 explicit buffer(std::string_view sv, std::shared_ptr<void> life)
155 : view_{force_move(sv)},
156 life_{force_move(life)}
166 explicit buffer(
char const* s, std::size_t count,std::shared_ptr<void> life)
168 life_{force_move(life)}
181 typename std::enable_if_t<
182 detail::is_input_iterator<It>::value &&
183 detail::is_input_iterator<End>::value
186 explicit buffer(It first, End last, std::shared_ptr<void> life)
187 : view_{&*first, static_cast<std::size_t>(std::distance(first, last))},
188 life_{force_move(life)}
196 constexpr const_iterator
begin() const noexcept {
197 return view_.begin();
204 constexpr const_iterator
cbegin() const noexcept {
205 return view_.cbegin();
212 constexpr const_iterator
end() const noexcept {
220 constexpr const_iterator
cend() const noexcept {
228 constexpr const_reverse_iterator
rbegin() const noexcept {
229 return view_.rbegin();
236 constexpr const_reverse_iterator
crbegin() const noexcept {
237 return view_.crbegin();
244 constexpr const_reverse_iterator
rend() const noexcept {
252 constexpr const_reverse_iterator
crend() const noexcept {
253 return view_.crend();
270 constexpr const_reference
at(size_type pos)
const {
271 return view_.at(pos);
278 constexpr const_reference
front()
const {
279 return view_.front();
286 constexpr const_reference
back()
const {
294 constexpr const_pointer
data() const noexcept {
302 constexpr size_type
size() const noexcept {
310 constexpr size_type
length() const noexcept {
311 return view_.length();
319 return view_.max_size();
326 constexpr bool empty() const noexcept {
327 return view_.empty();
335 view_.remove_prefix(n);
343 view_.remove_suffix(n);
351 view_.swap(buf.view_);
352 life_.swap(buf.life_);
361 size_type
copy(
char* dest, size_type count, size_type pos = 0 )
const {
362 return view_.copy(dest, count, pos);
375 return buffer(view_.substr(pos, count), life_);
388 return buffer(view_.substr(pos, count), force_move(life_));
400 return view_.compare(buf.view_);
411 constexpr int compare(std::string_view
const& v)
const noexcept {
412 return view_.compare(v);
425 constexpr int compare(size_type pos1, size_type count1,
buffer const& buf)
const noexcept {
426 return view_.compare(pos1, count1, buf.view_);
439 constexpr int compare(size_type pos1, size_type count1, std::string_view
const& v)
const noexcept {
440 return view_.compare(pos1, count1, v);
462 return view_.compare(pos1, count1, buf.view_, pos2, count2);
480 std::string_view
const& v,
484 return view_.compare(pos1, count1, v, pos2, count2);
495 constexpr int compare(
char const* s)
const noexcept {
496 return view_.compare(s);
509 constexpr int compare(size_type pos1, size_type count1,
char const* s)
const noexcept {
510 return view_.compare(pos1, count1, s);
532 return view_.compare(pos1, count1, s, pos2, count2);
541 constexpr size_type
find(
buffer const& buf, size_type pos = 0) const noexcept {
542 return view_.find(buf.view_, pos);
551 constexpr size_type
find(std::string_view v, size_type pos = 0) const noexcept {
552 return view_.find(v, pos);
561 constexpr size_type
find(
char ch, size_type pos = 0) const noexcept {
562 return view_.find(ch, pos);
572 constexpr size_type
find(
char const* s, size_type pos, size_type count)
const {
573 return view_.find(s, pos, count);
582 constexpr size_type
find(
char const* s, size_type pos = 0)
const {
583 return view_.find(s, pos);
592 constexpr size_type
rfind(
buffer const& buf, size_type pos = npos)
const noexcept {
593 return view_.rfind(buf.view_, pos);
602 constexpr size_type
rfind( std::string_view v, size_type pos = npos)
const noexcept {
603 return view_.rfind(v, pos);
612 constexpr size_type
rfind(
char ch, size_type pos = npos)
const noexcept {
613 return view_.rfind(ch, pos);
623 constexpr size_type
rfind(
char const* s, size_type pos, size_type count)
const {
624 return view_.rfind(s, pos, count);
633 constexpr size_type
rfind(
char const* s, size_type pos = npos)
const {
634 return view_.rfind(s, pos);
644 return view_.find_first_of(buf.view_, pos);
653 constexpr size_type
find_first_of( std::string_view v, size_type pos = 0) const noexcept {
654 return view_.find_first_of(v, pos);
663 constexpr size_type
find_first_of(
char ch, size_type pos = 0) const noexcept {
664 return view_.find_first_of(ch, pos);
674 constexpr size_type
find_first_of(
char const* s, size_type pos, size_type count)
const {
675 return view_.find_first_of(s, pos, count);
685 return view_.find_first_of(s, pos);
695 return view_.find_last_of(buf.view_, pos);
704 constexpr size_type
find_last_of( std::string_view v, size_type pos = npos)
const noexcept {
705 return view_.find_last_of(v, pos);
714 constexpr size_type
find_last_of(
char ch, size_type pos = npos)
const noexcept {
715 return view_.find_last_of(ch, pos);
725 constexpr size_type
find_last_of(
char const* s, size_type pos, size_type count)
const {
726 return view_.find_last_of(s, pos, count);
734 constexpr size_type
find_last_of(
char const* s, size_type pos = npos)
const {
735 return view_.find_last_of(s, pos);
746 return view_.find_first_not_of(buf.view_, pos);
757 return view_.find_first_not_of(v, pos);
768 return view_.find_first_not_of(ch, pos);
780 return view_.find_first_not_of(s, pos, count);
791 return view_.find_first_not_of(s, pos);
802 return view_.find_last_not_of(buf.view_, pos);
812 constexpr size_type
find_last_not_of( std::string_view v, size_type pos = npos)
const noexcept {
813 return view_.find_last_not_of(v, pos);
824 return view_.find_last_not_of(ch, pos);
835 constexpr size_type
find_last_not_of(
char const* s, size_type pos, size_type count)
const {
836 return view_.find_last_not_of(s, pos, count);
847 return view_.find_last_not_of(s, pos);
854 operator as::const_buffer()
const {
855 return as::buffer(view_.data(), view_.size());
862 operator std::string_view()
const {
875 return lhs.view_ == rhs.view_;
887 return lhs.view_ != rhs.view_;
899 return lhs.view_ < rhs.view_;
911 return lhs.view_ <= rhs.view_;
923 return lhs.view_ > rhs.view_;
935 return lhs.view_ >= rhs.view_;
959 std::string_view view_;
973inline std::size_t hash_value(buffer
const& v)
noexcept {
974 std::size_t result = 0;
975 boost::hash_combine(result,
static_cast<std::string_view const&
>(v));
1018class hash<async_mqtt::buffer> {
1026 return std::hash<std::string_view>()(
static_cast<std::string_view const&
>(v));
buffer that has string_view interface and shared ownership This class is only for advanced usecase su...
Definition buffer.hpp:46
constexpr size_type find_first_not_of(char const *s, size_type pos=0) const
find the first character not equal to any of the characters in the given view
Definition buffer.hpp:790
friend constexpr bool operator>=(buffer const &lhs, buffer const &rhs) noexcept
greater than or equal to operator comparison target is the view of the buffer. life holder is not com...
Definition buffer.hpp:934
constexpr size_type find_first_not_of(char ch, size_type pos=0) const noexcept
find the first character not equal to any of the characters in the given view
Definition buffer.hpp:767
constexpr size_type rfind(buffer const &buf, size_type pos=npos) const noexcept
find the last substring equal to the given view
Definition buffer.hpp:592
friend std::ostream & operator<<(std::ostream &o, buffer const &v)
output to the stream
Definition buffer.hpp:945
constexpr const_reverse_iterator rbegin() const noexcept
get a reverse terator to the beginning
Definition buffer.hpp:228
constexpr size_type find(char const *s, size_type pos=0) const
find the first substring equal to the given view
Definition buffer.hpp:582
constexpr const_reference operator[](size_type pos) const
get a reference of the specific character
Definition buffer.hpp:261
constexpr size_type find_first_not_of(std::string_view v, size_type pos=0) const noexcept
find the first character not equal to any of the characters in the given view
Definition buffer.hpp:756
constexpr size_type find_last_of(char ch, size_type pos=npos) const noexcept
find the last character equal to any of the characters in the given view
Definition buffer.hpp:714
buffer(std::string s)
string constructor
Definition buffer.hpp:142
constexpr size_type find(buffer const &buf, size_type pos=0) const noexcept
find the first substring equal to the given view
Definition buffer.hpp:541
constexpr buffer() noexcept=default
default constructor
constexpr void remove_prefix(size_type n)
remove the first n characters from the buffer
Definition buffer.hpp:334
constexpr int compare(size_type pos1, size_type count1, char const *s) const noexcept
compare buffer Compare only the view.
Definition buffer.hpp:509
friend constexpr bool operator==(buffer const &lhs, buffer const &rhs) noexcept
equal operator comparison target is the view of the buffer. life holder is not compared.
Definition buffer.hpp:874
constexpr void remove_suffix(size_type n)
remove the last n characters from the buffer
Definition buffer.hpp:342
constexpr size_type find_first_of(std::string_view v, size_type pos=0) const noexcept
find the first character equal to any of the characters in the given view
Definition buffer.hpp:653
constexpr size_type find_last_not_of(std::string_view v, size_type pos=npos) const noexcept
find the last character not equal to any of the characters in the given view
Definition buffer.hpp:812
constexpr size_type find_last_not_of(char ch, size_type pos=npos) const noexcept
find the last character not equal to any of the characters in the given view
Definition buffer.hpp:823
constexpr size_type find(char const *s, size_type pos, size_type count) const
find the first substring equal to the given view
Definition buffer.hpp:572
constexpr int compare(size_type pos1, size_type count1, buffer const &buf, size_type pos2, size_type count2) const noexcept
compare buffer Compare only the view.
Definition buffer.hpp:455
buffer(char const *s, std::size_t count, std::shared_ptr< void > life)
pointer, size, and lifetime constructor
Definition buffer.hpp:166
buffer(std::string_view sv, std::shared_ptr< void > life)
std::string_view and lifetime constructor
Definition buffer.hpp:154
constexpr bool empty() const noexcept
checking the buffer is empty
Definition buffer.hpp:326
constexpr int compare(size_type pos1, size_type count1, std::string_view const &v, size_type pos2, size_type count2) const noexcept
compare buffer Compare only the view.
Definition buffer.hpp:477
void swap(buffer &buf) noexcept
swap the buffer
Definition buffer.hpp:350
constexpr buffer(It first, End last)
move assign operator The buffer doesn't manage the lifetime. The memory between first and last must b...
Definition buffer.hpp:124
constexpr const_iterator cend() const noexcept
get an iterator to the end
Definition buffer.hpp:220
constexpr const_iterator end() const noexcept
get an iterator to the end
Definition buffer.hpp:212
constexpr size_type size() const noexcept
get the number of characters
Definition buffer.hpp:302
constexpr size_type rfind(char const *s, size_type pos=npos) const
find the last substring equal to the given view
Definition buffer.hpp:633
buffer substr(size_type pos=0, size_type count=npos) const &
get substring The returned buffer ragnge is the same as std::string_view::substr()....
Definition buffer.hpp:373
constexpr size_type find_last_not_of(char const *s, size_type pos=npos) const
find the last character not equal to any of the characters in the given view
Definition buffer.hpp:846
constexpr size_type find_first_of(char const *s, size_type pos, size_type count) const
find the first character equal to any of the characters in the given view
Definition buffer.hpp:674
constexpr const_reference back() const
get a reference of the last character
Definition buffer.hpp:286
constexpr size_type find_last_of(char const *s, size_type pos=npos) const
find the last character equal to any of the characters in the given view
Definition buffer.hpp:734
constexpr size_type length() const noexcept
get the number of characters
Definition buffer.hpp:310
constexpr size_type find_last_of(std::string_view v, size_type pos=npos) const noexcept
find the last character equal to any of the characters in the given view
Definition buffer.hpp:704
constexpr size_type find_last_not_of(char const *s, size_type pos, size_type count) const
find the last character not equal to any of the characters in the given view
Definition buffer.hpp:835
constexpr int compare(size_type pos1, size_type count1, char const *s, size_type pos2, size_type count2) const noexcept
compare buffer Compare only the view.
Definition buffer.hpp:525
constexpr size_type rfind(char const *s, size_type pos, size_type count) const
find the last substring equal to the given view
Definition buffer.hpp:623
constexpr const_iterator begin() const noexcept
get an iterator to the beginning
Definition buffer.hpp:196
constexpr const_iterator cbegin() const noexcept
get an iterator to the beginning
Definition buffer.hpp:204
constexpr buffer(char const *s)
constructor The buffer doesn't manage the lifetime
Definition buffer.hpp:105
constexpr const_reverse_iterator crbegin() const noexcept
get a reverse terator to the beginning
Definition buffer.hpp:236
size_type copy(char *dest, size_type count, size_type pos=0) const
copy the buffer characters
Definition buffer.hpp:361
constexpr size_type find_last_of(buffer const &buf, size_type pos=npos) const noexcept
find the last character equal to any of the characters in the given view
Definition buffer.hpp:694
constexpr size_type rfind(std::string_view v, size_type pos=npos) const noexcept
find the last substring equal to the given view
Definition buffer.hpp:602
constexpr size_type find_last_not_of(buffer const &buf, size_type pos=npos) const noexcept
find the last character not equal to any of the characters in the given view
Definition buffer.hpp:801
friend constexpr bool operator!=(buffer const &lhs, buffer const &rhs) noexcept
not equal operator comparison target is the view of the buffer. life holder is not compared.
Definition buffer.hpp:886
constexpr size_type find_last_of(char const *s, size_type pos, size_type count) const
find the last character equal to any of the characters in the given view
Definition buffer.hpp:725
constexpr int compare(buffer const &buf) const noexcept
compare buffer Compare only the view.
Definition buffer.hpp:399
constexpr int compare(size_type pos1, size_type count1, buffer const &buf) const noexcept
compare buffer Compare only the view.
Definition buffer.hpp:425
constexpr size_type max_size() const noexcept
get the largeset possible number of the buffer
Definition buffer.hpp:318
constexpr const_pointer data() const noexcept
get a pointer of the first character of the buffer
Definition buffer.hpp:294
constexpr size_type find_first_of(char ch, size_type pos=0) const noexcept
find the first character equal to any of the characters in the given view
Definition buffer.hpp:663
constexpr size_type find(std::string_view v, size_type pos=0) const noexcept
find the first substring equal to the given view
Definition buffer.hpp:551
constexpr size_type find(char ch, size_type pos=0) const noexcept
find the first substring equal to the given view
Definition buffer.hpp:561
constexpr size_type find_first_of(char const *s, size_type pos=0) const
find the first character equal to any of the characters in the given view
Definition buffer.hpp:684
constexpr buffer(std::string_view sv)
std::string_view constructor
Definition buffer.hpp:134
constexpr const_reference front() const
get a reference of the first character
Definition buffer.hpp:278
friend constexpr bool operator<=(buffer const &lhs, buffer const &rhs) noexcept
less than or equal to operator comparison target is the view of the buffer. life holder is not compar...
Definition buffer.hpp:910
buffer(It first, End last, std::shared_ptr< void > life)
range and lifetime constructor
Definition buffer.hpp:186
buffer substr(size_type pos=0, size_type count=npos) &&
get substring The returned buffer ragnge is the same as std::string_view::substr()....
Definition buffer.hpp:386
constexpr const_reference at(size_type pos) const
get a reference of the specific character with bounds checking
Definition buffer.hpp:270
constexpr size_type find_first_not_of(buffer const &buf, size_type pos=0) const noexcept
find the first character not equal to any of the characters in the given view
Definition buffer.hpp:745
constexpr size_type rfind(char ch, size_type pos=npos) const noexcept
find the last substring equal to the given view
Definition buffer.hpp:612
constexpr int compare(std::string_view const &v) const noexcept
compare buffer Compare only the view.
Definition buffer.hpp:411
constexpr const_reverse_iterator rend() const noexcept
get a reverse terator to the end
Definition buffer.hpp:244
friend constexpr bool operator>(buffer const &lhs, buffer const &rhs) noexcept
greater than operator comparison target is the view of the buffer. life holder is not compared.
Definition buffer.hpp:922
bool has_life() const noexcept
life checking
Definition buffer.hpp:954
friend constexpr bool operator<(buffer const &lhs, buffer const &rhs) noexcept
less than operator comparison target is the view of the buffer. life holder is not compared.
Definition buffer.hpp:898
constexpr size_type find_first_of(buffer const &buf, size_type pos=0) const noexcept
find the first character equal to any of the characters in the given view
Definition buffer.hpp:643
constexpr int compare(char const *s) const noexcept
compare buffer Compare only the view.
Definition buffer.hpp:495
constexpr size_type find_first_not_of(char const *s, size_type pos, size_type count) const
find the first character not equal to any of the characters in the given view
Definition buffer.hpp:779
constexpr int compare(size_type pos1, size_type count1, std::string_view const &v) const noexcept
compare buffer Compare only the view.
Definition buffer.hpp:439
constexpr const_reverse_iterator crend() const noexcept
get a reverse terator to the end
Definition buffer.hpp:252
std::uint64_t operator()(async_mqtt::buffer const &v) const noexcept
hashing operator
Definition buffer.hpp:1025
const_buffer buffer(async_mqtt::buffer const &data)
create boost::asio::const_buffer from the async_mqtt::buffer boost::asio::const_buffer is a kind of v...
Definition buffer.hpp:999