include/boost/corosio/io/io_read_stream.hpp

88.9% Lines (8/9) 100.0% List of functions (16/16) 50.0% Branches (4/8)
io_read_stream.hpp
f(x) Functions (16)
Function Calls Lines Branches Blocks
boost::corosio::io_read_stream::~io_read_stream() :44 16872x 100.0% 100.0% boost::corosio::io_read_stream::read_some_awaitable<boost::capy::consuming_buffers<boost::capy::mutable_buffer>>::read_some_awaitable(boost::corosio::io_read_stream::read_some_awaitable<boost::capy::consuming_buffers<boost::capy::mutable_buffer>>&&) :49 26x 100.0% 100.0% boost::corosio::io_read_stream::read_some_awaitable<boost::capy::consuming_buffers<boost::capy::mutable_buffer>>::~read_some_awaitable() :49 52x 100.0% 100.0% boost::corosio::io_read_stream::read_some_awaitable<boost::capy::mutable_buffer>::read_some_awaitable(boost::corosio::io_read_stream::read_some_awaitable<boost::capy::mutable_buffer>&&) :49 1467132x 100.0% 100.0% boost::corosio::io_read_stream::read_some_awaitable<boost::capy::mutable_buffer>::~read_some_awaitable() :49 2934266x 100.0% 100.0% boost::corosio::io_read_stream::read_some_awaitable<std::__1::span<boost::capy::mutable_buffer const, 18446744073709551615ul>>::~read_some_awaitable() :49 48492x 100.0% 100.0% boost::corosio::io_read_stream::read_some_awaitable<boost::capy::consuming_buffers<boost::capy::mutable_buffer>>::read_some_awaitable(boost::corosio::io_read_stream&, boost::capy::consuming_buffers<boost::capy::mutable_buffer>) :55 26x 100.0% 100.0% boost::corosio::io_read_stream::read_some_awaitable<boost::capy::mutable_buffer>::read_some_awaitable(boost::corosio::io_read_stream&, boost::capy::mutable_buffer) :55 1467134x 100.0% 100.0% boost::corosio::io_read_stream::read_some_awaitable<std::__1::span<boost::capy::mutable_buffer const, 18446744073709551615ul>>::read_some_awaitable(boost::corosio::io_read_stream&, std::__1::span<boost::capy::mutable_buffer const, 18446744073709551615ul>) :55 48492x 100.0% 100.0% boost::corosio::io_read_stream::read_some_awaitable<boost::capy::consuming_buffers<boost::capy::mutable_buffer>>::dispatch(std::__1::coroutine_handle<void>, boost::capy::executor_ref) const :59 13x 75.0% 25.0% 50.0% boost::corosio::io_read_stream::read_some_awaitable<boost::capy::mutable_buffer>::dispatch(std::__1::coroutine_handle<void>, boost::capy::executor_ref) const :59 733567x 75.0% 25.0% 50.0% boost::corosio::io_read_stream::read_some_awaitable<std::__1::span<boost::capy::mutable_buffer const, 18446744073709551615ul>>::dispatch(std::__1::coroutine_handle<void>, boost::capy::executor_ref) const :59 24246x 100.0% 50.0% boost::corosio::io_read_stream::io_read_stream() :86 16872x 100.0% 100.0% auto boost::corosio::io_read_stream::read_some<boost::capy::consuming_buffers<boost::capy::mutable_buffer>>(boost::capy::consuming_buffers<boost::capy::mutable_buffer> const&) :114 13x 100.0% 100.0% auto boost::corosio::io_read_stream::read_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&) :114 733567x 100.0% 100.0% auto boost::corosio::io_read_stream::read_some<std::__1::span<boost::capy::mutable_buffer const, 18446744073709551615ul>>(std::__1::span<boost::capy::mutable_buffer const, 18446744073709551615ul> const&) :114 24246x 100.0% 100.0%
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Steve Gerbino
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/corosio
8 //
9
10 #ifndef BOOST_COROSIO_IO_IO_READ_STREAM_HPP
11 #define BOOST_COROSIO_IO_IO_READ_STREAM_HPP
12
13 #include <boost/corosio/detail/config.hpp>
14 #include <boost/corosio/detail/op_base.hpp>
15 #include <boost/corosio/io/io_object.hpp>
16 #include <boost/corosio/detail/buffer_param.hpp>
17 #include <boost/capy/io_result.hpp>
18 #include <boost/capy/ex/executor_ref.hpp>
19 #include <boost/capy/ex/io_env.hpp>
20
21 #include <coroutine>
22 #include <cstddef>
23 #include <stop_token>
24 #include <system_error>
25
26 namespace boost::corosio {
27
28 /** Abstract base for streams that support async reads.
29
30 Provides the `read_some` operation via a pure virtual
31 `do_read_some` dispatch point. Concrete classes override
32 `do_read_some` to route through their implementation.
33
34 Uses virtual inheritance from @ref io_object so that
35 @ref io_stream can combine this with @ref io_write_stream
36 without duplicating the `io_object` base.
37
38 @par Thread Safety
39 Distinct objects: Safe.
40 Shared objects: Unsafe.
41
42 @see io_write_stream, io_stream, io_object
43 */
44 class BOOST_COROSIO_DECL io_read_stream : virtual public io_object
45 {
46 protected:
47 /// Awaitable for async read operations.
48 template<class MutableBufferSequence>
49 struct read_some_awaitable
50 : detail::bytes_op_base<read_some_awaitable<MutableBufferSequence>>
51 {
52 io_read_stream& ios_;
53 MutableBufferSequence buffers_;
54
55 1515652x read_some_awaitable(
56 io_read_stream& ios, MutableBufferSequence buffers) noexcept
57 1515652x : ios_(ios), buffers_(std::move(buffers)) {}
58
59 757826x std::coroutine_handle<> dispatch(
60 std::coroutine_handle<> h, capy::executor_ref ex) const
61 {
62
4/8
✓ Branch 0 taken 757813 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 757813 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
1515652x return ios_.do_read_some(
63 757826x h, ex, buffers_, this->token_, &this->ec_, &this->bytes_);
64 }
65 };
66
67 /** Dispatch a read through the concrete implementation.
68
69 @param h Coroutine handle to resume on completion.
70 @param ex Executor for dispatching the completion.
71 @param buffers Target buffer sequence.
72 @param token Stop token for cancellation.
73 @param ec Output error code.
74 @param bytes Output bytes transferred.
75
76 @return Coroutine handle to resume immediately.
77 */
78 virtual std::coroutine_handle<> do_read_some(
79 std::coroutine_handle<>,
80 capy::executor_ref,
81 buffer_param,
82 std::stop_token,
83 std::error_code*,
84 std::size_t*) = 0;
85
86 16872x io_read_stream() noexcept = default;
87
88 /// Construct from a handle.
89 explicit io_read_stream(handle h) noexcept : io_object(std::move(h)) {}
90
91 io_read_stream(io_read_stream&&) noexcept = default;
92 io_read_stream& operator=(io_read_stream&&) noexcept = delete;
93 io_read_stream(io_read_stream const&) = delete;
94 io_read_stream& operator=(io_read_stream const&) = delete;
95
96 public:
97 /** Asynchronously read data from the stream.
98
99 Suspends the calling coroutine and initiates a kernel-level
100 read. The coroutine resumes when at least one byte is read,
101 an error occurs, or the operation is cancelled.
102
103 This stream must outlive the returned awaitable. The memory
104 referenced by @p buffers must remain valid until the operation
105 completes.
106
107 @param buffers The buffer sequence to read data into.
108
109 @return An awaitable yielding `(error_code, std::size_t)`.
110
111 @see io_stream::write_some
112 */
113 template<capy::MutableBufferSequence MB>
114 757826x auto read_some(MB const& buffers)
115 {
116 757826x return read_some_awaitable<MB>(*this, buffers);
117 }
118 };
119
120 } // namespace boost::corosio
121
122 #endif
123