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