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   /** Abstract base for streams that support async writes. 28   /** Abstract base for streams that support async writes.
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   io_write_stream& ios_; 52   io_write_stream& ios_;
53   ConstBufferSequence buffers_; 53   ConstBufferSequence buffers_;
54   54  
HITCBC 55   218144 write_some_awaitable( 55   222378 write_some_awaitable(
56   io_write_stream& ios, ConstBufferSequence buffers) noexcept 56   io_write_stream& ios, ConstBufferSequence buffers) noexcept
HITCBC 57   218144 : ios_(ios) 57   222378 : ios_(ios)
HITCBC 58   218144 , buffers_(std::move(buffers)) 58   222378 , buffers_(std::move(buffers))
59   { 59   {
HITCBC 60   218144 } 60   222378 }
61   61  
62   std::coroutine_handle<> 62   std::coroutine_handle<>
HITCBC 63   218144 dispatch(std::coroutine_handle<> h, capy::executor_ref ex) const 63   222378 dispatch(std::coroutine_handle<> h, capy::executor_ref ex) const
64   { 64   {
HITCBC 65   436288 return ios_.do_write_some( 65   444756 return ios_.do_write_some(
HITCBC 66   654432 h, ex, buffers_, this->token_, &this->ec_, &this->bytes_); 66   667134 h, ex, buffers_, this->token_, &this->ec_, &this->bytes_);
67   } 67   }
68   }; 68   };
69   69  
70   /** Dispatch a write through the concrete implementation. 70   /** Dispatch a write through the concrete implementation.
71   71  
72   @param h Coroutine handle to resume on completion. 72   @param h Coroutine handle to resume on completion.
73   @param ex Executor for dispatching the completion. 73   @param ex Executor for dispatching the completion.
74   @param buffers Source buffer sequence. 74   @param buffers Source buffer sequence.
75   @param token Stop token for cancellation. 75   @param token Stop token for cancellation.
76   @param ec Output error code. 76   @param ec Output error code.
77   @param bytes Output bytes transferred. 77   @param bytes Output bytes transferred.
78   78  
79   @return Coroutine handle to resume immediately. 79   @return Coroutine handle to resume immediately.
80   */ 80   */
81   virtual std::coroutine_handle<> do_write_some( 81   virtual std::coroutine_handle<> do_write_some(
82   std::coroutine_handle<>, 82   std::coroutine_handle<>,
83   capy::executor_ref, 83   capy::executor_ref,
84   buffer_param, 84   buffer_param,
85   std::stop_token, 85   std::stop_token,
86   std::error_code*, 86   std::error_code*,
87   std::size_t*) = 0; 87   std::size_t*) = 0;
88   88  
HITCBC 89   10297 io_write_stream() noexcept = default; 89   10347 io_write_stream() noexcept = default;
90   90  
91   io_write_stream(io_write_stream&&) noexcept = default; 91   io_write_stream(io_write_stream&&) noexcept = default;
92   io_write_stream& operator=(io_write_stream&&) noexcept = delete; 92   io_write_stream& operator=(io_write_stream&&) noexcept = delete;
93   io_write_stream(io_write_stream const&) = delete; 93   io_write_stream(io_write_stream const&) = delete;
94   io_write_stream& operator=(io_write_stream const&) = delete; 94   io_write_stream& operator=(io_write_stream const&) = delete;
95   95  
96   public: 96   public:
97   /** Asynchronously write data to the stream. 97   /** Asynchronously write data to the stream.
98   98  
99   Suspends the calling coroutine and initiates a kernel-level 99   Suspends the calling coroutine and initiates a kernel-level
100   write. The coroutine resumes when at least one byte is written, 100   write. The coroutine resumes when at least one byte is written,
101   an error occurs, or the operation is cancelled. 101   an error occurs, or the operation is cancelled.
102   102  
103   This stream must outlive the returned awaitable. The memory 103   This stream must outlive the returned awaitable. The memory
104   referenced by @p buffers must remain valid until the operation 104   referenced by @p buffers must remain valid until the operation
105   completes. 105   completes.
106   106  
107   A closed stream completes with `errc::bad_file_descriptor`. 107   A closed stream completes with `errc::bad_file_descriptor`.
108   108  
109   @param buffers The buffer sequence containing data to write. 109   @param buffers The buffer sequence containing data to write.
110   110  
111   @return An awaitable yielding `(error_code, std::size_t)`. 111   @return An awaitable yielding `(error_code, std::size_t)`.
112   112  
113   @see io_stream::read_some 113   @see io_stream::read_some
114   */ 114   */
115   template<capy::ConstBufferSequence CB> 115   template<capy::ConstBufferSequence CB>
HITCBC 116   218144 [[nodiscard]] auto write_some(CB const& buffers) 116   222378 [[nodiscard]] auto write_some(CB const& buffers)
117   { 117   {
HITCBC 118   218144 return write_some_awaitable<CB>(*this, buffers); 118   222378 return write_some_awaitable<CB>(*this, buffers);
119   } 119   }
120   }; 120   };
121   121  
122   } // namespace boost::corosio 122   } // namespace boost::corosio
123   123  
124   #endif 124   #endif