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_size;
32
34 public:
35 explicit Buffer(std::size_t capacity) { buf_.reserve(capacity); }
36 Buffer() = default;
37 ~Buffer() = default;
38
39 // Copy.
40 Buffer(const Buffer&) = default;
41 Buffer& operator=(const Buffer&) = default;
42
43 // Move.
44 Buffer(Buffer&&) noexcept = default;
45 Buffer& operator=(Buffer&&) noexcept = default;
46
48 ConstBuffer data() const noexcept { return {rptr(), size()}; }
49
51 ConstBuffer data(std::size_t limit) const noexcept { return {rptr(), std::min(limit, size())}; }
52
54 std::string_view str() const noexcept { return {rptr(), size()}; }
55
57 std::string_view front(std::size_t size) const noexcept
58 {
59 const auto limit = this->size();
60 return {rptr(), std::min(size, limit)};
61 }
63 std::string_view back(std::size_t size) const noexcept
64 {
65 const auto limit = this->size();
66 size = std::min(size, limit);
67 return {rptr() + limit - size, size};
68 }
69
71 bool empty() const noexcept { return size() == 0U; };
72
74 std::size_t size() const noexcept { return wpos_ - rpos_; }
75
77 void clear() noexcept { rpos_ = wpos_ = 0; }
78
80 void commit(std::size_t count) noexcept { wpos_ += count; }
81
83 void consume(std::size_t count) noexcept;
84
86 MutableBuffer prepare(std::size_t size)
87 {
88 if (const auto avail = available(); size > avail) {
89 // More buffer space required.
90 const auto diff = size - avail;
91 buf_.resize(buf_.size() + diff);
92 }
93 return {wptr(), size};
94 }
95
97 void reserve(std::size_t capacity) { buf_.reserve(capacity); }
98
99 private:
100 const char* rptr() const noexcept { return buf_.data() + rpos_; }
101 char* wptr() noexcept { return buf_.data() + wpos_; }
102 std::size_t available() const noexcept { return buf_.size() - wpos_; }
103
104 std::size_t rpos_{}, wpos_{};
105 boost::container::vector<char> buf_;
106};
107
108TOOLBOX_API ConstBuffer advance(ConstBuffer buf, std::size_t n) noexcept;
109TOOLBOX_API MutableBuffer advance(MutableBuffer buf, std::size_t n) noexcept;
110
111} // namespace io
112} // namespace toolbox
113
114#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:51
Buffer(std::size_t capacity)
Definition Buffer.hpp:35
std::size_t size() const noexcept
Returns number of bytes available for read.
Definition Buffer.hpp:74
Buffer & operator=(const Buffer &)=default
MutableBuffer prepare(std::size_t size)
Returns write buffer of at least size bytes.
Definition Buffer.hpp:86
Buffer(Buffer &&) noexcept=default
void reserve(std::size_t capacity)
Reserve storage.
Definition Buffer.hpp:97
std::string_view str() const noexcept
Returns available data as a string view.
Definition Buffer.hpp:54
bool empty() const noexcept
Returns true if read buffer is empty.
Definition Buffer.hpp:71
void clear() noexcept
Clear buffer.
Definition Buffer.hpp:77
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:63
std::string_view front(std::size_t size) const noexcept
Returns slice of available data at the front of the buffer.
Definition Buffer.hpp:57
void commit(std::size_t count) noexcept
Move characters from the write sequence to the read sequence.
Definition Buffer.hpp:80
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