TLA Line data 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/detail/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 : /** Asynchronously 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 `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 `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 `cond::canceled`: the operation was canceled.
57 : @li `cond::eof`: the stream reached its end before `buffers` was
58 : filled.
59 :
60 : @par Await-postcondition
61 : If `n == buffer_size(buffers)` the transfer completed and `ec` is
62 : success; otherwise `ec` is set.
63 :
64 : @par Remarks
65 : Cancellation follows the stop token of the caller's
66 : @ref await_context.
67 :
68 : @par Exception Safety
69 : Calling `read` allocates the coroutine frame, which throws if the
70 : frame allocator fails. Awaiting the task rethrows any exception
71 : thrown by awaiting `stream.read_some`.
72 :
73 : @par Example
74 :
75 : @par !example example
76 :
77 :
78 : @param stream The stream to read from. If the lifetime of `stream` ends
79 : before the coroutine finishes, the behavior is undefined.
80 :
81 : @param buffers The buffer sequence to fill. If the lifetime of the
82 : buffer sequence represented by `buffers` ends before the coroutine
83 : finishes, the behavior is undefined.
84 :
85 : @return A task yielding `results<std::size_t>` whose second element
86 : is the number of bytes read.
87 :
88 : @see @ref ReadStream, @ref MutableBufferSequence
89 : */
90 : template <typename S, typename MB>
91 : requires ReadStream<S> && MutableBufferSequence<MB>
92 : auto
93 HIT 101 : read(S& stream, MB buffers) ->
94 : task<results<std::size_t>>
95 : {
96 : detail::consuming_buffers consuming(buffers);
97 : std::size_t const total_size = buffer_size(buffers);
98 : std::size_t total_read = 0;
99 :
100 : while(total_read < total_size)
101 : {
102 : auto [ec, n] = co_await stream.read_some(consuming.data());
103 : consuming.consume(n);
104 : total_read += n;
105 : // A contingency that still completed the transfer is a success:
106 : // report it only when the buffer was not filled.
107 : if(ec && total_read < total_size)
108 : co_return {ec, total_read};
109 : }
110 :
111 : co_return {std::error_code(), total_read};
112 202 : }
113 :
114 : } // namespace capy
115 : } // namespace boost
116 :
117 : #endif
|