include/boost/corosio/io/io_read_stream.hpp
100.0% Lines (17/17)
100.0% List of functions (16/16)
100.0% Branches (3/3)
Functions (16)
Function
Calls
Lines
Branches
Blocks
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>)
:56
6x
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)
:56
237415x
100.0%
–
100.0%
boost::corosio::io_read_stream::read_some_awaitable<std::span<boost::capy::mutable_buffer const, 18446744073709551615ull> >::read_some_awaitable(boost::corosio::io_read_stream&, std::span<boost::capy::mutable_buffer const, 18446744073709551615ull>)
:56
34826x
100.0%
–
100.0%
boost::corosio::io_read_stream::read_some_awaitable<boost::capy::consuming_buffers<boost::capy::mutable_buffer> >::await_ready() const
:63
6x
100.0%
–
100.0%
boost::corosio::io_read_stream::read_some_awaitable<boost::capy::mutable_buffer>::await_ready() const
:63
237415x
100.0%
–
100.0%
boost::corosio::io_read_stream::read_some_awaitable<std::span<boost::capy::mutable_buffer const, 18446744073709551615ull> >::await_ready() const
:63
34826x
100.0%
–
100.0%
boost::corosio::io_read_stream::read_some_awaitable<boost::capy::consuming_buffers<boost::capy::mutable_buffer> >::await_resume() const
:68
6x
75.0%
50.0%
66.7%
boost::corosio::io_read_stream::read_some_awaitable<boost::capy::mutable_buffer>::await_resume() const
:68
237415x
100.0%
100.0%
100.0%
boost::corosio::io_read_stream::read_some_awaitable<std::span<boost::capy::mutable_buffer const, 18446744073709551615ull> >::await_resume() const
:68
34826x
100.0%
100.0%
100.0%
boost::corosio::io_read_stream::read_some_awaitable<boost::capy::consuming_buffers<boost::capy::mutable_buffer> >::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:75
6x
100.0%
100.0%
75.0%
boost::corosio::io_read_stream::read_some_awaitable<boost::capy::mutable_buffer>::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:75
237415x
100.0%
100.0%
75.0%
boost::corosio::io_read_stream::read_some_awaitable<std::span<boost::capy::mutable_buffer const, 18446744073709551615ull> >::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::io_env const*)
:75
34826x
100.0%
100.0%
75.0%
boost::corosio::io_read_stream::io_read_stream()
:103
4832x
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&)
:131
6x
100.0%
–
100.0%
auto boost::corosio::io_read_stream::read_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&)
:131
237415x
100.0%
–
100.0%
auto boost::corosio::io_read_stream::read_some<std::span<boost::capy::mutable_buffer const, 18446744073709551615ull> >(std::span<boost::capy::mutable_buffer const, 18446744073709551615ull> const&)
:131
34826x
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/io/io_object.hpp> | |||
| 15 | #include <boost/corosio/detail/buffer_param.hpp> | |||
| 16 | #include <boost/capy/io_result.hpp> | |||
| 17 | #include <boost/capy/ex/executor_ref.hpp> | |||
| 18 | #include <boost/capy/ex/io_env.hpp> | |||
| 19 | ||||
| 20 | #include <coroutine> | |||
| 21 | #include <cstddef> | |||
| 22 | #include <stop_token> | |||
| 23 | #include <system_error> | |||
| 24 | ||||
| 25 | namespace boost::corosio { | |||
| 26 | ||||
| 27 | /** Abstract base for streams that support async reads. | |||
| 28 | ||||
| 29 | Provides the `read_some` operation via a pure virtual | |||
| 30 | `do_read_some` dispatch point. Concrete classes override | |||
| 31 | `do_read_some` to route through their implementation. | |||
| 32 | ||||
| 33 | Uses virtual inheritance from @ref io_object so that | |||
| 34 | @ref io_stream can combine this with @ref io_write_stream | |||
| 35 | without duplicating the `io_object` base. | |||
| 36 | ||||
| 37 | @par Thread Safety | |||
| 38 | Distinct objects: Safe. | |||
| 39 | Shared objects: Unsafe. | |||
| 40 | ||||
| 41 | @see io_write_stream, io_stream, io_object | |||
| 42 | */ | |||
| 43 | class BOOST_COROSIO_DECL io_read_stream : virtual public io_object | |||
| 44 | { | |||
| 45 | protected: | |||
| 46 | /// Awaitable for async read operations. | |||
| 47 | template<class MutableBufferSequence> | |||
| 48 | struct read_some_awaitable | |||
| 49 | { | |||
| 50 | io_read_stream& ios_; | |||
| 51 | MutableBufferSequence buffers_; | |||
| 52 | std::stop_token token_; | |||
| 53 | mutable std::error_code ec_; | |||
| 54 | mutable std::size_t bytes_transferred_ = 0; | |||
| 55 | ||||
| 56 | 272247x | read_some_awaitable( | ||
| 57 | io_read_stream& ios, MutableBufferSequence buffers) noexcept | |||
| 58 | 272247x | : ios_(ios) | ||
| 59 | 272247x | , buffers_(std::move(buffers)) | ||
| 60 | { | |||
| 61 | 272247x | } | ||
| 62 | ||||
| 63 | 272247x | bool await_ready() const noexcept | ||
| 64 | { | |||
| 65 | 272247x | return token_.stop_requested(); | ||
| 66 | } | |||
| 67 | ||||
| 68 | 272247x | capy::io_result<std::size_t> await_resume() const noexcept | ||
| 69 | { | |||
| 70 |
2/2✓ Branch 3 → 4 taken 800 times.
✓ Branch 3 → 6 taken 271447 times.
|
272247x | if (token_.stop_requested()) | |
| 71 | 800x | return {make_error_code(std::errc::operation_canceled), 0}; | ||
| 72 | 271447x | return {ec_, bytes_transferred_}; | ||
| 73 | } | |||
| 74 | ||||
| 75 | 272247x | auto await_suspend(std::coroutine_handle<> h, capy::io_env const* env) | ||
| 76 | -> std::coroutine_handle<> | |||
| 77 | { | |||
| 78 | 272247x | token_ = env->stop_token; | ||
| 79 |
1/1✓ Branch 5 → 6 taken 272247 times.
|
544494x | return ios_.do_read_some( | |
| 80 | 816741x | h, env->executor, buffers_, token_, &ec_, &bytes_transferred_); | ||
| 81 | } | |||
| 82 | }; | |||
| 83 | ||||
| 84 | /** Dispatch a read through the concrete implementation. | |||
| 85 | ||||
| 86 | @param h Coroutine handle to resume on completion. | |||
| 87 | @param ex Executor for dispatching the completion. | |||
| 88 | @param buffers Target buffer sequence. | |||
| 89 | @param token Stop token for cancellation. | |||
| 90 | @param ec Output error code. | |||
| 91 | @param bytes Output bytes transferred. | |||
| 92 | ||||
| 93 | @return Coroutine handle to resume immediately. | |||
| 94 | */ | |||
| 95 | virtual std::coroutine_handle<> do_read_some( | |||
| 96 | std::coroutine_handle<>, | |||
| 97 | capy::executor_ref, | |||
| 98 | buffer_param, | |||
| 99 | std::stop_token, | |||
| 100 | std::error_code*, | |||
| 101 | std::size_t*) = 0; | |||
| 102 | ||||
| 103 | 4832x | io_read_stream() noexcept = default; | ||
| 104 | ||||
| 105 | /// Construct from a handle. | |||
| 106 | explicit io_read_stream(handle h) noexcept : io_object(std::move(h)) {} | |||
| 107 | ||||
| 108 | io_read_stream(io_read_stream&&) noexcept = default; | |||
| 109 | io_read_stream& operator=(io_read_stream&&) noexcept = delete; | |||
| 110 | io_read_stream(io_read_stream const&) = delete; | |||
| 111 | io_read_stream& operator=(io_read_stream const&) = delete; | |||
| 112 | ||||
| 113 | public: | |||
| 114 | /** Asynchronously read data from the stream. | |||
| 115 | ||||
| 116 | Suspends the calling coroutine and initiates a kernel-level | |||
| 117 | read. The coroutine resumes when at least one byte is read, | |||
| 118 | an error occurs, or the operation is cancelled. | |||
| 119 | ||||
| 120 | This stream must outlive the returned awaitable. The memory | |||
| 121 | referenced by @p buffers must remain valid until the operation | |||
| 122 | completes. | |||
| 123 | ||||
| 124 | @param buffers The buffer sequence to read data into. | |||
| 125 | ||||
| 126 | @return An awaitable yielding `(error_code, std::size_t)`. | |||
| 127 | ||||
| 128 | @see io_stream::write_some | |||
| 129 | */ | |||
| 130 | template<capy::MutableBufferSequence MB> | |||
| 131 | 272247x | auto read_some(MB const& buffers) | ||
| 132 | { | |||
| 133 | 272247x | return read_some_awaitable<MB>(*this, buffers); | ||
| 134 | } | |||
| 135 | }; | |||
| 136 | ||||
| 137 | } // namespace boost::corosio | |||
| 138 | ||||
| 139 | #endif | |||
| 140 |