100.00% Lines (6/6) 100.00% Functions (3/3)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
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/capy 7   // Official repository: https://github.com/cppalliance/capy
8   // 8   //
9   9  
10   #ifndef BOOST_CAPY_READ_HPP 10   #ifndef BOOST_CAPY_READ_HPP
11   #define BOOST_CAPY_READ_HPP 11   #define BOOST_CAPY_READ_HPP
12   12  
13   #include <boost/capy/detail/config.hpp> 13   #include <boost/capy/detail/config.hpp>
14   #include <boost/capy/cond.hpp> 14   #include <boost/capy/cond.hpp>
15   #include <boost/capy/io_task.hpp> 15   #include <boost/capy/io_task.hpp>
16   #include <boost/capy/buffers.hpp> 16   #include <boost/capy/buffers.hpp>
17   #include <boost/capy/buffers/buffer_slice.hpp> 17   #include <boost/capy/buffers/buffer_slice.hpp>
18   #include <boost/capy/concept/dynamic_buffer.hpp> 18   #include <boost/capy/concept/dynamic_buffer.hpp>
19   #include <boost/capy/concept/read_source.hpp> 19   #include <boost/capy/concept/read_source.hpp>
20   #include <boost/capy/concept/read_stream.hpp> 20   #include <boost/capy/concept/read_stream.hpp>
21   #include <system_error> 21   #include <system_error>
22   22  
23   #include <cstddef> 23   #include <cstddef>
24   24  
25   namespace boost { 25   namespace boost {
26   namespace capy { 26   namespace capy {
27   27  
28   /** Read data from a stream until the buffer sequence is full. 28   /** Read data from a stream until the buffer sequence is full.
29   29  
30   @par Await-effects 30   @par Await-effects
31   31  
32   Reads data from `stream` via awaiting `stream.read_some` repeatedly 32   Reads data from `stream` via awaiting `stream.read_some` repeatedly
33   until: 33   until:
34   34  
35   @li either the entire buffer sequence @c buffers is filled, 35   @li either the entire buffer sequence @c buffers is filled,
36 - @li or a contingency occurs. 36 + @li or a contingency occurs on `stream.read_some`.
37   37  
38   If `buffer_size(buffers) == 0` then no awaiting `stream.read_some` 38   If `buffer_size(buffers) == 0` then no awaiting `stream.read_some`
39   is performed. This is not a contingency. 39   is performed. This is not a contingency.
40   40  
41   @par Await-returns 41   @par Await-returns
42   An object of type `io_result<std::size_t>` destructuring as `[ec, n]`. 42   An object of type `io_result<std::size_t>` destructuring as `[ec, n]`.
43   43  
44   Upon a contingency, `n` represents the number of bytes read so far, 44   Upon a contingency, `n` represents the number of bytes read so far,
45   inclusive of the last partial read. 45   inclusive of the last partial read.
46   46  
47   Contingencies: 47   Contingencies:
48   48  
49 - @li The first contingency reported from awaiting @c stream.read_some . 49 + @li The first contingency reported from awaiting @c stream.read_some
  50 + while `buffers` is not yet filled. A contingency that accompanies
  51 + the read which fills `buffers` is not reported: a completed
  52 + transfer is a success.
50   53  
51   Notable conditions: 54   Notable conditions:
52   55  
53   @li @c cond::canceled — Operation was cancelled, 56   @li @c cond::canceled — Operation was cancelled,
54 - @li @c cond::eof — Stream reached end before `buffers` was filled. 57 + @li @c cond::eof — Stream reached end before @c buffers was filled.
55   58  
56   @par Await-postcondition 59   @par Await-postcondition
57 - `ec || n == buffer_size(buffers)`. 60 + If `n == buffer_size(buffers)` the transfer completed and `ec` is
  61 + success; otherwise `ec` is set.
58   62  
59   @param stream The stream to read from. If the lifetime of `stream` ends 63   @param stream The stream to read from. If the lifetime of `stream` ends
60   before the coroutine finishes, the behavior is undefined. 64   before the coroutine finishes, the behavior is undefined.
61   65  
62   @param buffers The buffer sequence to fill. If the lifetime of the buffer 66   @param buffers The buffer sequence to fill. If the lifetime of the buffer
63   sequence represented by `buffers` ends before the coroutine finishes, the behavior is undefined. 67   sequence represented by `buffers` ends before the coroutine finishes, the behavior is undefined.
64   68  
65   69  
66   @par Remarks 70   @par Remarks
67   Supports _IoAwaitable cancellation_. 71   Supports _IoAwaitable cancellation_.
68   72  
69   73  
70   @par Example 74   @par Example
71   75  
72   @code 76   @code
73   capy::task<> process_message(capy::ReadStream auto& stream) 77   capy::task<> process_message(capy::ReadStream auto& stream)
74   { 78   {
75   std::vector<char> header(16); // known header size for some protocol 79   std::vector<char> header(16); // known header size for some protocol
76   auto [ec, n] = co_await capy::read(stream, capy::mutable_buffer(header)); 80   auto [ec, n] = co_await capy::read(stream, capy::mutable_buffer(header));
77   if (ec == capy::cond::eof) 81   if (ec == capy::cond::eof)
78   co_return; // Connection closed 82   co_return; // Connection closed
79   if (ec) 83   if (ec)
80   throw std::system_error(ec); 84   throw std::system_error(ec);
81   85  
82   // at this point `header` contains exactly 16 bytes 86   // at this point `header` contains exactly 16 bytes
83   } 87   }
84   @endcode 88   @endcode
85   89  
86   @see ReadStream, MutableBufferSequence 90   @see ReadStream, MutableBufferSequence
87   */ 91   */
88   template <typename S, typename MB> 92   template <typename S, typename MB>
89   requires ReadStream<S> && MutableBufferSequence<MB> 93   requires ReadStream<S> && MutableBufferSequence<MB>
90   auto 94   auto
HITCBC 91   94 read(S& stream, MB buffers) -> 95   98 read(S& stream, MB buffers) ->
92   io_task<std::size_t> 96   io_task<std::size_t>
93   { 97   {
94   auto consuming = buffer_slice(buffers); 98   auto consuming = buffer_slice(buffers);
95   std::size_t const total_size = buffer_size(buffers); 99   std::size_t const total_size = buffer_size(buffers);
96   std::size_t total_read = 0; 100   std::size_t total_read = 0;
97   101  
98   while(total_read < total_size) 102   while(total_read < total_size)
99   { 103   {
100   auto [ec, n] = co_await stream.read_some(consuming.data()); 104   auto [ec, n] = co_await stream.read_some(consuming.data());
101   consuming.remove_prefix(n); 105   consuming.remove_prefix(n);
102   total_read += n; 106   total_read += n;
103 - if(ec) 107 + // A contingency that still completed the transfer is a success:
  108 + // report it only when the buffer was not filled.
  109 + if(ec && total_read < total_size)
104   co_return {ec, total_read}; 110   co_return {ec, total_read};
105   } 111   }
106   112  
107   co_return {{}, total_read}; 113   co_return {{}, total_read};
HITCBC 108   188 } 114   196 }
109   115  
110   /** Read all data from a stream into a dynamic buffer. 116   /** Read all data from a stream into a dynamic buffer.
111   117  
112   @par Await-effects 118   @par Await-effects
113   119  
114   Reads data from `stream` via awaiting `stream.read_some` repeatedly 120   Reads data from `stream` via awaiting `stream.read_some` repeatedly
115 - and appending the results to `dynbuf`, 121 + and appending it to `dynbuf` using prepare/commit semantics
116 - until a contingency occurs. 122 + until:
  123 +
  124 + @li either @c dynbuf.size() == @c dynbuf.max_size() ,
  125 + @li or a contingency on @c stream.read_some occurs.
  126 +
  127 + The last, potenitally partial, read is also appended.
  128 +
  129 + The value passed in the first call to `dynbuf.prepare` is `initial_amount`.
  130 + The value is grown to 1.5 times the preceding value only after a read that
  131 + completely filled the prepared buffer; otherwise it is left unchanged for
  132 + the next call.
117 - Data is appended using prepare/commit semantics.  
118 - The buffer grows with 1.5x factor when filled.  
119   133  
120   134  
121   @par Await-returns 135   @par Await-returns
122   136  
123   An object of type `io_result<std::size_t>` destructuring as `[ec, n]`. 137   An object of type `io_result<std::size_t>` destructuring as `[ec, n]`.
124   138  
125   `n` represents the total number of bytes read, 139   `n` represents the total number of bytes read,
126   inclusive of the last partial read. 140   inclusive of the last partial read.
127   141  
128   Contingencies: 142   Contingencies:
129   143  
130   @li The first contingency, other than one matching to @c cond::eof, reported from awaiting @c stream.read_some . 144   @li The first contingency, other than one matching to @c cond::eof, reported from awaiting @c stream.read_some .
131   145  
  146 +
132   @par Await-throws 147   @par Await-throws
133 - `std::bad_alloc` when append to `dynbuf` fails. 148 +
  149 + Whatever operations on @c dunbuf throw.
  150 +
  151 + (Note: types modeling @c DynamicBufferParam provided by Capy throw
  152 + @c std::bad_alloc from member function
  153 + @c prepare .)
  154 +
134   155  
135   @param stream The stream to read from. If the lifetime of `stream` ends 156   @param stream The stream to read from. If the lifetime of `stream` ends
136   before the coroutine finishes, the behavior is undefined. 157   before the coroutine finishes, the behavior is undefined.
137   158  
138   @param dynbuf The dynamic buffer to append data to. If the lifetime of the buffer 159   @param dynbuf The dynamic buffer to append data to. If the lifetime of the buffer
139   sequence represented by `dynbuf` ends before the coroutine finishes, the behavior is undefined. 160   sequence represented by `dynbuf` ends before the coroutine finishes, the behavior is undefined.
140   161  
141 - @param initial_amount Initial bytes to prepare (default 2048). 162 + @param initial_amount Hint for the value to be passed in the initial call to `dynbuf.prepare()`
  163 + (default 2048).
142   164  
143   165  
144   @par Remarks 166   @par Remarks
145   Supports _IoAwaitable cancellation_. 167   Supports _IoAwaitable cancellation_.
146   168  
147   @par Example 169   @par Example
148   170  
149   @code 171   @code
150   capy::task<std::string> read_body(capy::ReadStream auto& stream) 172   capy::task<std::string> read_body(capy::ReadStream auto& stream)
151   { 173   {
152   std::string body; 174   std::string body;
153   auto [ec, n] = co_await capy::read(stream, capy::dynamic_buffer(body)); 175   auto [ec, n] = co_await capy::read(stream, capy::dynamic_buffer(body));
154   if (ec) 176   if (ec)
155   throw std::system_error(ec); 177   throw std::system_error(ec);
156   return body; 178   return body;
157   } 179   }
158   @endcode 180   @endcode
159   181  
160   @see read_some, ReadStream, DynamicBufferParam 182   @see read_some, ReadStream, DynamicBufferParam
161   */ 183   */
162   template <typename S, typename DB> 184   template <typename S, typename DB>
163   requires ReadStream<S> && DynamicBufferParam<DB> 185   requires ReadStream<S> && DynamicBufferParam<DB>
164   auto 186   auto
HITCBC 165   80 read( 187   80 read(
166   S& stream, 188   S& stream,
167   DB&& dynbuf, 189   DB&& dynbuf,
168   std::size_t initial_amount = 2048) -> 190   std::size_t initial_amount = 2048) ->
169   io_task<std::size_t> 191   io_task<std::size_t>
170   { 192   {
171   std::size_t amount = initial_amount; 193   std::size_t amount = initial_amount;
172   std::size_t total_read = 0; 194   std::size_t total_read = 0;
173   for(;;) 195   for(;;)
174   { 196   {
175   auto mb = dynbuf.prepare(amount); 197   auto mb = dynbuf.prepare(amount);
176   auto const mb_size = buffer_size(mb); 198   auto const mb_size = buffer_size(mb);
177   auto [ec, n] = co_await stream.read_some(mb); 199   auto [ec, n] = co_await stream.read_some(mb);
178   dynbuf.commit(n); 200   dynbuf.commit(n);
179   total_read += n; 201   total_read += n;
180   if(ec == cond::eof) 202   if(ec == cond::eof)
181   co_return {{}, total_read}; 203   co_return {{}, total_read};
182   if(ec) 204   if(ec)
183   co_return {ec, total_read}; 205   co_return {ec, total_read};
184   if(n == mb_size) 206   if(n == mb_size)
185   amount = amount / 2 + amount; 207   amount = amount / 2 + amount;
186   } 208   }
HITCBC 187   160 } 209   160 }
188   210  
189   /** Read all data from a source into a dynamic buffer. 211   /** Read all data from a source into a dynamic buffer.
190   212  
191   @par Await-effects 213   @par Await-effects
192   214  
193   Reads data from `stream` by calling `source.read` repeatedly 215   Reads data from `stream` by calling `source.read` repeatedly
194 - and appending it to `dynbuf` until a contingency occurs. 216 + and appending it to `dynbuf` using prepare/commit semantics
  217 + until:
  218 +
  219 + @li either @c dynbuf.size() == @c dynbuf.max_size() ,
  220 + @li or a contingency on @c stream.read occurs.
  221 +
195   The last, potenitally partial, read is also appended. 222   The last, potenitally partial, read is also appended.
196   223  
197 - Data is appended using prepare/commit semantics. 224 + The value passed in the first call to `dynbuf.prepare` is `initial_amount`.
198 - The buffer grows with 1.5x factor when filled. 225 + The value is grown to 1.5 times the preceding value only after a read that
  226 + completely filled the prepared buffer; otherwise it is left unchanged for
  227 + the next call.
  228 +
199   229  
200   @par Await-returns 230   @par Await-returns
201   231  
202   An object of type `io_result<std::size_t>` destructuring as `[ec, n]`. 232   An object of type `io_result<std::size_t>` destructuring as `[ec, n]`.
203   233  
204   `n` represents the total number of bytes read, 234   `n` represents the total number of bytes read,
205   inclusive of the last partial read. 235   inclusive of the last partial read.
206   236  
207   237  
208   Contingencies: 238   Contingencies:
209   239  
210   @li The first contingency, other than one matching to @c cond::eof, reported from awaiting @c stream.read_some . 240   @li The first contingency, other than one matching to @c cond::eof, reported from awaiting @c stream.read_some .
211   241  
  242 +
212   @par Await-throws 243   @par Await-throws
  244 +
  245 + Whatever operations on @c dunbuf throw.
213   246  
214 - `std::bad_alloc` when append to `dynbuf` fails. 247 + (Note: types modeling @c DynamicBufferParam provided by Capy throw
  248 + @c std::bad_alloc from member function
  249 + @c prepare .)
  250 +
215   251  
216   @param source The source to read from. If the lifetime of `source` ends 252   @param source The source to read from. If the lifetime of `source` ends
217   before the coroutine finishes, the behavior is undefined. 253   before the coroutine finishes, the behavior is undefined.
218   254  
219   @param dynbuf The dynamic buffer to append data to. If the lifetime of the 255   @param dynbuf The dynamic buffer to append data to. If the lifetime of the
220   buffer sequence represented by `dynbuf` ends before the coroutine finishes, 256   buffer sequence represented by `dynbuf` ends before the coroutine finishes,
221   the behavior is undefined. 257   the behavior is undefined.
222   258  
223 - @param initial_amount Initial bytes to prepare (default 2048). 259 + @param initial_amount Hint for the value to be passed in the initial call to `dynbuf.prepare()`
  260 + (default 2048).
224   261  
225   @par Remarks 262   @par Remarks
226   Supports _IoAwaitable cancellation_. 263   Supports _IoAwaitable cancellation_.
227   264  
228   @par Example 265   @par Example
229   266  
230   @code 267   @code
231   capy::task<std::string> read_body(capy::ReadSource auto& source) 268   capy::task<std::string> read_body(capy::ReadSource auto& source)
232   { 269   {
233   std::string body; 270   std::string body;
234   auto [ec, n] = co_await capy::read(source, capy::dynamic_buffer(body)); 271   auto [ec, n] = co_await capy::read(source, capy::dynamic_buffer(body));
235   if (ec) 272   if (ec)
236   throw std::system_error(ec); 273   throw std::system_error(ec);
237   return body; 274   return body;
238   } 275   }
239   @endcode 276   @endcode
240   277  
241   @see ReadSource, DynamicBufferParam 278   @see ReadSource, DynamicBufferParam
242   */ 279   */
243   template <typename S, typename DB> 280   template <typename S, typename DB>
244   requires ReadSource<S> && DynamicBufferParam<DB> 281   requires ReadSource<S> && DynamicBufferParam<DB>
245   auto 282   auto
HITCBC 246   54 read( 283   54 read(
247   S& source, 284   S& source,
248   DB&& dynbuf, 285   DB&& dynbuf,
249   std::size_t initial_amount = 2048) -> 286   std::size_t initial_amount = 2048) ->
250   io_task<std::size_t> 287   io_task<std::size_t>
251   { 288   {
252   std::size_t amount = initial_amount; 289   std::size_t amount = initial_amount;
253   std::size_t total_read = 0; 290   std::size_t total_read = 0;
254   for(;;) 291   for(;;)
255   { 292   {
256   auto mb = dynbuf.prepare(amount); 293   auto mb = dynbuf.prepare(amount);
257   auto const mb_size = buffer_size(mb); 294   auto const mb_size = buffer_size(mb);
258   auto [ec, n] = co_await source.read(mb); 295   auto [ec, n] = co_await source.read(mb);
259   dynbuf.commit(n); 296   dynbuf.commit(n);
260   total_read += n; 297   total_read += n;
261   if(ec == cond::eof) 298   if(ec == cond::eof)
262   co_return {{}, total_read}; 299   co_return {{}, total_read};
263   if(ec) 300   if(ec)
264   co_return {ec, total_read}; 301   co_return {ec, total_read};
265   if(n == mb_size) 302   if(n == mb_size)
266   amount = amount / 2 + amount; // 1.5x growth 303   amount = amount / 2 + amount; // 1.5x growth
267   } 304   }
HITCBC 268   108 } 305   108 }
269   306  
270   } // namespace capy 307   } // namespace capy
271   } // namespace boost 308   } // namespace boost
272   309  
273   #endif 310   #endif