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