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