| 1 |
|
|
1 |
|
|
| 2 |
|
// Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
|
2 |
|
// Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
|
| 3 |
|
|
3 |
|
|
| 4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
| 5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
| 6 |
|
|
6 |
|
|
| 7 |
|
// Official repository: https://github.com/cppalliance/capy
|
7 |
|
// Official repository: https://github.com/cppalliance/capy
|
| 8 |
|
|
8 |
|
|
| 9 |
|
|
9 |
|
|
| 10 |
|
#ifndef BOOST_CAPY_BUFFERS_STRING_DYNAMIC_BUFFER_HPP
|
10 |
|
#ifndef BOOST_CAPY_BUFFERS_STRING_DYNAMIC_BUFFER_HPP
|
| 11 |
|
#define BOOST_CAPY_BUFFERS_STRING_DYNAMIC_BUFFER_HPP
|
11 |
|
#define BOOST_CAPY_BUFFERS_STRING_DYNAMIC_BUFFER_HPP
|
| 12 |
|
|
12 |
|
|
| 13 |
|
#include <boost/capy/detail/config.hpp>
|
13 |
|
#include <boost/capy/detail/config.hpp>
|
| 14 |
|
#include <boost/capy/buffers.hpp>
|
14 |
|
#include <boost/capy/buffers.hpp>
|
| 15 |
|
#include <boost/capy/detail/except.hpp>
|
15 |
|
#include <boost/capy/detail/except.hpp>
|
| 16 |
|
|
16 |
|
|
| 17 |
|
|
17 |
|
|
| 18 |
|
|
18 |
|
|
| 19 |
|
|
19 |
|
|
| 20 |
|
|
20 |
|
|
| 21 |
|
/** A dynamic buffer backed by a `std::basic_string`.
|
21 |
|
/** A dynamic buffer backed by a `std::basic_string`.
|
| 22 |
|
|
22 |
|
|
| 23 |
|
This adapter wraps an externally-owned string and
|
23 |
|
This adapter wraps an externally-owned string and
|
| 24 |
|
exposes it through the @ref DynamicBuffer interface.
|
24 |
|
exposes it through the @ref DynamicBuffer interface.
|
| 25 |
|
Readable bytes occupy the front of the string; writable
|
25 |
|
Readable bytes occupy the front of the string; writable
|
| 26 |
|
bytes are appended by `prepare` and made readable by
|
26 |
|
bytes are appended by `prepare` and made readable by
|
| 27 |
|
|
27 |
|
|
| 28 |
|
|
28 |
|
|
| 29 |
|
@note The wrapped string must outlive this adapter.
|
29 |
|
@note The wrapped string must outlive this adapter.
|
| 30 |
|
Calls to `prepare`, `commit`, and `consume`
|
30 |
|
Calls to `prepare`, `commit`, and `consume`
|
| 31 |
|
invalidate previously returned buffer views.
|
31 |
|
invalidate previously returned buffer views.
|
| 32 |
|
|
32 |
|
|
| 33 |
|
|
33 |
|
|
| 34 |
|
|
34 |
|
|
| 35 |
|
|
35 |
|
|
| 36 |
|
|
36 |
|
|
| 37 |
|
|
37 |
|
|
| 38 |
|
|
38 |
|
|
| 39 |
|
|
39 |
|
|
| 40 |
|
auto buf = dynamic_buffer( s, 4096 );
|
40 |
|
auto buf = dynamic_buffer( s, 4096 );
|
| 41 |
|
auto mb = buf.prepare( 100 );
|
41 |
|
auto mb = buf.prepare( 100 );
|
| 42 |
|
|
42 |
|
|
| 43 |
|
|
43 |
|
|
| 44 |
|
// buf.data() now has 100 readable bytes
|
44 |
|
// buf.data() now has 100 readable bytes
|
| 45 |
|
|
45 |
|
|
| 46 |
|
|
46 |
|
|
| 47 |
|
|
47 |
|
|
| 48 |
|
@tparam CharT The character type.
|
48 |
|
@tparam CharT The character type.
|
| 49 |
|
@tparam Traits The character traits type.
|
49 |
|
@tparam Traits The character traits type.
|
| 50 |
|
@tparam Allocator The allocator type.
|
50 |
|
@tparam Allocator The allocator type.
|
| 51 |
|
|
51 |
|
|
| 52 |
|
@see DynamicBuffer, string_dynamic_buffer, dynamic_buffer
|
52 |
|
@see DynamicBuffer, string_dynamic_buffer, dynamic_buffer
|
| 53 |
|
|
53 |
|
|
| 54 |
|
|
54 |
|
|
| 55 |
|
|
55 |
|
|
| 56 |
|
class Traits = std::char_traits<CharT>,
|
56 |
|
class Traits = std::char_traits<CharT>,
|
| 57 |
|
class Allocator = std::allocator<CharT>>
|
57 |
|
class Allocator = std::allocator<CharT>>
|
| 58 |
|
class basic_string_dynamic_buffer
|
58 |
|
class basic_string_dynamic_buffer
|
| 59 |
|
|
59 |
|
|
| 60 |
|
|
60 |
|
|
| 61 |
|
CharT, Traits, Allocator>* s_;
|
61 |
|
CharT, Traits, Allocator>* s_;
|
| 62 |
|
|
62 |
|
|
| 63 |
|
|
63 |
|
|
| 64 |
|
std::size_t in_size_ = 0;
|
64 |
|
std::size_t in_size_ = 0;
|
| 65 |
|
std::size_t out_size_ = 0;
|
65 |
|
std::size_t out_size_ = 0;
|
| 66 |
|
|
66 |
|
|
| 67 |
|
|
67 |
|
|
| 68 |
|
/// Indicates this is a DynamicBuffer adapter over external storage.
|
68 |
|
/// Indicates this is a DynamicBuffer adapter over external storage.
|
| 69 |
|
using is_dynamic_buffer_adapter = void;
|
69 |
|
using is_dynamic_buffer_adapter = void;
|
| 70 |
|
|
70 |
|
|
| 71 |
|
/// The underlying string type.
|
71 |
|
/// The underlying string type.
|
| 72 |
|
using string_type = std::basic_string<
|
72 |
|
using string_type = std::basic_string<
|
| 73 |
|
CharT, Traits, Allocator>;
|
73 |
|
CharT, Traits, Allocator>;
|
| 74 |
|
|
74 |
|
|
| 75 |
|
/// The ConstBufferSequence type for readable bytes.
|
75 |
|
/// The ConstBufferSequence type for readable bytes.
|
| 76 |
|
using const_buffers_type = const_buffer;
|
76 |
|
using const_buffers_type = const_buffer;
|
| 77 |
|
|
77 |
|
|
| 78 |
|
/// The MutableBufferSequence type for writable bytes.
|
78 |
|
/// The MutableBufferSequence type for writable bytes.
|
| 79 |
|
using mutable_buffers_type = mutable_buffer;
|
79 |
|
using mutable_buffers_type = mutable_buffer;
|
| 80 |
|
|
80 |
|
|
| 81 |
|
|
81 |
|
|
| 82 |
|
~basic_string_dynamic_buffer() = default;
|
82 |
|
~basic_string_dynamic_buffer() = default;
|
| 83 |
|
|
83 |
|
|
| 84 |
|
/// Construct by moving from another buffer.
|
84 |
|
/// Construct by moving from another buffer.
|
| 85 |
|
basic_string_dynamic_buffer(
|
85 |
|
basic_string_dynamic_buffer(
|
| 86 |
|
basic_string_dynamic_buffer&& other) noexcept
|
86 |
|
basic_string_dynamic_buffer&& other) noexcept
|
| 87 |
|
|
87 |
|
|
| 88 |
|
, max_size_(other.max_size_)
|
88 |
|
, max_size_(other.max_size_)
|
| 89 |
|
, in_size_(other.in_size_)
|
89 |
|
, in_size_(other.in_size_)
|
| 90 |
|
, out_size_(other.out_size_)
|
90 |
|
, out_size_(other.out_size_)
|
| 91 |
|
|
91 |
|
|
| 92 |
|
|
92 |
|
|
| 93 |
|
|
93 |
|
|
| 94 |
|
|
94 |
|
|
| 95 |
|
/** Construct from an existing string.
|
95 |
|
/** Construct from an existing string.
|
| 96 |
|
|
96 |
|
|
| 97 |
|
@param s Pointer to the string to wrap. Must
|
97 |
|
@param s Pointer to the string to wrap. Must
|
| 98 |
|
remain valid for the lifetime of this object.
|
98 |
|
remain valid for the lifetime of this object.
|
| 99 |
|
@param max_size Optional upper bound on the number
|
99 |
|
@param max_size Optional upper bound on the number
|
| 100 |
|
of bytes the buffer may hold.
|
100 |
|
of bytes the buffer may hold.
|
| 101 |
|
|
101 |
|
|
| 102 |
|
|
102 |
|
|
| 103 |
|
basic_string_dynamic_buffer(
|
103 |
|
basic_string_dynamic_buffer(
|
| 104 |
|
|
104 |
|
|
| 105 |
|
|
105 |
|
|
| 106 |
|
std::size_t(-1)) noexcept
|
106 |
|
std::size_t(-1)) noexcept
|
| 107 |
|
|
107 |
|
|
| 108 |
|
|
108 |
|
|
| 109 |
|
max_size > s_->max_size()
|
109 |
|
max_size > s_->max_size()
|
| 110 |
|
|
110 |
|
|
| 111 |
|
|
111 |
|
|
| 112 |
|
|
112 |
|
|
| 113 |
|
if(s_->size() > max_size_)
|
113 |
|
if(s_->size() > max_size_)
|
| 114 |
|
|
114 |
|
|
| 115 |
|
|
115 |
|
|
| 116 |
|
|
116 |
|
|
| 117 |
|
|
117 |
|
|
| 118 |
|
/// Copy assignment is deleted.
|
118 |
|
/// Copy assignment is deleted.
|
| 119 |
|
basic_string_dynamic_buffer& operator=(
|
119 |
|
basic_string_dynamic_buffer& operator=(
|
| 120 |
|
basic_string_dynamic_buffer const&) = delete;
|
120 |
|
basic_string_dynamic_buffer const&) = delete;
|
| 121 |
|
|
121 |
|
|
| 122 |
|
/// Return the number of readable bytes.
|
122 |
|
/// Return the number of readable bytes.
|
| 123 |
|
|
123 |
|
|
| 124 |
|
|
124 |
|
|
| 125 |
|
|
125 |
|
|
| 126 |
|
|
126 |
|
|
| 127 |
|
|
127 |
|
|
| 128 |
|
|
128 |
|
|
| 129 |
|
/// Return the maximum number of bytes the buffer can hold.
|
129 |
|
/// Return the maximum number of bytes the buffer can hold.
|
| 130 |
|
|
130 |
|
|
| 131 |
|
max_size() const noexcept
|
131 |
|
max_size() const noexcept
|
| 132 |
|
|
132 |
|
|
| 133 |
|
|
133 |
|
|
| 134 |
|
|
134 |
|
|
| 135 |
|
|
135 |
|
|
| 136 |
|
/// Return the number of writable bytes without reallocation.
|
136 |
|
/// Return the number of writable bytes without reallocation.
|
| 137 |
|
|
137 |
|
|
| 138 |
|
capacity() const noexcept
|
138 |
|
capacity() const noexcept
|
| 139 |
|
|
139 |
|
|
| 140 |
|
if(s_->capacity() <= max_size_)
|
140 |
|
if(s_->capacity() <= max_size_)
|
| 141 |
|
return s_->capacity() - in_size_;
|
141 |
|
return s_->capacity() - in_size_;
|
| 142 |
|
return max_size_ - in_size_;
|
142 |
|
return max_size_ - in_size_;
|
| 143 |
|
|
143 |
|
|
| 144 |
|
|
144 |
|
|
| 145 |
|
/// Return a buffer sequence representing the readable bytes.
|
145 |
|
/// Return a buffer sequence representing the readable bytes.
|
| 146 |
|
|
146 |
|
|
| 147 |
|
|
147 |
|
|
| 148 |
|
|
148 |
|
|
| 149 |
|
return const_buffers_type(
|
149 |
|
return const_buffers_type(
|
| 150 |
|
|
150 |
|
|
| 151 |
|
|
151 |
|
|
| 152 |
|
|
152 |
|
|
| 153 |
|
/** Prepare writable space of at least `n` bytes.
|
153 |
|
/** Prepare writable space of at least `n` bytes.
|
| 154 |
|
|
154 |
|
|
| 155 |
|
Invalidates iterators and references returned by
|
155 |
|
Invalidates iterators and references returned by
|
| 156 |
|
previous calls to `data` and `prepare`.
|
156 |
|
previous calls to `data` and `prepare`.
|
| 157 |
|
|
157 |
|
|
| 158 |
|
@throws std::invalid_argument if `n` exceeds
|
158 |
|
@throws std::invalid_argument if `n` exceeds
|
| 159 |
|
|
159 |
|
|
| 160 |
|
|
160 |
|
|
| 161 |
|
@param n The number of bytes to prepare.
|
161 |
|
@param n The number of bytes to prepare.
|
| 162 |
|
|
162 |
|
|
| 163 |
|
@return A mutable buffer of exactly `n` bytes.
|
163 |
|
@return A mutable buffer of exactly `n` bytes.
|
| 164 |
|
|
164 |
|
|
| 165 |
|
|
165 |
|
|
| 166 |
|
|
166 |
|
|
| 167 |
|
|
167 |
|
|
| 168 |
|
// n exceeds available space
|
168 |
|
// n exceeds available space
|
| 169 |
|
if(n > max_size_ - in_size_)
|
169 |
|
if(n > max_size_ - in_size_)
|
| 170 |
|
detail::throw_invalid_argument();
|
170 |
|
detail::throw_invalid_argument();
|
| 171 |
|
|
171 |
|
|
| 172 |
|
if( s_->size() < in_size_ + n)
|
172 |
|
if( s_->size() < in_size_ + n)
|
| 173 |
|
s_->resize(in_size_ + n);
|
173 |
|
s_->resize(in_size_ + n);
|
| 174 |
|
|
174 |
|
|
| 175 |
|
return mutable_buffers_type(
|
175 |
|
return mutable_buffers_type(
|
| 176 |
|
&(*s_)[in_size_], out_size_);
|
176 |
|
&(*s_)[in_size_], out_size_);
|
| 177 |
|
|
177 |
|
|
| 178 |
|
|
178 |
|
|
| 179 |
|
/** Move bytes from the writable to the readable area.
|
179 |
|
/** Move bytes from the writable to the readable area.
|
| 180 |
|
|
180 |
|
|
| 181 |
|
Invalidates iterators and references returned by
|
181 |
|
Invalidates iterators and references returned by
|
| 182 |
|
previous calls to `data` and `prepare`.
|
182 |
|
previous calls to `data` and `prepare`.
|
| 183 |
|
|
183 |
|
|
| 184 |
|
@param n The number of bytes to commit. Clamped
|
184 |
|
@param n The number of bytes to commit. Clamped
|
| 185 |
|
to the size of the writable area.
|
185 |
|
to the size of the writable area.
|
| 186 |
|
|
186 |
|
|
| 187 |
|
void commit(std::size_t n) noexcept
|
187 |
|
void commit(std::size_t n) noexcept
|
| 188 |
|
|
188 |
|
|
| 189 |
|
|
189 |
|
|
| 190 |
|
|
190 |
|
|
| 191 |
|
|
191 |
|
|
| 192 |
|
|
192 |
|
|
| 193 |
|
|
193 |
|
|
| 194 |
|
|
194 |
|
|
| 195 |
|
|
195 |
|
|
| 196 |
|
|
196 |
|
|
| 197 |
|
/** Remove bytes from the beginning of the readable area.
|
197 |
|
/** Remove bytes from the beginning of the readable area.
|
| 198 |
|
|
198 |
|
|
| 199 |
|
Invalidates iterators and references returned by
|
199 |
|
Invalidates iterators and references returned by
|
| 200 |
|
previous calls to `data` and `prepare`.
|
200 |
|
previous calls to `data` and `prepare`.
|
| 201 |
|
|
201 |
|
|
| 202 |
|
@param n The number of bytes to consume. Clamped
|
202 |
|
@param n The number of bytes to consume. Clamped
|
| 203 |
|
to the number of readable bytes.
|
203 |
|
to the number of readable bytes.
|
| 204 |
|
|
204 |
|
|
| 205 |
|
void consume(std::size_t n) noexcept
|
205 |
|
void consume(std::size_t n) noexcept
|
| 206 |
|
|
206 |
|
|
| 207 |
|
|
207 |
|
|
| 208 |
|
|
208 |
|
|
| 209 |
|
|
209 |
|
|
| 210 |
|
|
210 |
|
|
| 211 |
|
|
211 |
|
|
| 212 |
|
|
212 |
|
|
| 213 |
|
|
213 |
|
|
| 214 |
|
|
214 |
|
|
| 215 |
|
|
215 |
|
|
| 216 |
|
|
216 |
|
|
| 217 |
|
|
217 |
|
|
| 218 |
|
|
218 |
|
|
| 219 |
|
|
219 |
|
|
| 220 |
|
|
220 |
|
|
| 221 |
|
/// A dynamic buffer using `std::string`.
|
221 |
|
/// A dynamic buffer using `std::string`.
|
| 222 |
|
using string_dynamic_buffer = basic_string_dynamic_buffer<char>;
|
222 |
|
using string_dynamic_buffer = basic_string_dynamic_buffer<char>;
|
| 223 |
|
|
223 |
|
|
| 224 |
|
/** Create a dynamic buffer from a string.
|
224 |
|
/** Create a dynamic buffer from a string.
|
| 225 |
|
|
225 |
|
|
| 226 |
|
@param s The string to wrap.
|
226 |
|
@param s The string to wrap.
|
| 227 |
|
@param max_size Optional maximum size limit.
|
227 |
|
@param max_size Optional maximum size limit.
|
| 228 |
|
@return A string_dynamic_buffer wrapping the string.
|
228 |
|
@return A string_dynamic_buffer wrapping the string.
|
| 229 |
|
|
229 |
|
|
| 230 |
|
template<class CharT, class Traits, class Allocator>
|
230 |
|
template<class CharT, class Traits, class Allocator>
|
| 231 |
|
basic_string_dynamic_buffer<CharT, Traits, Allocator>
|
231 |
|
basic_string_dynamic_buffer<CharT, Traits, Allocator>
|
| 232 |
|
|
232 |
|
|
| 233 |
|
std::basic_string<CharT, Traits, Allocator>& s,
|
233 |
|
std::basic_string<CharT, Traits, Allocator>& s,
|
| 234 |
|
std::size_t max_size = std::size_t(-1))
|
234 |
|
std::size_t max_size = std::size_t(-1))
|
| 235 |
|
|
235 |
|
|
| 236 |
|
return basic_string_dynamic_buffer<CharT, Traits, Allocator>(&s, max_size);
|
236 |
|
return basic_string_dynamic_buffer<CharT, Traits, Allocator>(&s, max_size);
|
| 237 |
|
|
237 |
|
|
| 238 |
|
|
238 |
|
|
| 239 |
|
|
239 |
|
|
| 240 |
|
|
240 |
|
|
| 241 |
|
|
241 |
|
|
| 242 |
|
|
242 |
|
|