async_mqtt 9.0.1
Loading...
Searching...
No Matches
buffer.hpp
1// Copyright Takatoshi Kondo 2022
2//
3// Distributed under the Boost Software License, Version 1.0.
4// (See accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6
7#if !defined(ASYNC_MQTT_UTIL_BUFFER_HPP)
8#define ASYNC_MQTT_UTIL_BUFFER_HPP
9
10#include <string_view>
11#include <memory>
12
13
14#include <boost/asio/buffer.hpp>
15#include <boost/container_hash/hash.hpp>
16
17#include <async_mqtt/util/move.hpp>
18#include <async_mqtt/util/detail/is_iterator.hpp>
19
25namespace async_mqtt {
26
27namespace as = boost::asio;
28
46class buffer {
47public:
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;
61
62 static constexpr size_type npos = std::string_view::npos;
63
67 constexpr buffer() noexcept = default;
68
72 buffer(buffer const& other) = default;
73
77 buffer(buffer&& other) = default;
78
82 buffer& operator=(buffer const& other) = default;
83
87 buffer& operator=(buffer&& other) = default;
88
95 constexpr buffer(char const* s, std::size_t count)
96 :view_{s, count}
97 {}
98
105 constexpr buffer(char const* s)
106 :view_{s}
107 {}
108
116 template <
117 typename It,
118 typename End,
119 typename std::enable_if_t<
120 detail::is_input_iterator<It>::value &&
121 detail::is_input_iterator<End>::value
122 >* = nullptr
123 >
124 constexpr buffer(It first, End last)
125 :view_{&*first, &*first + std::distance(first, last)}
126 {}
127
134 explicit constexpr buffer(std::string_view sv)
135 : view_{force_move(sv)}
136 {}
137
142 explicit buffer(std::string s) {
143 auto str = std::make_shared<std::string>(force_move(s));
144 view_ = *str;
145 life_ = force_move(str);
146 }
147
154 explicit buffer(std::string_view sv, std::shared_ptr<void> life)
155 : view_{force_move(sv)},
156 life_{force_move(life)}
157 {}
158
166 explicit buffer(char const* s, std::size_t count,std::shared_ptr<void> life)
167 : view_{s, count},
168 life_{force_move(life)}
169 {}
170
178 template <
179 typename It,
180 typename End,
181 typename std::enable_if_t<
182 detail::is_input_iterator<It>::value &&
183 detail::is_input_iterator<End>::value
184 >* = nullptr
185 >
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)}
189
190 {}
191
196 constexpr const_iterator begin() const noexcept {
197 return view_.begin();
198 }
199
204 constexpr const_iterator cbegin() const noexcept {
205 return view_.cbegin();
206 }
207
212 constexpr const_iterator end() const noexcept {
213 return view_.end();
214 }
215
220 constexpr const_iterator cend() const noexcept {
221 return view_.cend();
222 }
223
228 constexpr const_reverse_iterator rbegin() const noexcept {
229 return view_.rbegin();
230 }
231
236 constexpr const_reverse_iterator crbegin() const noexcept {
237 return view_.crbegin();
238 }
239
244 constexpr const_reverse_iterator rend() const noexcept {
245 return view_.rend();
246 }
247
252 constexpr const_reverse_iterator crend() const noexcept {
253 return view_.crend();
254 }
255
261 constexpr const_reference operator[](size_type pos) const {
262 return view_[pos];
263 }
264
270 constexpr const_reference at(size_type pos) const {
271 return view_.at(pos);
272 }
273
278 constexpr const_reference front() const {
279 return view_.front();
280 }
281
286 constexpr const_reference back() const {
287 return view_.back();
288 }
289
294 constexpr const_pointer data() const noexcept {
295 return view_.data();
296 }
297
302 constexpr size_type size() const noexcept {
303 return view_.size();
304 }
305
310 constexpr size_type length() const noexcept {
311 return view_.length();
312 }
313
318 constexpr size_type max_size() const noexcept {
319 return view_.max_size();
320 }
321
326 constexpr bool empty() const noexcept {
327 return view_.empty();
328 }
329
334 constexpr void remove_prefix(size_type n) {
335 view_.remove_prefix(n);
336 }
337
342 constexpr void remove_suffix(size_type n) {
343 view_.remove_suffix(n);
344 }
345
350 void swap(buffer& buf) noexcept {
351 view_.swap(buf.view_);
352 life_.swap(buf.life_);
353 }
354
361 size_type copy(char* dest, size_type count, size_type pos = 0 ) const {
362 return view_.copy(dest, count, pos);
363 }
364
373 buffer substr(size_type pos = 0, size_type count = npos) const& {
374 // range is checked in std::string_view::substr.
375 return buffer(view_.substr(pos, count), life_);
376 }
377
386 buffer substr(size_type pos = 0, size_type count = npos) && {
387 // range is checked in std::string_view::substr.
388 return buffer(view_.substr(pos, count), force_move(life_));
389 }
390
399 constexpr int compare(buffer const& buf) const noexcept {
400 return view_.compare(buf.view_);
401 }
402
411 constexpr int compare(std::string_view const& v) const noexcept {
412 return view_.compare(v);
413 }
414
425 constexpr int compare(size_type pos1, size_type count1, buffer const& buf) const noexcept {
426 return view_.compare(pos1, count1, buf.view_);
427 }
428
439 constexpr int compare(size_type pos1, size_type count1, std::string_view const& v) const noexcept {
440 return view_.compare(pos1, count1, v);
441 }
442
455 constexpr int compare(
456 size_type pos1,
457 size_type count1,
458 buffer const& buf,
459 size_type pos2,
460 size_type count2
461 ) const noexcept {
462 return view_.compare(pos1, count1, buf.view_, pos2, count2);
463 }
464
477 constexpr int compare(
478 size_type pos1,
479 size_type count1,
480 std::string_view const& v,
481 size_type pos2,
482 size_type count2
483 ) const noexcept {
484 return view_.compare(pos1, count1, v, pos2, count2);
485 }
486
495 constexpr int compare(char const* s) const noexcept {
496 return view_.compare(s);
497 }
498
509 constexpr int compare(size_type pos1, size_type count1, char const* s) const noexcept {
510 return view_.compare(pos1, count1, s);
511 }
512
525 constexpr int compare(
526 size_type pos1,
527 size_type count1,
528 char const* s,
529 size_type pos2,
530 size_type count2
531 ) const noexcept {
532 return view_.compare(pos1, count1, s, pos2, count2);
533 }
534
541 constexpr size_type find(buffer const& buf, size_type pos = 0) const noexcept {
542 return view_.find(buf.view_, pos);
543 }
544
551 constexpr size_type find(std::string_view v, size_type pos = 0) const noexcept {
552 return view_.find(v, pos);
553 }
554
561 constexpr size_type find(char ch, size_type pos = 0) const noexcept {
562 return view_.find(ch, pos);
563 }
564
572 constexpr size_type find(char const* s, size_type pos, size_type count) const {
573 return view_.find(s, pos, count);
574 }
575
582 constexpr size_type find(char const* s, size_type pos = 0) const {
583 return view_.find(s, pos);
584 }
585
592 constexpr size_type rfind(buffer const& buf, size_type pos = npos) const noexcept {
593 return view_.rfind(buf.view_, pos);
594 }
595
602 constexpr size_type rfind( std::string_view v, size_type pos = npos) const noexcept {
603 return view_.rfind(v, pos);
604 }
605
612 constexpr size_type rfind(char ch, size_type pos = npos) const noexcept {
613 return view_.rfind(ch, pos);
614 }
615
623 constexpr size_type rfind(char const* s, size_type pos, size_type count) const {
624 return view_.rfind(s, pos, count);
625 }
626
633 constexpr size_type rfind(char const* s, size_type pos = npos) const {
634 return view_.rfind(s, pos);
635 }
636
643 constexpr size_type find_first_of(buffer const& buf, size_type pos = 0) const noexcept {
644 return view_.find_first_of(buf.view_, pos);
645 }
646
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);
655 }
656
663 constexpr size_type find_first_of(char ch, size_type pos = 0) const noexcept {
664 return view_.find_first_of(ch, pos);
665 }
666
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);
676 }
677
684 constexpr size_type find_first_of(char const* s, size_type pos = 0) const {
685 return view_.find_first_of(s, pos);
686 }
687
694 constexpr size_type find_last_of(buffer const& buf, size_type pos = npos) const noexcept {
695 return view_.find_last_of(buf.view_, pos);
696 }
697
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);
706 }
707
714 constexpr size_type find_last_of(char ch, size_type pos = npos) const noexcept {
715 return view_.find_last_of(ch, pos);
716 }
717
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);
727 }
734 constexpr size_type find_last_of(char const* s, size_type pos = npos) const {
735 return view_.find_last_of(s, pos);
736 }
737
745 constexpr size_type find_first_not_of(buffer const& buf, size_type pos = 0) const noexcept {
746 return view_.find_first_not_of(buf.view_, pos);
747 }
748
756 constexpr size_type find_first_not_of( std::string_view v, size_type pos = 0) const noexcept {
757 return view_.find_first_not_of(v, pos);
758 }
759
767 constexpr size_type find_first_not_of(char ch, size_type pos = 0) const noexcept {
768 return view_.find_first_not_of(ch, pos);
769 }
770
779 constexpr size_type find_first_not_of(char const* s, size_type pos, size_type count) const {
780 return view_.find_first_not_of(s, pos, count);
781 }
782
790 constexpr size_type find_first_not_of(char const* s, size_type pos = 0) const {
791 return view_.find_first_not_of(s, pos);
792 }
793
801 constexpr size_type find_last_not_of(buffer const& buf, size_type pos = npos) const noexcept {
802 return view_.find_last_not_of(buf.view_, pos);
803 }
804
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);
814 }
815
823 constexpr size_type find_last_not_of(char ch, size_type pos = npos) const noexcept {
824 return view_.find_last_not_of(ch, pos);
825 }
826
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);
837 }
838
846 constexpr size_type find_last_not_of(char const* s, size_type pos = npos) const {
847 return view_.find_last_not_of(s, pos);
848 }
849
854 operator as::const_buffer() const {
855 return as::buffer(view_.data(), view_.size());
856 }
857
862 operator std::string_view() const {
863 return view_;
864 }
865
873 friend
874 constexpr bool operator==(buffer const& lhs, buffer const& rhs) noexcept {
875 return lhs.view_ == rhs.view_;
876 }
877
885 friend
886 constexpr bool operator!=(buffer const& lhs, buffer const& rhs) noexcept {
887 return lhs.view_ != rhs.view_;
888 }
889
897 friend
898 constexpr bool operator<(buffer const& lhs, buffer const& rhs) noexcept {
899 return lhs.view_ < rhs.view_;
900 }
901
909 friend
910 constexpr bool operator<=(buffer const& lhs, buffer const& rhs) noexcept {
911 return lhs.view_ <= rhs.view_;
912 }
913
921 friend
922 constexpr bool operator>(buffer const& lhs, buffer const& rhs) noexcept {
923 return lhs.view_ > rhs.view_;
924 }
925
933 friend
934 constexpr bool operator>=(buffer const& lhs, buffer const& rhs) noexcept {
935 return lhs.view_ >= rhs.view_;
936 }
937
944 friend
945 std::ostream& operator<<(std::ostream& o, buffer const& v) {
946 o << v.view_;
947 return o;
948 }
949
954 bool has_life() const noexcept {
955 return bool(life_);
956 }
957
958private:
959 std::string_view view_;
960 life_type life_;
961};
962
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));
976 return result;
977}
978
979} // namespace async_mqtt
980
981namespace boost {
982namespace asio {
983
999inline const_buffer buffer(async_mqtt::buffer const& data) {
1000 return buffer(data.data(), data.size());
1001}
1002
1003} // namespace asio
1004} // namespace boost
1005
1006namespace std {
1007
1017template <>
1018class hash<async_mqtt::buffer> {
1019public:
1025 std::uint64_t operator()(async_mqtt::buffer const& v) const noexcept {
1026 return std::hash<std::string_view>()(static_cast<std::string_view const&>(v));
1027 }
1028};
1029
1030} // namespace std
1031
1032#endif // ASYNC_MQTT_UTIL_BUFFER_HPP
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