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