96.15% Lines (25/26)
85.71% Functions (6/7)
| 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 | // Copyright (c) 2026 Michael Vandeberg | 3 | // Copyright (c) 2026 Michael Vandeberg | |||||
| 4 | // | 4 | // | |||||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||||
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||||
| 7 | // | 7 | // | |||||
| 8 | // Official repository: https://github.com/cppalliance/capy | 8 | // Official repository: https://github.com/cppalliance/capy | |||||
| 9 | // | 9 | // | |||||
| 10 | 10 | |||||||
| 11 | #ifndef BOOST_CAPY_TEST_BUFGRIND_HPP | 11 | #ifndef BOOST_CAPY_TEST_BUFGRIND_HPP | |||||
| 12 | #define BOOST_CAPY_TEST_BUFGRIND_HPP | 12 | #define BOOST_CAPY_TEST_BUFGRIND_HPP | |||||
| 13 | 13 | |||||||
| 14 | #include <boost/capy/detail/config.hpp> | 14 | #include <boost/capy/detail/config.hpp> | |||||
| 15 | #include <boost/capy/buffers.hpp> | 15 | #include <boost/capy/buffers.hpp> | |||||
| 16 | #include <boost/capy/detail/slice_of.hpp> | 16 | #include <boost/capy/detail/slice_of.hpp> | |||||
| 17 | #include <coroutine> | 17 | #include <coroutine> | |||||
| 18 | #include <boost/capy/ex/await_context.hpp> | 18 | #include <boost/capy/ex/await_context.hpp> | |||||
| 19 | 19 | |||||||
| 20 | #include <algorithm> | 20 | #include <algorithm> | |||||
| 21 | #include <cstddef> | 21 | #include <cstddef> | |||||
| 22 | #include <limits> | 22 | #include <limits> | |||||
| 23 | #include <type_traits> | 23 | #include <type_traits> | |||||
| 24 | #include <utility> | 24 | #include <utility> | |||||
| 25 | 25 | |||||||
| 26 | namespace boost { | 26 | namespace boost { | |||||
| 27 | namespace capy { | 27 | namespace capy { | |||||
| 28 | namespace test { | 28 | namespace test { | |||||
| 29 | 29 | |||||||
| 30 | /** Iterates split points of a buffer sequence into two adjacent halves. | 30 | /** Iterates split points of a buffer sequence into two adjacent halves. | |||||
| 31 | 31 | |||||||
| 32 | This class iterates through all possible ways to split a buffer | 32 | This class iterates through all possible ways to split a buffer | |||||
| 33 | sequence into two parts (b1, b2) where concatenating them yields | 33 | sequence into two parts (b1, b2) where concatenating them yields | |||||
| 34 | the original sequence. It uses an async-generator-like pattern | 34 | the original sequence. It uses an async-generator-like pattern | |||||
| 35 | that allows `co_await` between iterations. | 35 | that allows `co_await` between iterations. | |||||
| 36 | 36 | |||||||
| 37 | The split type automatically preserves mutability: passing a | 37 | The split type automatically preserves mutability: passing a | |||||
| 38 | `MutableBufferSequence` yields halves that model | 38 | `MutableBufferSequence` yields halves that model | |||||
| 39 | @ref MutableBufferSequence, while passing a `ConstBufferSequence` | 39 | @ref MutableBufferSequence, while passing a `ConstBufferSequence` | |||||
| 40 | yields halves that model @ref ConstBufferSequence. A single buffer | 40 | yields halves that model @ref ConstBufferSequence. A single buffer | |||||
| 41 | splits into two `mutable_buffer` or `const_buffer` values; any other | 41 | splits into two `mutable_buffer` or `const_buffer` values; any other | |||||
| 42 | sequence splits into two borrowed views. Either way each half can be | 42 | sequence splits into two borrowed views. Either way each half can be | |||||
| 43 | passed directly to `read_some`, `write_some`, `buffer_size`, etc. | 43 | passed directly to `read_some`, `write_some`, `buffer_size`, etc. | |||||
| 44 | 44 | |||||||
| 45 | + | The grinder holds a reference to the buffer sequence. The sequence | ||||||
| 46 | + | must outlive the grinder. For a sequence that is not a single | ||||||
| 47 | + | buffer, each half borrows the sequence, so the halves must not | ||||||
| 48 | + | outlive it either. | ||||||
| 49 | + | |||||||
| 45 | @par Thread Safety | 50 | @par Thread Safety | |||||
| 46 | Not thread-safe. | 51 | Not thread-safe. | |||||
| 47 | 52 | |||||||
| 48 | @par Example | 53 | @par Example | |||||
| 49 | @par !example example_1 | 54 | @par !example example_1 | |||||
| 50 | 55 | |||||||
| 51 | 56 | |||||||
| 52 | @par Mutable Buffer Example | 57 | @par Mutable Buffer Example | |||||
| 53 | @par !example example_2 | 58 | @par !example example_2 | |||||
| 54 | 59 | |||||||
| 55 | 60 | |||||||
| 56 | @par Step Size Example | 61 | @par Step Size Example | |||||
| 57 | @par !example example_3 | 62 | @par !example example_3 | |||||
| 58 | 63 | |||||||
| 59 | */ | 64 | */ | |||||
| 60 | template<ConstBufferSequence BS> | 65 | template<ConstBufferSequence BS> | |||||
| 61 | class bufgrind | 66 | class bufgrind | |||||
| 62 | { | 67 | { | |||||
| 63 | BS const& bs_; | 68 | BS const& bs_; | |||||
| 64 | std::size_t size_; | 69 | std::size_t size_; | |||||
| 65 | std::size_t step_; | 70 | std::size_t step_; | |||||
| 66 | std::size_t pos_ = 0; | 71 | std::size_t pos_ = 0; | |||||
| 67 | 72 | |||||||
| 68 | public: | 73 | public: | |||||
| 69 | /// Names the buffer-sequence type of each half. | 74 | /// Names the buffer-sequence type of each half. | |||||
| 70 | using slice_type = std::conditional_t< | 75 | using slice_type = std::conditional_t< | |||||
| 71 | ConstBuffer<BS>, | 76 | ConstBuffer<BS>, | |||||
| 72 | buffer_type<BS>, | 77 | buffer_type<BS>, | |||||
| 73 | detail::slice_of<BS>>; | 78 | detail::slice_of<BS>>; | |||||
| 74 | 79 | |||||||
| 75 | /// Pairs the two `slice_type` halves that @ref next yields together. | 80 | /// Pairs the two `slice_type` halves that @ref next yields together. | |||||
| 76 | using split_type = std::pair<slice_type, slice_type>; | 81 | using split_type = std::pair<slice_type, slice_type>; | |||||
| 77 | 82 | |||||||
| 78 | private: | 83 | private: | |||||
| 79 | static slice_type | 84 | static slice_type | |||||
| HITCBC | 80 | 396 | slice( | 85 | 396 | slice( | ||
| 81 | BS const& bs, | 86 | BS const& bs, | |||||
| 82 | std::size_t offset, | 87 | std::size_t offset, | |||||
| 83 | std::size_t length = | 88 | std::size_t length = | |||||
| 84 | (std::numeric_limits<std::size_t>::max)()) noexcept | 89 | (std::numeric_limits<std::size_t>::max)()) noexcept | |||||
| 85 | { | 90 | { | |||||
| 86 | if constexpr (ConstBuffer<BS>) | 91 | if constexpr (ConstBuffer<BS>) | |||||
| 87 | { | 92 | { | |||||
| HITCBC | 88 | 382 | slice_type b = bs; | 93 | 382 | slice_type b = bs; | ||
| HITCBC | 89 | 382 | b += offset; | 94 | 382 | b += offset; | ||
| HITCBC | 90 | 382 | if(length < b.size()) | 95 | 382 | if(length < b.size()) | ||
| HITCBC | 91 | 155 | b = slice_type(b.data(), length); | 96 | 155 | b = slice_type(b.data(), length); | ||
| HITCBC | 92 | 382 | return b; | 97 | 382 | return b; | ||
| 93 | } | 98 | } | |||||
| 94 | else | 99 | else | |||||
| 95 | { | 100 | { | |||||
| HITCBC | 96 | 14 | return slice_type(bs, offset, length); | 101 | 14 | return slice_type(bs, offset, length); | ||
| 97 | } | 102 | } | |||||
| 98 | } | 103 | } | |||||
| 99 | 104 | |||||||
| 100 | public: | 105 | public: | |||||
| 101 | 106 | |||||||
| 102 | /** Construct a buffer grinder. | 107 | /** Construct a buffer grinder. | |||||
| 103 | 108 | |||||||
| 104 | - | @param bs The buffer sequence to iterate over. | 109 | + | @param bs The buffer sequence to iterate over. The grinder | |||
| 110 | + | keeps a reference to it, so passing a temporary leaves the | ||||||
| 111 | + | grinder dangling. | ||||||
| 105 | 112 | |||||||
| 106 | @param step The number of bytes to advance on each call to | 113 | @param step The number of bytes to advance on each call to | |||||
| 107 | @ref next. A value of 0 is treated as 1. The final split | 114 | @ref next. A value of 0 is treated as 1. The final split | |||||
| 108 | at `buffer_size( bs )` is always included regardless of | 115 | at `buffer_size( bs )` is always included regardless of | |||||
| 109 | step alignment. | 116 | step alignment. | |||||
| 110 | */ | 117 | */ | |||||
| 111 | explicit | 118 | explicit | |||||
| HITCBC | 112 | 39 | bufgrind( | 119 | 39 | bufgrind( | ||
| 113 | BS const& bs, | 120 | BS const& bs, | |||||
| 114 | std::size_t step = 1) noexcept | 121 | std::size_t step = 1) noexcept | |||||
| HITCBC | 115 | 39 | : bs_(bs) | 122 | 39 | : bs_(bs) | ||
| HITCBC | 116 | 39 | , size_(buffer_size(bs)) | 123 | 39 | , size_(buffer_size(bs)) | ||
| HITCBC | 117 | 39 | , step_(step > 0 ? step : 1) | 124 | 39 | , step_(step > 0 ? step : 1) | ||
| 118 | { | 125 | { | |||||
| HITCBC | 119 | 39 | } | 126 | 39 | } | ||
| 120 | 127 | |||||||
| 121 | /** Check if more split points remain. | 128 | /** Check if more split points remain. | |||||
| 122 | 129 | |||||||
| 123 | @return `true` if @ref next can be called, `false` otherwise. | 130 | @return `true` if @ref next can be called, `false` otherwise. | |||||
| 124 | */ | 131 | */ | |||||
| HITCBC | 125 | 237 | explicit operator bool() const noexcept | 132 | 237 | explicit operator bool() const noexcept | ||
| 126 | { | 133 | { | |||||
| HITCBC | 127 | 237 | return pos_ <= size_; | 134 | 237 | return pos_ <= size_; | ||
| 128 | } | 135 | } | |||||
| 129 | 136 | |||||||
| 130 | /** Computes the current split synchronously, so awaiting it never suspends the caller. | 137 | /** Computes the current split synchronously, so awaiting it never suspends the caller. | |||||
| 131 | */ | 138 | */ | |||||
| 132 | struct next_awaitable | 139 | struct next_awaitable | |||||
| 133 | { | 140 | { | |||||
| 134 | /// The grinder that produced this awaitable. | 141 | /// The grinder that produced this awaitable. | |||||
| 135 | bufgrind* self_; | 142 | bufgrind* self_; | |||||
| 136 | 143 | |||||||
| 137 | - | /** Report whether the awaitable is ready. | 144 | + | /** Check whether the awaitable is ready. | |||
| 138 | 145 | |||||||
| 139 | @return `true` always; the split is available without suspending. | 146 | @return `true` always; the split is available without suspending. | |||||
| 140 | */ | 147 | */ | |||||
| HITCBC | 141 | 198 | bool await_ready() const noexcept { return true; } | 148 | 198 | bool await_ready() const noexcept { return true; } | ||
| 142 | 149 | |||||||
| 143 | /** Resume the caller inline without suspending. | 150 | /** Resume the caller inline without suspending. | |||||
| 144 | 151 | |||||||
| 145 | @param h The awaiting coroutine handle. | 152 | @param h The awaiting coroutine handle. | |||||
| 146 | 153 | |||||||
| 147 | @return @p h, so the caller resumes immediately. | 154 | @return @p h, so the caller resumes immediately. | |||||
| 148 | */ | 155 | */ | |||||
| MISUBC | 149 | ✗ | std::coroutine_handle<> await_suspend(std::coroutine_handle<> h, await_context const*) const noexcept { return h; } | 156 | ✗ | std::coroutine_handle<> await_suspend(std::coroutine_handle<> h, await_context const*) const noexcept { return h; } | ||
| 150 | 157 | |||||||
| 151 | /** Return the current split and advance to the next. | 158 | /** Return the current split and advance to the next. | |||||
| 152 | 159 | |||||||
| 153 | @return The `(b1, b2)` split at the current position. | 160 | @return The `(b1, b2)` split at the current position. | |||||
| 154 | */ | 161 | */ | |||||
| 155 | split_type | 162 | split_type | |||||
| HITCBC | 156 | 198 | await_resume() | 163 | 198 | await_resume() | ||
| 157 | { | 164 | { | |||||
| HITCBC | 158 | 198 | split_type result{ | 165 | 198 | split_type result{ | ||
| HITCBC | 159 | 198 | slice(self_->bs_, 0, self_->pos_), | 166 | 198 | slice(self_->bs_, 0, self_->pos_), | ||
| HITCBC | 160 | 198 | slice(self_->bs_, self_->pos_) | 167 | 198 | slice(self_->bs_, self_->pos_) | ||
| 161 | }; | 168 | }; | |||||
| HITCBC | 162 | 198 | if(self_->pos_ < self_->size_) | 169 | 198 | if(self_->pos_ < self_->size_) | ||
| HITCBC | 163 | 161 | self_->pos_ = (std::min)(self_->pos_ + self_->step_, self_->size_); | 170 | 161 | self_->pos_ = (std::min)(self_->pos_ + self_->step_, self_->size_); | ||
| 164 | else | 171 | else | |||||
| HITCBC | 165 | 37 | ++self_->pos_; | 172 | 37 | ++self_->pos_; | ||
| HITCBC | 166 | 198 | return result; | 173 | 198 | return result; | ||
| 167 | } | 174 | } | |||||
| 168 | }; | 175 | }; | |||||
| 169 | 176 | |||||||
| 170 | /** Return the next split point. | 177 | /** Return the next split point. | |||||
| 171 | 178 | |||||||
| 172 | Returns an awaitable that yields the current (b1, b2) pair | 179 | Returns an awaitable that yields the current (b1, b2) pair | |||||
| 173 | and advances to the next split point. | 180 | and advances to the next split point. | |||||
| 174 | 181 | |||||||
| 175 | - | @par Preconditions | 182 | + | @pre `static_cast<bool>( *this )` is `true`. | |||
| 176 | - | `static_cast<bool>( *this )` is `true`. | ||||||
| 177 | 183 | |||||||
| 178 | @return An awaitable that await-returns `split_type`. | 184 | @return An awaitable that await-returns `split_type`. | |||||
| 179 | */ | 185 | */ | |||||
| 180 | next_awaitable | 186 | next_awaitable | |||||
| HITCBC | 181 | 198 | next() noexcept | 187 | 198 | next() noexcept | ||
| 182 | { | 188 | { | |||||
| HITCBC | 183 | 198 | return {this}; | 189 | 198 | return {this}; | ||
| 184 | } | 190 | } | |||||
| 185 | }; | 191 | }; | |||||
| 186 | 192 | |||||||
| 187 | } // test | 193 | } // test | |||||
| 188 | } // capy | 194 | } // capy | |||||
| 189 | } // boost | 195 | } // boost | |||||
| 190 | 196 | |||||||
| 191 | #endif | 197 | #endif | |||||