Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Buffer.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_IO_BUFFER_HPP
18#define TOOLBOX_IO_BUFFER_HPP
19
20#include <toolbox/Config.h>
21
22#include <boost/asio/buffer.hpp>
23#include <boost/container/vector.hpp>
24
25namespace toolbox {
26inline namespace io {
27
28using ConstBuffer = boost::asio::const_buffer;
29using MutableBuffer = boost::asio::mutable_buffer;
30
31using boost::asio::buffer_cast;
32using boost::asio::buffer_size;
33
35 public:
36 explicit Buffer(std::size_t capacity) { buf_.reserve(capacity); }
37 Buffer() = default;
38 ~Buffer() = default;
39
40 // Copy.
41 Buffer(const Buffer&) = default;
42 Buffer& operator=(const Buffer&) = default;
43
44 // Move.
45 Buffer(Buffer&&) noexcept = default;
46 Buffer& operator=(Buffer&&) noexcept = default;
47
49 ConstBuffer data() const noexcept { return {rptr(), size()}; }
50
52 ConstBuffer data(std::size_t limit) const noexcept { return {rptr(), std::min(limit, size())}; }
53
55 std::string_view str() const noexcept { return {rptr(), size()}; }
56
58 std::string_view front(std::size_t size) const noexcept
59 {
60 const auto limit = this->size();
61 return {rptr(), std::min(size, limit)};
62 }
64 std::string_view back(std::size_t size) const noexcept
65 {
66 const auto limit = this->size();
67 size = std::min(size, limit);
68 return {rptr() + limit - size, size};
69 }
70
72 bool empty() const noexcept { return size() == 0U; };
73
75 std::size_t size() const noexcept { return wpos_ - rpos_; }
76
78 void clear() noexcept { rpos_ = wpos_ = 0; }
79
81 void commit(std::size_t count) noexcept { wpos_ += count; }
82
84 void consume(std::size_t count) noexcept;
85
87 MutableBuffer prepare(std::size_t size)
88 {
89 if (const auto avail = available(); size > avail) {
90 // More buffer space required.
91 const auto diff = size - avail;
92 buf_.resize(buf_.size() + diff);
93 }
94 return {wptr(), size};
95 }
96
98 void reserve(std::size_t capacity) { buf_.reserve(capacity); }
99
100 private:
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_; }
104
105 std::size_t rpos_{}, wpos_{};
106 boost::container::vector<char> buf_;
107};
108
109TOOLBOX_API ConstBuffer advance(ConstBuffer buf, std::size_t n) noexcept;
110TOOLBOX_API MutableBuffer advance(MutableBuffer buf, std::size_t n) noexcept;
111
112} // namespace io
113} // namespace toolbox
114
115#endif // TOOLBOX_IO_BUFFER_HPP
#define TOOLBOX_API
Definition Config.h:39
ConstBuffer data(std::size_t limit) const noexcept
Returns slice of available data as a buffer.
Definition Buffer.hpp:52
Buffer(std::size_t capacity)
Definition Buffer.hpp:36
std::size_t size() const noexcept
Returns number of bytes available for read.
Definition Buffer.hpp:75
Buffer & operator=(const Buffer &)=default
MutableBuffer prepare(std::size_t size)
Returns write buffer of at least size bytes.
Definition Buffer.hpp:87
Buffer(Buffer &&) noexcept=default
void reserve(std::size_t capacity)
Reserve storage.
Definition Buffer.hpp:98
std::string_view str() const noexcept
Returns available data as a string view.
Definition Buffer.hpp:55
bool empty() const noexcept
Returns true if read buffer is empty.
Definition Buffer.hpp:72
void clear() noexcept
Clear buffer.
Definition Buffer.hpp:78
Buffer(const Buffer &)=default
std::string_view back(std::size_t size) const noexcept
Returns slice of available data at the back of the buffer.
Definition Buffer.hpp:64
std::string_view front(std::size_t size) const noexcept
Returns slice of available data at the front of the buffer.
Definition Buffer.hpp:58
void commit(std::size_t count) noexcept
Move characters from the write sequence to the read sequence.
Definition Buffer.hpp:81
boost::asio::const_buffer ConstBuffer
Definition Buffer.hpp:28
boost::asio::mutable_buffer MutableBuffer
Definition Buffer.hpp:29
ConstBuffer advance(ConstBuffer buf, std::size_t n) noexcept
Definition Buffer.cpp:48
constexpr std::size_t size(const detail::Struct< detail::Member< TagsT, ValuesT >... > &s)
Definition Struct.hpp:98
constexpr auto bind() noexcept
Definition Slot.hpp:92