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_READ_STREAM_HPP 10   #ifndef BOOST_COROSIO_IO_IO_READ_STREAM_HPP
11   #define BOOST_COROSIO_IO_IO_READ_STREAM_HPP 11   #define BOOST_COROSIO_IO_IO_READ_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 reads. 28 + /** Reads bytes from a stream asynchronously.
29   29  
30   Provides the `read_some` operation via a pure virtual 30   Provides the `read_some` operation via a pure virtual
31   `do_read_some` dispatch point. Concrete classes override 31   `do_read_some` dispatch point. Concrete classes override
32   `do_read_some` to route through their implementation. 32   `do_read_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_write_stream 35   @ref io_stream can combine this with @ref io_write_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_write_stream, io_stream, io_object 42   @see io_write_stream, io_stream, io_object
43   */ 43   */
44   class BOOST_COROSIO_DECL io_read_stream : virtual public io_object 44   class BOOST_COROSIO_DECL io_read_stream : virtual public io_object
45   { 45   {
46   protected: 46   protected:
47   /// Awaitable for async read operations. 47   /// Awaitable for async read operations.
48   template<class MutableBufferSequence> 48   template<class MutableBufferSequence>
49   struct read_some_awaitable 49   struct read_some_awaitable
50   : detail::bytes_op_base<read_some_awaitable<MutableBufferSequence>> 50   : detail::bytes_op_base<read_some_awaitable<MutableBufferSequence>>
51   { 51   {
52 - io_read_stream& ios_; 52 + private:
53 - MutableBufferSequence buffers_; 53 + friend io_read_stream;
54   54  
HITCBC 55   201982 read_some_awaitable( 55   198263 read_some_awaitable(
56   io_read_stream& ios, MutableBufferSequence buffers) noexcept 56   io_read_stream& ios, MutableBufferSequence buffers) noexcept
HITCBC 57   201982 : ios_(ios) 57   198263 : ios_(ios)
HITCBC 58   201982 , buffers_(std::move(buffers)) 58   198263 , buffers_(std::move(buffers))
59   { 59   {
HITCBC 60   201982 } 60   198263 }
61   61  
  62 + friend detail::bytes_op_base<
  63 + read_some_awaitable<MutableBufferSequence>>;
  64 + io_read_stream& ios_;
  65 + MutableBufferSequence buffers_;
  66 +
62   std::coroutine_handle<> 67   std::coroutine_handle<>
HITCBC 63   201973 dispatch(std::coroutine_handle<> h, capy::executor_ref ex) const 68   198254 dispatch(std::coroutine_handle<> h, capy::executor_ref ex) const
64   { 69   {
HITCBC 65   403946 return ios_.do_read_some( 70   396508 return ios_.do_read_some(
HITCBC 66   605919 h, ex, buffers_, this->token_, &this->ec_, &this->bytes_); 71   594762 h, ex, buffers_, this->token_, &this->ec_, &this->bytes_);
67   } 72   }
68   }; 73   };
69   74  
70   /** Dispatch a read through the concrete implementation. 75   /** Dispatch a read through the concrete implementation.
71   76  
72   @param h Coroutine handle to resume on completion. 77   @param h Coroutine handle to resume on completion.
73   @param ex Executor for dispatching the completion. 78   @param ex Executor for dispatching the completion.
74   @param buffers Target buffer sequence. 79   @param buffers Target buffer sequence.
75   @param token Stop token for cancellation. 80   @param token Stop token for cancellation.
76   @param ec Output error code. 81   @param ec Output error code.
77   @param bytes Output bytes transferred. 82   @param bytes Output bytes transferred.
78   83  
79   @return Coroutine handle to resume immediately. 84   @return Coroutine handle to resume immediately.
80   */ 85   */
81   virtual std::coroutine_handle<> do_read_some( 86   virtual std::coroutine_handle<> do_read_some(
82 - std::coroutine_handle<>, 87 + std::coroutine_handle<> h,
83 - capy::executor_ref, 88 + capy::executor_ref ex,
84 - buffer_param, 89 + buffer_param buffers,
85 - std::stop_token, 90 + std::stop_token token,
86 - std::error_code*, 91 + std::error_code* ec,
87 - std::size_t*) = 0; 92 + std::size_t* bytes) = 0;
88   93  
  94 + /// Default construct; the handle is supplied through @ref io_object.
HITCBC 89   10088 io_read_stream() noexcept = default; 95   7142 io_read_stream() noexcept = default;
90   96  
91 - io_read_stream(io_read_stream&&) noexcept = default; 97 + /// Move construct; the handle moves with @ref io_object.
  98 + io_read_stream(io_read_stream&&) noexcept = default;
  99 + /// Move assignment is disabled; reseating a live stream is not supported.
92   io_read_stream& operator=(io_read_stream&&) noexcept = delete; 100   io_read_stream& operator=(io_read_stream&&) noexcept = delete;
93 - io_read_stream(io_read_stream const&) = delete; 101 + /// Copy construction is disabled; the handle is uniquely owned.
94 - io_read_stream& operator=(io_read_stream const&) = delete; 102 + io_read_stream(io_read_stream const&) = delete;
  103 + /// Copy assignment is disabled; the handle is uniquely owned.
  104 + io_read_stream& operator=(io_read_stream const&) = delete;
95   105  
96   public: 106   public:
97   /** Asynchronously read data from the stream. 107   /** Asynchronously read data from the stream.
98   108  
99   Suspends the calling coroutine and initiates a kernel-level 109   Suspends the calling coroutine and initiates a kernel-level
100   read. The coroutine resumes when at least one byte is read, 110   read. The coroutine resumes when at least one byte is read,
101   an error occurs, or the operation is cancelled. 111   an error occurs, or the operation is cancelled.
102   112  
103   This stream must outlive the returned awaitable. The memory 113   This stream must outlive the returned awaitable. The memory
104   referenced by @p buffers must remain valid until the operation 114   referenced by @p buffers must remain valid until the operation
105   completes. 115   completes.
106   116  
107   A closed stream completes with `errc::bad_file_descriptor`. 117   A closed stream completes with `errc::bad_file_descriptor`.
108   118  
109   @param buffers The buffer sequence to read data into. 119   @param buffers The buffer sequence to read data into.
110   120  
111   @return An awaitable yielding `(error_code, std::size_t)`. 121   @return An awaitable yielding `(error_code, std::size_t)`.
112   122  
113   @see io_stream::write_some 123   @see io_stream::write_some
114   */ 124   */
115   template<capy::MutableBufferSequence MB> 125   template<capy::MutableBufferSequence MB>
HITCBC 116   201982 [[nodiscard]] auto read_some(MB const& buffers) 126   198263 [[nodiscard]] auto read_some(MB const& buffers)
117   { 127   {
HITCBC 118   201982 return read_some_awaitable<MB>(*this, buffers); 128   198263 return read_some_awaitable<MB>(*this, buffers);
119   } 129   }
120   }; 130   };
121   131  
122   } // namespace boost::corosio 132   } // namespace boost::corosio
123   133  
124   #endif 134   #endif