include/boost/http/io/buffer_param.hpp
100.0% Lines (31 / 31)
96.4% Functions (54 / 56)
Functions (56)
Function
Calls
Lines
Blocks
boost::http::buffer_param<boost::capy::const_buffer, false>::refill()
:160
147x
100.0%
100.0%
boost::http::buffer_param<boost::capy::const_buffer, true>::refill()
:160
124x
100.0%
100.0%
boost::http::buffer_param<boost::capy::mutable_buffer, false>::refill()
:160
212x
100.0%
100.0%
boost::http::buffer_param<boost::span<boost::capy::mutable_buffer const, 18446744073709551615ul>, false>::refill()
:160
2x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::const_buffer, 2ul>, false>::refill()
:160
4x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::const_buffer, 2ul>, true>::refill()
:160
16x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 1ul>, false>::refill()
:160
108x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 20ul>, false>::refill()
:160
26x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 2ul>, false>::refill()
:160
16x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 32ul>, false>::refill()
:160
8x
100.0%
100.0%
boost::http::buffer_param<std::vector<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, false>::refill()
:160
16x
100.0%
100.0%
boost::http::buffer_param<std::vector<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, true>::refill()
:160
26x
100.0%
100.0%
boost::http::buffer_param<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, false>::refill()
:160
3x
100.0%
100.0%
boost::http::buffer_param<boost::capy::const_buffer, false>::buffer_param(boost::capy::const_buffer const&)
:180
102x
100.0%
100.0%
boost::http::buffer_param<boost::capy::const_buffer, true>::buffer_param(boost::capy::const_buffer const&)
:180
87x
100.0%
100.0%
boost::http::buffer_param<boost::capy::mutable_buffer, false>::buffer_param(boost::capy::mutable_buffer const&)
:180
168x
100.0%
100.0%
boost::http::buffer_param<boost::span<boost::capy::mutable_buffer const, 18446744073709551615ul>, false>::buffer_param(boost::span<boost::capy::mutable_buffer const, 18446744073709551615ul> const&)
:180
2x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::const_buffer, 2ul>, false>::buffer_param(std::array<boost::capy::const_buffer, 2ul> const&)
:180
2x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::const_buffer, 2ul>, true>::buffer_param(std::array<boost::capy::const_buffer, 2ul> const&)
:180
12x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 1ul>, false>::buffer_param(std::array<boost::capy::mutable_buffer, 1ul> const&)
:180
108x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 20ul>, false>::buffer_param(std::array<boost::capy::mutable_buffer, 20ul> const&)
:180
16x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 2ul>, false>::buffer_param(std::array<boost::capy::mutable_buffer, 2ul> const&)
:180
12x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 32ul>, false>::buffer_param(std::array<boost::capy::mutable_buffer, 32ul> const&)
:180
6x
100.0%
100.0%
boost::http::buffer_param<std::vector<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, false>::buffer_param(std::vector<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> > const&)
:180
7x
100.0%
100.0%
boost::http::buffer_param<std::vector<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, true>::buffer_param(std::vector<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> > const&)
:180
16x
100.0%
100.0%
boost::http::buffer_param<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, false>::buffer_param(std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> > const&)
:180
2x
100.0%
100.0%
boost::http::buffer_param<boost::capy::const_buffer, false>::data()
:202
134x
100.0%
100.0%
boost::http::buffer_param<boost::capy::const_buffer, true>::data()
:202
110x
100.0%
100.0%
boost::http::buffer_param<boost::capy::mutable_buffer, false>::data()
:202
207x
100.0%
100.0%
boost::http::buffer_param<boost::span<boost::capy::mutable_buffer const, 18446744073709551615ul>, false>::data()
:202
2x
66.7%
60.0%
boost::http::buffer_param<std::array<boost::capy::const_buffer, 2ul>, false>::data()
:202
4x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::const_buffer, 2ul>, true>::data()
:202
16x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 1ul>, false>::data()
:202
364x
66.7%
60.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 20ul>, false>::data()
:202
26x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 2ul>, false>::data()
:202
16x
100.0%
100.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 32ul>, false>::data()
:202
8x
83.3%
70.0%
boost::http::buffer_param<std::vector<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, false>::data()
:202
14x
100.0%
100.0%
boost::http::buffer_param<std::vector<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, true>::data()
:202
26x
100.0%
100.0%
boost::http::buffer_param<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, false>::data()
:202
2x
100.0%
100.0%
boost::http::buffer_param<boost::capy::const_buffer, false>::more() const
:221
1x
100.0%
100.0%
boost::http::buffer_param<boost::capy::const_buffer, true>::more() const
:221
26x
100.0%
100.0%
boost::http::buffer_param<std::vector<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, false>::more() const
:221
4x
100.0%
100.0%
boost::http::buffer_param<std::vector<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, true>::more() const
:221
12x
100.0%
100.0%
boost::http::buffer_param<boost::capy::const_buffer, false>::consume(unsigned long)
:235
33x
100.0%
100.0%
boost::http::buffer_param<boost::capy::const_buffer, true>::consume(unsigned long)
:235
23x
77.8%
80.0%
boost::http::buffer_param<boost::capy::mutable_buffer, false>::consume(unsigned long)
:235
40x
100.0%
90.0%
boost::http::buffer_param<boost::span<boost::capy::mutable_buffer const, 18446744073709551615ul>, false>::consume(unsigned long)
:235
0
0.0%
0.0%
boost::http::buffer_param<std::array<boost::capy::const_buffer, 2ul>, false>::consume(unsigned long)
:235
0
0.0%
0.0%
boost::http::buffer_param<std::array<boost::capy::const_buffer, 2ul>, true>::consume(unsigned long)
:235
4x
77.8%
80.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 1ul>, false>::consume(unsigned long)
:235
182x
77.8%
90.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 20ul>, false>::consume(unsigned long)
:235
10x
77.8%
80.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 2ul>, false>::consume(unsigned long)
:235
4x
77.8%
80.0%
boost::http::buffer_param<std::array<boost::capy::mutable_buffer, 32ul>, false>::consume(unsigned long)
:235
4x
77.8%
80.0%
boost::http::buffer_param<std::vector<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, false>::consume(unsigned long)
:235
8x
77.8%
80.0%
boost::http::buffer_param<std::vector<boost::capy::const_buffer, std::allocator<boost::capy::const_buffer> >, true>::consume(unsigned long)
:235
10x
77.8%
80.0%
boost::http::buffer_param<std::vector<boost::capy::mutable_buffer, std::allocator<boost::capy::mutable_buffer> >, false>::consume(unsigned long)
:235
1x
77.8%
80.0%
| Line | TLA | Hits | Source Code |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | ||
| 3 | // Copyright (c) 2026 Michael Vandeberg | ||
| 4 | // Copyright (c) 2026 Steve Gerbino | ||
| 5 | // | ||
| 6 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| 7 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 8 | // | ||
| 9 | // Official repository: https://github.com/cppalliance/http | ||
| 10 | // | ||
| 11 | |||
| 12 | /* | ||
| 13 | COROUTINE BUFFER SEQUENCE LIFETIME REQUIREMENT | ||
| 14 | =============================================== | ||
| 15 | Buffer sequence parameters in coroutine APIs MUST be passed BY VALUE, | ||
| 16 | never by reference. When a coroutine suspends, reference parameters may | ||
| 17 | dangle if the caller's object goes out of scope before resumption. | ||
| 18 | |||
| 19 | CORRECT: task<> read_some(MutableBufferSequence auto buffers) | ||
| 20 | WRONG: task<> read_some(MutableBufferSequence auto& buffers) | ||
| 21 | WRONG: task<> read_some(MutableBufferSequence auto const& buffers) | ||
| 22 | |||
| 23 | The buffer_param class works with this model: it takes a const& in its | ||
| 24 | constructor (for the non-coroutine scope) but the caller's template | ||
| 25 | function accepts the buffer sequence by value, ensuring the sequence | ||
| 26 | lives in the coroutine frame. | ||
| 27 | */ | ||
| 28 | |||
| 29 | #ifndef BOOST_HTTP_IO_BUFFER_PARAM_HPP | ||
| 30 | #define BOOST_HTTP_IO_BUFFER_PARAM_HPP | ||
| 31 | |||
| 32 | #include <boost/capy/detail/config.hpp> | ||
| 33 | #include <boost/capy/buffers.hpp> | ||
| 34 | |||
| 35 | #include <new> | ||
| 36 | #include <span> | ||
| 37 | #include <type_traits> | ||
| 38 | |||
| 39 | namespace boost { | ||
| 40 | namespace http { | ||
| 41 | |||
| 42 | /** A buffer sequence wrapper providing windowed access. | ||
| 43 | |||
| 44 | This template class wraps any buffer sequence and provides | ||
| 45 | incremental access through a sliding window of buffer | ||
| 46 | descriptors. It handles both const and mutable buffer | ||
| 47 | sequences automatically. | ||
| 48 | |||
| 49 | @par Coroutine Lifetime Requirement | ||
| 50 | |||
| 51 | When used in coroutine APIs, the outer template function | ||
| 52 | MUST accept the buffer sequence parameter BY VALUE: | ||
| 53 | |||
| 54 | @code | ||
| 55 | // CORRECT | ||
| 56 | capy::task<> write(capy::ConstBufferSequence auto buffers); | ||
| 57 | |||
| 58 | // WRONG - dangling reference | ||
| 59 | capy::task<> write(capy::ConstBufferSequence auto& buffers); | ||
| 60 | @endcode | ||
| 61 | |||
| 62 | Pass-by-value ensures the buffer sequence is copied into | ||
| 63 | the coroutine frame and remains valid across suspension | ||
| 64 | points. References would dangle when the caller's scope | ||
| 65 | exits before the coroutine resumes. | ||
| 66 | |||
| 67 | @par Purpose | ||
| 68 | |||
| 69 | When iterating through large buffer sequences, it is often | ||
| 70 | more efficient to process buffers in batches rather than | ||
| 71 | one at a time. This class maintains a window of up to a | ||
| 72 | fixed, implementation-defined number of buffer descriptors | ||
| 73 | (currently 16). It refills the window from the underlying | ||
| 74 | sequence as buffers are consumed. | ||
| 75 | |||
| 76 | @par Example | ||
| 77 | |||
| 78 | Create a `buffer_param` from any buffer sequence and use | ||
| 79 | `data()` to get the current window of buffers. After | ||
| 80 | processing some bytes, call `consume()` to advance through | ||
| 81 | the sequence. | ||
| 82 | |||
| 83 | @code | ||
| 84 | capy::task<> send(capy::ConstBufferSequence auto buffers) | ||
| 85 | { | ||
| 86 | buffer_param bp(buffers); | ||
| 87 | while(true) | ||
| 88 | { | ||
| 89 | auto bufs = bp.data(); | ||
| 90 | if(bufs.empty()) | ||
| 91 | break; | ||
| 92 | auto n = co_await do_something(bufs); | ||
| 93 | bp.consume(n); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | @endcode | ||
| 97 | |||
| 98 | @par Virtual Interface Pattern | ||
| 99 | |||
| 100 | This class enables passing arbitrary buffer sequences through | ||
| 101 | a virtual function boundary. The template function captures | ||
| 102 | the buffer sequence by value and drives the iteration, while | ||
| 103 | the virtual function receives a simple span. Plain CTAD | ||
| 104 | (`buffer_param bp(buffers)`) deduces `BS`'s own buffer type, so a | ||
| 105 | mutable sequence yields `span<mutable_buffer>`. That does not match | ||
| 106 | `write_impl`'s `span<const_buffer>` parameter. Use @ref const_buffer_param | ||
| 107 | to force `const_buffer` storage regardless of what `BS` is: | ||
| 108 | |||
| 109 | @code | ||
| 110 | class base | ||
| 111 | { | ||
| 112 | public: | ||
| 113 | template<capy::ConstBufferSequence BS> | ||
| 114 | capy::task<> write(BS buffers) | ||
| 115 | { | ||
| 116 | const_buffer_param<BS> bp(buffers); | ||
| 117 | while(true) | ||
| 118 | { | ||
| 119 | auto bufs = bp.data(); | ||
| 120 | if(bufs.empty()) | ||
| 121 | break; | ||
| 122 | std::size_t n = 0; | ||
| 123 | co_await write_impl(bufs, n); | ||
| 124 | bp.consume(n); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | protected: | ||
| 129 | virtual capy::task<> write_impl( | ||
| 130 | std::span<capy::const_buffer> buffers, | ||
| 131 | std::size_t& bytes_written) = 0; | ||
| 132 | }; | ||
| 133 | @endcode | ||
| 134 | |||
| 135 | @tparam BS The buffer sequence type. Must satisfy | ||
| 136 | `capy::ConstBufferSequence`. | ||
| 137 | */ | ||
| 138 | template<class BS, bool MakeConst = false> | ||
| 139 | requires capy::ConstBufferSequence<BS> | ||
| 140 | class buffer_param | ||
| 141 | { | ||
| 142 | public: | ||
| 143 | /// Names `const_buffer` when `MakeConst`, else `BS`'s own buffer type. | ||
| 144 | using buffer_type = std::conditional_t< | ||
| 145 | MakeConst, | ||
| 146 | capy::const_buffer, | ||
| 147 | capy::buffer_type<BS>>; | ||
| 148 | |||
| 149 | private: | ||
| 150 | decltype(capy::buffer_sequence_begin(std::declval<BS const&>())) it_; | ||
| 151 | decltype(capy::buffer_sequence_end(std::declval<BS const&>())) end_; | ||
| 152 | union { | ||
| 153 | int dummy_; | ||
| 154 | buffer_type arr_[capy::detail::max_iovec_]; | ||
| 155 | }; | ||
| 156 | std::size_t size_ = 0; | ||
| 157 | std::size_t pos_ = 0; | ||
| 158 | |||
| 159 | void | ||
| 160 | 708x | refill() | |
| 161 | { | ||
| 162 | 708x | pos_ = 0; | |
| 163 | 708x | size_ = 0; | |
| 164 | 2027x | for(; it_ != end_ && size_ < capy::detail::max_iovec_; ++it_) | |
| 165 | { | ||
| 166 | 1319x | buffer_type buf(*it_); | |
| 167 | 1319x | if(buf.size() > 0) | |
| 168 | 1276x | ::new(&arr_[size_++]) buffer_type(buf); | |
| 169 | } | ||
| 170 | 708x | } | |
| 171 | |||
| 172 | public: | ||
| 173 | /** Construct from a buffer sequence. | ||
| 174 | |||
| 175 | @param bs The buffer sequence to wrap. The caller must | ||
| 176 | ensure the buffer sequence remains valid for the | ||
| 177 | lifetime of this object. | ||
| 178 | */ | ||
| 179 | explicit | ||
| 180 | 540x | buffer_param(BS const& bs) | |
| 181 | 540x | : it_(capy::buffer_sequence_begin(bs)) | |
| 182 | 540x | , end_(capy::buffer_sequence_end(bs)) | |
| 183 | 540x | , dummy_(0) | |
| 184 | { | ||
| 185 | 540x | refill(); | |
| 186 | 540x | } | |
| 187 | |||
| 188 | /** Return the current window of buffer descriptors. | ||
| 189 | |||
| 190 | Returns a span of buffer descriptors representing the | ||
| 191 | currently available portion of the buffer sequence. | ||
| 192 | The span contains at most a fixed, implementation-defined | ||
| 193 | number of buffers (currently 16). | ||
| 194 | |||
| 195 | When the current window is exhausted, this function | ||
| 196 | automatically refills from the underlying sequence. | ||
| 197 | |||
| 198 | @return A span of buffer descriptors. Empty span | ||
| 199 | indicates no more data is available. | ||
| 200 | */ | ||
| 201 | std::span<buffer_type> | ||
| 202 | 929x | data() | |
| 203 | { | ||
| 204 | 929x | if(pos_ >= size_) | |
| 205 | 168x | refill(); | |
| 206 | 929x | if(size_ == 0) | |
| 207 | 146x | return {}; | |
| 208 | 783x | return {arr_ + pos_, size_ - pos_}; | |
| 209 | } | ||
| 210 | |||
| 211 | /** Check if more buffers exist beyond the current window. | ||
| 212 | |||
| 213 | Returns `true` if the underlying buffer sequence has | ||
| 214 | additional buffers that have not yet been loaded into | ||
| 215 | the current window. Call after @ref data to determine | ||
| 216 | whether the current window is the last one. | ||
| 217 | |||
| 218 | @return `true` if more buffers remain in the sequence. | ||
| 219 | */ | ||
| 220 | bool | ||
| 221 | 43x | more() const noexcept | |
| 222 | { | ||
| 223 | 43x | return it_ != end_; | |
| 224 | } | ||
| 225 | |||
| 226 | /** Consume bytes from the buffer sequence. | ||
| 227 | |||
| 228 | Advances the current position by `n` bytes, consuming | ||
| 229 | data from the front of the sequence. Partially consumed | ||
| 230 | buffers are adjusted in place. | ||
| 231 | |||
| 232 | @param n Number of bytes to consume. | ||
| 233 | */ | ||
| 234 | void | ||
| 235 | 319x | consume(std::size_t n) | |
| 236 | { | ||
| 237 | 1012x | while(n > 0 && pos_ < size_) | |
| 238 | { | ||
| 239 | 693x | auto avail = arr_[pos_].size(); | |
| 240 | 693x | if(n < avail) | |
| 241 | { | ||
| 242 | 187x | arr_[pos_] += n; | |
| 243 | 187x | n = 0; | |
| 244 | } | ||
| 245 | else | ||
| 246 | { | ||
| 247 | 506x | n -= avail; | |
| 248 | 506x | ++pos_; | |
| 249 | } | ||
| 250 | } | ||
| 251 | 319x | } | |
| 252 | }; | ||
| 253 | |||
| 254 | /** Deduce the sequence type from the constructor argument. | ||
| 255 | |||
| 256 | @tparam BS The buffer sequence type. | ||
| 257 | */ | ||
| 258 | template<class BS> | ||
| 259 | buffer_param(BS const&) -> buffer_param<BS>; | ||
| 260 | |||
| 261 | /// Forces `buffer_param` to store windows as `const_buffer`, whatever `BS` is. | ||
| 262 | template<class BS> | ||
| 263 | using const_buffer_param = buffer_param<BS, true>; | ||
| 264 | |||
| 265 | } // namespace http | ||
| 266 | } // namespace boost | ||
| 267 | |||
| 268 | #endif | ||
| 269 |