include/boost/capy/read.hpp

100.0% Lines (2 / 2) 100.0% Functions (4 / 4)
read.hpp
f(x) Functions (4)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2026 Michael Vandeberg
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/capy
9 //
10
11 #ifndef BOOST_CAPY_READ_HPP
12 #define BOOST_CAPY_READ_HPP
13
14 #include <boost/capy/detail/config.hpp>
15 #include <boost/capy/cond.hpp>
16 #include <boost/capy/results.hpp>
17 #include <boost/capy/task.hpp>
18 #include <boost/capy/buffers.hpp>
19 #include <boost/capy/buffers/consuming_buffers.hpp>
20 #include <boost/capy/concept/read_stream.hpp>
21
22 #include <algorithm>
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 `results<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 @return A task yielding `results<std::size_t>` whose second element
70 is the number of bytes read.
71
72 @par Remarks
73 Supports _ContextAwaitable cancellation_.
74
75
76 @par Example
77
78 @par !example example
79
80
81 @see ReadStream, MutableBufferSequence
82 */
83 template <typename S, typename MB>
84 requires ReadStream<S> && MutableBufferSequence<MB>
85 auto
86 99x read(S& stream, MB buffers) ->
87 task<results<std::size_t>>
88 {
89 consuming_buffers consuming(buffers);
90 std::size_t const total_size = buffer_size(buffers);
91 std::size_t total_read = 0;
92
93 while(total_read < total_size)
94 {
95 auto [ec, n] = co_await stream.read_some(consuming.data());
96 consuming.consume(n);
97 total_read += n;
98 // A contingency that still completed the transfer is a success:
99 // report it only when the buffer was not filled.
100 if(ec && total_read < total_size)
101 co_return {ec, total_read};
102 }
103
104 co_return {std::error_code(), total_read};
105 198x }
106
107 } // namespace capy
108 } // namespace boost
109
110 #endif
111