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