include/boost/capy/read.hpp

100.0% Lines (6/6) 100.0% List of functions (8/8)
read.hpp
f(x) Functions (8)
Function Calls Lines Blocks
boost::capy::task<boost::capy::io_result<unsigned long> > boost::capy::read<boost::capy::contingent_read_stream, boost::capy::mutable_buffer>(boost::capy::contingent_read_stream&, boost::capy::mutable_buffer) :95 4x 100.0% 44.0% boost::capy::task<boost::capy::io_result<unsigned long> > boost::capy::read<boost::capy::test::read_stream, boost::capy::mutable_buffer>(boost::capy::test::read_stream&, boost::capy::mutable_buffer) :95 22x 100.0% 44.0% boost::capy::task<boost::capy::io_result<unsigned long> > boost::capy::read<boost::capy::test::read_stream, std::array<boost::capy::mutable_buffer, 2ul> >(boost::capy::test::read_stream&, std::array<boost::capy::mutable_buffer, 2ul>) :95 34x 100.0% 44.0% boost::capy::task<boost::capy::io_result<unsigned long> > boost::capy::read<boost::capy::test::stream, boost::capy::mutable_buffer>(boost::capy::test::stream&, boost::capy::mutable_buffer) :95 38x 100.0% 44.0% boost::capy::task<boost::capy::io_result<unsigned long> > boost::capy::read<boost::capy::test::read_stream, boost::capy::basic_string_dynamic_buffer<char, std::char_traits<char>, std::allocator<char> >&>(boost::capy::test::read_stream&, boost::capy::basic_string_dynamic_buffer<char, std::char_traits<char>, std::allocator<char> >&, unsigned long) :187 50x 100.0% 44.0% boost::capy::task<boost::capy::io_result<unsigned long> > boost::capy::read<boost::capy::test::read_stream, boost::capy::circular_dynamic_buffer&>(boost::capy::test::read_stream&, boost::capy::circular_dynamic_buffer&, unsigned long) :187 30x 100.0% 44.0% boost::capy::task<boost::capy::io_result<unsigned long> > boost::capy::read<boost::capy::test::read_source, boost::capy::basic_string_dynamic_buffer<char, std::char_traits<char>, std::allocator<char> >&>(boost::capy::test::read_source&, boost::capy::basic_string_dynamic_buffer<char, std::char_traits<char>, std::allocator<char> >&, unsigned long) :283 30x 100.0% 44.0% boost::capy::task<boost::capy::io_result<unsigned long> > boost::capy::read<boost::capy::test::read_source, boost::capy::circular_dynamic_buffer&>(boost::capy::test::read_source&, boost::capy::circular_dynamic_buffer&, unsigned long) :283 24x 100.0% 44.0%
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 //
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)
6 //
7 // Official repository: https://github.com/cppalliance/capy
8 //
9
10 #ifndef BOOST_CAPY_READ_HPP
11 #define BOOST_CAPY_READ_HPP
12
13 #include <boost/capy/detail/config.hpp>
14 #include <boost/capy/cond.hpp>
15 #include <boost/capy/io_task.hpp>
16 #include <boost/capy/buffers.hpp>
17 #include <boost/capy/buffers/buffer_slice.hpp>
18 #include <boost/capy/concept/dynamic_buffer.hpp>
19 #include <boost/capy/concept/read_source.hpp>
20 #include <boost/capy/concept/read_stream.hpp>
21 #include <system_error>
22
23 #include <cstddef>
24
25 namespace boost {
26 namespace capy {
27
28 /** Read data from a stream until the buffer sequence is full.
29
30 @par Await-effects
31
32 Reads data from `stream` via awaiting `stream.read_some` repeatedly
33 until:
34
35 @li either the entire buffer sequence @c buffers is filled,
36 @li or a contingency occurs on `stream.read_some`.
37
38 If `buffer_size(buffers) == 0` then no awaiting `stream.read_some`
39 is performed. This is not a contingency.
40
41 @par Await-returns
42 An object of type `io_result<std::size_t>` destructuring as `[ec, n]`.
43
44 Upon a contingency, `n` represents the number of bytes read so far,
45 inclusive of the last partial read.
46
47 Contingencies:
48
49 @li The first contingency reported from awaiting @c stream.read_some
50 while `buffers` is not yet filled. A contingency that accompanies
51 the read which fills `buffers` is not reported: a completed
52 transfer is a success.
53
54 Notable conditions:
55
56 @li @c cond::canceled — Operation was cancelled,
57 @li @c cond::eof — Stream reached end before @c buffers was filled.
58
59 @par Await-postcondition
60 If `n == buffer_size(buffers)` the transfer completed and `ec` is
61 success; otherwise `ec` is set.
62
63 @param stream The stream to read from. If the lifetime of `stream` ends
64 before the coroutine finishes, the behavior is undefined.
65
66 @param buffers The buffer sequence to fill. If the lifetime of the buffer
67 sequence represented by `buffers` ends before the coroutine finishes, the behavior is undefined.
68
69
70 @par Remarks
71 Supports _IoAwaitable cancellation_.
72
73
74 @par Example
75
76 @code
77 capy::task<> process_message(capy::ReadStream auto& stream)
78 {
79 std::vector<char> header(16); // known header size for some protocol
80 auto [ec, n] = co_await capy::read(stream, capy::mutable_buffer(header));
81 if (ec == capy::cond::eof)
82 co_return; // Connection closed
83 if (ec)
84 throw std::system_error(ec);
85
86 // at this point `header` contains exactly 16 bytes
87 }
88 @endcode
89
90 @see ReadStream, MutableBufferSequence
91 */
92 template <typename S, typename MB>
93 requires ReadStream<S> && MutableBufferSequence<MB>
94 auto
95 98x read(S& stream, MB buffers) ->
96 io_task<std::size_t>
97 {
98 auto consuming = buffer_slice(buffers);
99 std::size_t const total_size = buffer_size(buffers);
100 std::size_t total_read = 0;
101
102 while(total_read < total_size)
103 {
104 auto [ec, n] = co_await stream.read_some(consuming.data());
105 consuming.remove_prefix(n);
106 total_read += n;
107 // A contingency that still completed the transfer is a success:
108 // report it only when the buffer was not filled.
109 if(ec && total_read < total_size)
110 co_return {ec, total_read};
111 }
112
113 co_return {{}, total_read};
114 196x }
115
116 /** Read all data from a stream into a dynamic buffer.
117
118 @par Await-effects
119
120 Reads data from `stream` via awaiting `stream.read_some` repeatedly
121 and appending it to `dynbuf` using prepare/commit semantics
122 until:
123
124 @li either @c dynbuf.size() == @c dynbuf.max_size() ,
125 @li or a contingency on @c stream.read_some occurs.
126
127 The last, potenitally partial, read is also appended.
128
129 The value passed in the first call to `dynbuf.prepare` is `initial_amount`.
130 The value is grown to 1.5 times the preceding value only after a read that
131 completely filled the prepared buffer; otherwise it is left unchanged for
132 the next call.
133
134
135 @par Await-returns
136
137 An object of type `io_result<std::size_t>` destructuring as `[ec, n]`.
138
139 `n` represents the total number of bytes read,
140 inclusive of the last partial read.
141
142 Contingencies:
143
144 @li The first contingency, other than one matching to @c cond::eof, reported from awaiting @c stream.read_some .
145
146
147 @par Await-throws
148
149 Whatever operations on @c dunbuf throw.
150
151 (Note: types modeling @c DynamicBufferParam provided by Capy throw
152 @c std::bad_alloc from member function
153 @c prepare .)
154
155
156 @param stream The stream to read from. If the lifetime of `stream` ends
157 before the coroutine finishes, the behavior is undefined.
158
159 @param dynbuf The dynamic buffer to append data to. If the lifetime of the buffer
160 sequence represented by `dynbuf` ends before the coroutine finishes, the behavior is undefined.
161
162 @param initial_amount Hint for the value to be passed in the initial call to `dynbuf.prepare()`
163 (default 2048).
164
165
166 @par Remarks
167 Supports _IoAwaitable cancellation_.
168
169 @par Example
170
171 @code
172 capy::task<std::string> read_body(capy::ReadStream auto& stream)
173 {
174 std::string body;
175 auto [ec, n] = co_await capy::read(stream, capy::dynamic_buffer(body));
176 if (ec)
177 throw std::system_error(ec);
178 return body;
179 }
180 @endcode
181
182 @see read_some, ReadStream, DynamicBufferParam
183 */
184 template <typename S, typename DB>
185 requires ReadStream<S> && DynamicBufferParam<DB>
186 auto
187 80x read(
188 S& stream,
189 DB&& dynbuf,
190 std::size_t initial_amount = 2048) ->
191 io_task<std::size_t>
192 {
193 std::size_t amount = initial_amount;
194 std::size_t total_read = 0;
195 for(;;)
196 {
197 auto mb = dynbuf.prepare(amount);
198 auto const mb_size = buffer_size(mb);
199 auto [ec, n] = co_await stream.read_some(mb);
200 dynbuf.commit(n);
201 total_read += n;
202 if(ec == cond::eof)
203 co_return {{}, total_read};
204 if(ec)
205 co_return {ec, total_read};
206 if(n == mb_size)
207 amount = amount / 2 + amount;
208 }
209 160x }
210
211 /** Read all data from a source into a dynamic buffer.
212
213 @par Await-effects
214
215 Reads data from `stream` by calling `source.read` repeatedly
216 and appending it to `dynbuf` using prepare/commit semantics
217 until:
218
219 @li either @c dynbuf.size() == @c dynbuf.max_size() ,
220 @li or a contingency on @c stream.read occurs.
221
222 The last, potenitally partial, read is also appended.
223
224 The value passed in the first call to `dynbuf.prepare` is `initial_amount`.
225 The value is grown to 1.5 times the preceding value only after a read that
226 completely filled the prepared buffer; otherwise it is left unchanged for
227 the next call.
228
229
230 @par Await-returns
231
232 An object of type `io_result<std::size_t>` destructuring as `[ec, n]`.
233
234 `n` represents the total number of bytes read,
235 inclusive of the last partial read.
236
237
238 Contingencies:
239
240 @li The first contingency, other than one matching to @c cond::eof, reported from awaiting @c stream.read_some .
241
242
243 @par Await-throws
244
245 Whatever operations on @c dunbuf throw.
246
247 (Note: types modeling @c DynamicBufferParam provided by Capy throw
248 @c std::bad_alloc from member function
249 @c prepare .)
250
251
252 @param source The source to read from. If the lifetime of `source` ends
253 before the coroutine finishes, the behavior is undefined.
254
255 @param dynbuf The dynamic buffer to append data to. If the lifetime of the
256 buffer sequence represented by `dynbuf` ends before the coroutine finishes,
257 the behavior is undefined.
258
259 @param initial_amount Hint for the value to be passed in the initial call to `dynbuf.prepare()`
260 (default 2048).
261
262 @par Remarks
263 Supports _IoAwaitable cancellation_.
264
265 @par Example
266
267 @code
268 capy::task<std::string> read_body(capy::ReadSource auto& source)
269 {
270 std::string body;
271 auto [ec, n] = co_await capy::read(source, capy::dynamic_buffer(body));
272 if (ec)
273 throw std::system_error(ec);
274 return body;
275 }
276 @endcode
277
278 @see ReadSource, DynamicBufferParam
279 */
280 template <typename S, typename DB>
281 requires ReadSource<S> && DynamicBufferParam<DB>
282 auto
283 54x read(
284 S& source,
285 DB&& dynbuf,
286 std::size_t initial_amount = 2048) ->
287 io_task<std::size_t>
288 {
289 std::size_t amount = initial_amount;
290 std::size_t total_read = 0;
291 for(;;)
292 {
293 auto mb = dynbuf.prepare(amount);
294 auto const mb_size = buffer_size(mb);
295 auto [ec, n] = co_await source.read(mb);
296 dynbuf.commit(n);
297 total_read += n;
298 if(ec == cond::eof)
299 co_return {{}, total_read};
300 if(ec)
301 co_return {ec, total_read};
302 if(n == mb_size)
303 amount = amount / 2 + amount; // 1.5x growth
304 }
305 108x }
306
307 } // namespace capy
308 } // namespace boost
309
310 #endif
311