17#ifndef TOOLBOX_IO_BUFFER_HPP
18#define TOOLBOX_IO_BUFFER_HPP
22#include <boost/asio/buffer.hpp>
23#include <boost/container/vector.hpp>
31using boost::asio::buffer_cast;
32using boost::asio::buffer_size;
36 explicit Buffer(std::size_t capacity) { buf_.reserve(capacity); }
55 std::string_view
str() const noexcept {
return {rptr(),
size()}; }
58 std::string_view
front(std::size_t size)
const noexcept
60 const auto limit = this->
size();
61 return {rptr(), std::min(
size, limit)};
64 std::string_view
back(std::size_t size)
const noexcept
66 const auto limit = this->
size();
68 return {rptr() + limit -
size,
size};
72 bool empty() const noexcept {
return size() == 0U; };
75 std::size_t
size() const noexcept {
return wpos_ - rpos_; }
78 void clear() noexcept { rpos_ = wpos_ = 0; }
81 void commit(std::size_t count)
noexcept { wpos_ += count; }
84 void consume(std::size_t count)
noexcept;
89 if (
const auto avail = available();
size > avail) {
91 const auto diff =
size - avail;
92 buf_.resize(buf_.size() + diff);
94 return {wptr(),
size};
98 void reserve(std::size_t capacity) { buf_.reserve(capacity); }
101 const char* rptr() const noexcept {
return buf_.data() + rpos_; }
102 char* wptr() noexcept {
return buf_.data() + wpos_; }
103 std::size_t available() const noexcept {
return buf_.size() - wpos_; }
105 std::size_t rpos_{}, wpos_{};
106 boost::container::vector<char> buf_;