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