100.00% Lines (10/10)
100.00% Functions (4/4)
| TLA | Baseline | Branch | ||||||
|---|---|---|---|---|---|---|---|---|
| Line | Hits | Code | Line | Hits | Code | |||
| 1 | // | 1 | // | |||||
| 2 | // Copyright (c) 2026 Steve Gerbino | 2 | // Copyright (c) 2026 Steve Gerbino | |||||
| 3 | + | // Copyright (c) 2026 Michael Vandeberg | ||||||
| 3 | // | 4 | // | |||||
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 5 | // 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 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||||
| 6 | // | 7 | // | |||||
| 7 | // Official repository: https://github.com/cppalliance/corosio | 8 | // Official repository: https://github.com/cppalliance/corosio | |||||
| 8 | // | 9 | // | |||||
| 9 | 10 | |||||||
| 10 | #ifndef BOOST_COROSIO_IO_IO_READ_STREAM_HPP | 11 | #ifndef BOOST_COROSIO_IO_IO_READ_STREAM_HPP | |||||
| 11 | #define BOOST_COROSIO_IO_IO_READ_STREAM_HPP | 12 | #define BOOST_COROSIO_IO_IO_READ_STREAM_HPP | |||||
| 12 | 13 | |||||||
| 13 | #include <boost/corosio/detail/config.hpp> | 14 | #include <boost/corosio/detail/config.hpp> | |||||
| 14 | #include <boost/corosio/detail/op_base.hpp> | 15 | #include <boost/corosio/detail/op_base.hpp> | |||||
| 15 | #include <boost/corosio/io/io_object.hpp> | 16 | #include <boost/corosio/io/io_object.hpp> | |||||
| 16 | #include <boost/corosio/detail/buffer_param.hpp> | 17 | #include <boost/corosio/detail/buffer_param.hpp> | |||||
| 17 | #include <boost/capy/results.hpp> | 18 | #include <boost/capy/results.hpp> | |||||
| 18 | #include <boost/capy/ex/executor_ref.hpp> | 19 | #include <boost/capy/ex/executor_ref.hpp> | |||||
| 19 | #include <boost/capy/ex/await_context.hpp> | 20 | #include <boost/capy/ex/await_context.hpp> | |||||
| 20 | 21 | |||||||
| 21 | #include <boost/capy/continuation.hpp> | 22 | #include <boost/capy/continuation.hpp> | |||||
| 22 | 23 | |||||||
| 23 | #include <coroutine> | 24 | #include <coroutine> | |||||
| 24 | #include <cstddef> | 25 | #include <cstddef> | |||||
| 25 | #include <stop_token> | 26 | #include <stop_token> | |||||
| 26 | #include <system_error> | 27 | #include <system_error> | |||||
| 27 | 28 | |||||||
| 28 | namespace boost::corosio { | 29 | namespace boost::corosio { | |||||
| 29 | 30 | |||||||
| 30 | /** Reads bytes from a stream asynchronously. | 31 | /** Reads bytes from a stream asynchronously. | |||||
| 31 | 32 | |||||||
| 32 | Provides the `read_some` operation via a pure virtual | 33 | Provides the `read_some` operation via a pure virtual | |||||
| 33 | `do_read_some` dispatch point. Concrete classes override | 34 | `do_read_some` dispatch point. Concrete classes override | |||||
| 34 | `do_read_some` to route through their implementation. | 35 | `do_read_some` to route through their implementation. | |||||
| 35 | 36 | |||||||
| 36 | Uses virtual inheritance from @ref io_object so that | 37 | Uses virtual inheritance from @ref io_object so that | |||||
| 37 | @ref io_stream can combine this with @ref io_write_stream | 38 | @ref io_stream can combine this with @ref io_write_stream | |||||
| 38 | without duplicating the `io_object` base. | 39 | without duplicating the `io_object` base. | |||||
| 39 | 40 | |||||||
| 40 | @par Thread Safety | 41 | @par Thread Safety | |||||
| 41 | Distinct objects: Safe. | 42 | Distinct objects: Safe. | |||||
| 42 | Shared objects: Unsafe. | 43 | Shared objects: Unsafe. | |||||
| 43 | 44 | |||||||
| 44 | - | @see io_write_stream, io_stream, io_object | 45 | + | @see @ref io_write_stream, @ref io_stream, @ref io_object | |||
| 45 | */ | 46 | */ | |||||
| 46 | class BOOST_COROSIO_DECL io_read_stream : virtual public io_object | 47 | class BOOST_COROSIO_DECL io_read_stream : virtual public io_object | |||||
| 47 | { | 48 | { | |||||
| 48 | protected: | 49 | protected: | |||||
| 49 | /// Awaitable for async read operations. | 50 | /// Awaitable for async read operations. | |||||
| 50 | template<class MutableBufferSequence> | 51 | template<class MutableBufferSequence> | |||||
| 51 | struct read_some_awaitable | 52 | struct read_some_awaitable | |||||
| 52 | : detail::bytes_op_base<read_some_awaitable<MutableBufferSequence>> | 53 | : detail::bytes_op_base<read_some_awaitable<MutableBufferSequence>> | |||||
| 53 | { | 54 | { | |||||
| 54 | private: | 55 | private: | |||||
| 55 | friend io_read_stream; | 56 | friend io_read_stream; | |||||
| 56 | 57 | |||||||
| HITCBC | 57 | 207047 | read_some_awaitable( | 58 | 235386 | read_some_awaitable( | ||
| 58 | io_read_stream& ios, MutableBufferSequence buffers) noexcept | 59 | io_read_stream& ios, MutableBufferSequence buffers) noexcept | |||||
| HITCBC | 59 | 207047 | : ios_(ios) | 60 | 235386 | : ios_(ios) | ||
| HITCBC | 60 | 207047 | , buffers_(std::move(buffers)) | 61 | 235386 | , buffers_(std::move(buffers)) | ||
| 61 | { | 62 | { | |||||
| HITCBC | 62 | 207047 | } | 63 | 235386 | } | ||
| 63 | 64 | |||||||
| 64 | friend detail::bytes_op_base< | 65 | friend detail::bytes_op_base< | |||||
| 65 | read_some_awaitable<MutableBufferSequence>>; | 66 | read_some_awaitable<MutableBufferSequence>>; | |||||
| 66 | io_read_stream& ios_; | 67 | io_read_stream& ios_; | |||||
| 67 | MutableBufferSequence buffers_; | 68 | MutableBufferSequence buffers_; | |||||
| 68 | 69 | |||||||
| 69 | std::coroutine_handle<> | 70 | std::coroutine_handle<> | |||||
| HITCBC | 70 | 207024 | dispatch(capy::continuation& cont, capy::executor_ref ex) const | 71 | 235359 | dispatch(capy::continuation& cont, capy::executor_ref ex) const | ||
| 71 | { | 72 | { | |||||
| HITCBC | 72 | 414048 | return ios_.do_read_some( | 73 | 470718 | return ios_.do_read_some( | ||
| HITCBC | 73 | 621072 | cont, ex, buffers_, this->token_, &this->ec_, &this->bytes_); | 74 | 706077 | cont, ex, buffers_, this->token_, &this->ec_, &this->bytes_); | ||
| 74 | } | 75 | } | |||||
| 75 | }; | 76 | }; | |||||
| 76 | 77 | |||||||
| 77 | /** Dispatch a read through the concrete implementation. | 78 | /** Dispatch a read through the concrete implementation. | |||||
| 78 | 79 | |||||||
| 79 | @param cont Continuation to resume on completion; it lives in | 80 | @param cont Continuation to resume on completion; it lives in | |||||
| 80 | the awaiting frame until then. | 81 | the awaiting frame until then. | |||||
| 81 | @param ex Executor for dispatching the completion. | 82 | @param ex Executor for dispatching the completion. | |||||
| 82 | @param buffers Target buffer sequence. | 83 | @param buffers Target buffer sequence. | |||||
| 83 | @param token Stop token for cancellation. | 84 | @param token Stop token for cancellation. | |||||
| 84 | @param ec Output error code. | 85 | @param ec Output error code. | |||||
| 85 | @param bytes Output bytes transferred. | 86 | @param bytes Output bytes transferred. | |||||
| 86 | 87 | |||||||
| 87 | @return Coroutine handle to resume immediately. | 88 | @return Coroutine handle to resume immediately. | |||||
| 88 | */ | 89 | */ | |||||
| 89 | virtual std::coroutine_handle<> do_read_some( | 90 | virtual std::coroutine_handle<> do_read_some( | |||||
| 90 | capy::continuation& cont, | 91 | capy::continuation& cont, | |||||
| 91 | capy::executor_ref ex, | 92 | capy::executor_ref ex, | |||||
| 92 | buffer_param buffers, | 93 | buffer_param buffers, | |||||
| 93 | std::stop_token token, | 94 | std::stop_token token, | |||||
| 94 | std::error_code* ec, | 95 | std::error_code* ec, | |||||
| 95 | std::size_t* bytes) = 0; | 96 | std::size_t* bytes) = 0; | |||||
| 96 | 97 | |||||||
| 97 | /// Default construct; the handle is supplied through @ref io_object. | 98 | /// Default construct; the handle is supplied through @ref io_object. | |||||
| HITCBC | 98 | 26077 | io_read_stream() noexcept = default; | 99 | 25997 | io_read_stream() noexcept = default; | ||
| 99 | 100 | |||||||
| 100 | /// Move construct; the handle moves with @ref io_object. | 101 | /// Move construct; the handle moves with @ref io_object. | |||||
| 101 | io_read_stream(io_read_stream&&) noexcept = default; | 102 | io_read_stream(io_read_stream&&) noexcept = default; | |||||
| 102 | /// Move assignment is disabled; reseating a live stream is not supported. | 103 | /// Move assignment is disabled; reseating a live stream is not supported. | |||||
| 103 | io_read_stream& operator=(io_read_stream&&) noexcept = delete; | 104 | io_read_stream& operator=(io_read_stream&&) noexcept = delete; | |||||
| 104 | /// Copy construction is disabled; the handle is uniquely owned. | 105 | /// Copy construction is disabled; the handle is uniquely owned. | |||||
| 105 | io_read_stream(io_read_stream const&) = delete; | 106 | io_read_stream(io_read_stream const&) = delete; | |||||
| 106 | /// Copy assignment is disabled; the handle is uniquely owned. | 107 | /// Copy assignment is disabled; the handle is uniquely owned. | |||||
| 107 | io_read_stream& operator=(io_read_stream const&) = delete; | 108 | io_read_stream& operator=(io_read_stream const&) = delete; | |||||
| 108 | 109 | |||||||
| 109 | public: | 110 | public: | |||||
| 110 | /** Asynchronously read data from the stream. | 111 | /** Asynchronously read data from the stream. | |||||
| 111 | 112 | |||||||
| 112 | Suspends the calling coroutine and initiates a kernel-level | 113 | Suspends the calling coroutine and initiates a kernel-level | |||||
| 113 | read. The coroutine resumes when at least one byte is read, | 114 | read. The coroutine resumes when at least one byte is read, | |||||
| 114 | an error occurs, or the operation is cancelled. | 115 | an error occurs, or the operation is cancelled. | |||||
| 115 | 116 | |||||||
| 116 | This stream must outlive the returned awaitable. The memory | 117 | This stream must outlive the returned awaitable. The memory | |||||
| 117 | referenced by @p buffers must remain valid until the operation | 118 | referenced by @p buffers must remain valid until the operation | |||||
| 118 | completes. | 119 | completes. | |||||
| 119 | 120 | |||||||
| 120 | A closed stream completes with `errc::bad_file_descriptor`. | 121 | A closed stream completes with `errc::bad_file_descriptor`. | |||||
| 121 | 122 | |||||||
| 122 | @param buffers The buffer sequence to read data into. | 123 | @param buffers The buffer sequence to read data into. | |||||
| 123 | 124 | |||||||
| 124 | @return An awaitable yielding `(error_code, std::size_t)`. | 125 | @return An awaitable yielding `(error_code, std::size_t)`. | |||||
| 125 | 126 | |||||||
| 126 | - | @see io_stream::write_some | 127 | + | @see @ref io_stream::write_some | |||
| 127 | */ | 128 | */ | |||||
| 128 | template<capy::MutableBufferSequence MB> | 129 | template<capy::MutableBufferSequence MB> | |||||
| HITCBC | 129 | 207047 | [[nodiscard]] auto read_some(MB const& buffers) | 130 | 235386 | [[nodiscard]] auto read_some(MB const& buffers) | ||
| 130 | { | 131 | { | |||||
| HITCBC | 131 | 207047 | return read_some_awaitable<MB>(*this, buffers); | 132 | 235386 | return read_some_awaitable<MB>(*this, buffers); | ||
| 132 | } | 133 | } | |||||
| 133 | }; | 134 | }; | |||||
| 134 | 135 | |||||||
| 135 | } // namespace boost::corosio | 136 | } // namespace boost::corosio | |||||
| 136 | 137 | |||||||
| 137 | #endif | 138 | #endif | |||||