94.74% Lines (18/19) 83.33% Functions (5/6)
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/buffers/buffer_slice.hpp> 16   #include <boost/capy/buffers/buffer_slice.hpp>
17   #include <coroutine> 17   #include <coroutine>
18   #include <boost/capy/ex/io_env.hpp> 18   #include <boost/capy/ex/io_env.hpp>
19   19  
20   #include <algorithm> 20   #include <algorithm>
21   #include <cstddef> 21   #include <cstddef>
22   #include <type_traits> 22   #include <type_traits>
23   #include <utility> 23   #include <utility>
24   24  
25   namespace boost { 25   namespace boost {
26   namespace capy { 26   namespace capy {
27   namespace test { 27   namespace test {
28   28  
29   /** Iterates split points of a buffer sequence into two adjacent halves. 29   /** Iterates split points of a buffer sequence into two adjacent halves.
30   30  
31   This class iterates through all possible ways to split a buffer 31   This class iterates through all possible ways to split a buffer
32   sequence into two parts (b1, b2) where concatenating them yields 32   sequence into two parts (b1, b2) where concatenating them yields
33   the original sequence. It uses an async-generator-like pattern 33   the original sequence. It uses an async-generator-like pattern
34   that allows `co_await` between iterations. 34   that allows `co_await` between iterations.
35   35  
36   The split type automatically preserves mutability: passing a 36   The split type automatically preserves mutability: passing a
37   `MutableBufferSequence` yields halves that model 37   `MutableBufferSequence` yields halves that model
38   @ref MutableBufferSequence, while passing a `ConstBufferSequence` 38   @ref MutableBufferSequence, while passing a `ConstBufferSequence`
39   yields halves that model @ref ConstBufferSequence. Each half is 39   yields halves that model @ref ConstBufferSequence. Each half is
40   the buffer-sequence view exposed by a @ref buffer_slice over the 40   the buffer-sequence view exposed by a @ref buffer_slice over the
41   corresponding byte range, and can be passed directly to 41   corresponding byte range, and can be passed directly to
42   `read_some`, `write_some`, `buffer_size`, etc. 42   `read_some`, `write_some`, `buffer_size`, etc.
43   43  
44   @par Thread Safety 44   @par Thread Safety
45   Not thread-safe. 45   Not thread-safe.
46   46  
47   @par Example 47   @par Example
48 - @code 48 + @par !example example_1
49 - // Test all split points of a buffer  
50 - std::string data = "hello world";  
51 - auto cb = make_buffer( data );  
52 - fuse f;  
53 - auto r = f.inert( [&]( fuse& ) -> task<> {  
54 - bufgrind bg( cb );  
55 - while( bg ) {  
56 - auto [b1, b2] = co_await bg.next();  
57 - // b1 contains first N bytes (as a buffer sequence)  
58 - // b2 contains remaining bytes (as a buffer sequence)  
59 - // concatenating b1 + b2 equals original  
60 - co_await some_async_operation( b1, b2 );  
61 - }  
62 - } );  
63 - @endcode  
64   49  
65   50  
66   @par Mutable Buffer Example 51   @par Mutable Buffer Example
67 - @code 52 + @par !example example_2
68 - // Mutable buffers preserve mutability  
69 - char data[100];  
70 - mutable_buffer mb( data, sizeof( data ) );  
71 - bufgrind bg( mb );  
72 - while( bg ) {  
73 - auto [b1, b2] = co_await bg.next();  
74 - // b1, b2 yield mutable_buffer when iterated  
75 - }  
76 - @endcode  
77   53  
78   54  
79   @par Step Size Example 55   @par Step Size Example
80 - @code 56 + @par !example example_3
81 - // Skip by 10 bytes for faster iteration 57 +
82 - bufgrind bg( cb, 10 );  
83 - while( bg ) {  
84 - auto [b1, b2] = co_await bg.next();  
85 - // Visits positions 0, 10, 20, ..., and always size  
86 - }  
87 - @endcode  
88   58  
89   @see buffer_slice 59   @see buffer_slice
90   */ 60   */
91   template<ConstBufferSequence BS> 61   template<ConstBufferSequence BS>
92   class bufgrind 62   class bufgrind
93   { 63   {
94   BS const& bs_; 64   BS const& bs_;
95   std::size_t size_; 65   std::size_t size_;
96   std::size_t step_; 66   std::size_t step_;
97   std::size_t pos_ = 0; 67   std::size_t pos_ = 0;
98   68  
99   public: 69   public:
100   /// Names the buffer-sequence type `buffer_slice` yields for each half. 70   /// Names the buffer-sequence type `buffer_slice` yields for each half.
101   using slice_type = std::decay_t< 71   using slice_type = std::decay_t<
102   decltype(buffer_slice(std::declval<BS const&>()))>; 72   decltype(buffer_slice(std::declval<BS const&>()))>;
103   73  
104   /// Pairs the two `slice_type` halves that @ref next yields together. 74   /// Pairs the two `slice_type` halves that @ref next yields together.
105   using split_type = std::pair<slice_type, slice_type>; 75   using split_type = std::pair<slice_type, slice_type>;
106   76  
107   /** Construct a buffer grinder. 77   /** Construct a buffer grinder.
108   78  
109   @param bs The buffer sequence to iterate over. 79   @param bs The buffer sequence to iterate over.
110   80  
111   @param step The number of bytes to advance on each call to 81   @param step The number of bytes to advance on each call to
112   @ref next. A value of 0 is treated as 1. The final split 82   @ref next. A value of 0 is treated as 1. The final split
113   at `buffer_size( bs )` is always included regardless of 83   at `buffer_size( bs )` is always included regardless of
114   step alignment. 84   step alignment.
115   */ 85   */
116   explicit 86   explicit
HITCBC 117   39 bufgrind( 87   39 bufgrind(
118   BS const& bs, 88   BS const& bs,
119   std::size_t step = 1) noexcept 89   std::size_t step = 1) noexcept
HITCBC 120   39 : bs_(bs) 90   39 : bs_(bs)
HITCBC 121   39 , size_(buffer_size(bs)) 91   39 , size_(buffer_size(bs))
HITCBC 122   39 , step_(step > 0 ? step : 1) 92   39 , step_(step > 0 ? step : 1)
123   { 93   {
HITCBC 124   39 } 94   39 }
125   95  
126   /** Check if more split points remain. 96   /** Check if more split points remain.
127   97  
128   @return `true` if @ref next can be called, `false` otherwise. 98   @return `true` if @ref next can be called, `false` otherwise.
129   */ 99   */
HITCBC 130   237 explicit operator bool() const noexcept 100   237 explicit operator bool() const noexcept
131   { 101   {
HITCBC 132   237 return pos_ <= size_; 102   237 return pos_ <= size_;
133   } 103   }
134   104  
135   /** Computes the current split synchronously, so awaiting it never suspends the caller. 105   /** Computes the current split synchronously, so awaiting it never suspends the caller.
136   */ 106   */
137   struct next_awaitable 107   struct next_awaitable
138   { 108   {
139   /// The grinder that produced this awaitable. 109   /// The grinder that produced this awaitable.
140   bufgrind* self_; 110   bufgrind* self_;
141   111  
142   /** Report whether the awaitable is ready. 112   /** Report whether the awaitable is ready.
143   113  
144   @return `true` always; the split is available without suspending. 114   @return `true` always; the split is available without suspending.
145   */ 115   */
HITCBC 146   198 bool await_ready() const noexcept { return true; } 116   198 bool await_ready() const noexcept { return true; }
147   117  
148   /** Resume the caller inline without suspending. 118   /** Resume the caller inline without suspending.
149   119  
150   @param h The awaiting coroutine handle. 120   @param h The awaiting coroutine handle.
151   121  
152   @return @p h, so the caller resumes immediately. 122   @return @p h, so the caller resumes immediately.
153   */ 123   */
MISUBC 154   std::coroutine_handle<> await_suspend(std::coroutine_handle<> h, io_env const*) const noexcept { return h; } 124   std::coroutine_handle<> await_suspend(std::coroutine_handle<> h, io_env const*) const noexcept { return h; }
155   125  
156   /** Return the current split and advance to the next. 126   /** Return the current split and advance to the next.
157   127  
158   @return The `(b1, b2)` split at the current position. 128   @return The `(b1, b2)` split at the current position.
159   */ 129   */
160   split_type 130   split_type
HITCBC 161   198 await_resume() 131   198 await_resume()
162   { 132   {
HITCBC 163   198 split_type result{ 133   198 split_type result{
HITCBC 164   198 buffer_slice(self_->bs_, 0, self_->pos_), 134   198 buffer_slice(self_->bs_, 0, self_->pos_),
HITCBC 165   198 buffer_slice(self_->bs_, self_->pos_) 135   198 buffer_slice(self_->bs_, self_->pos_)
166   }; 136   };
HITCBC 167   198 if(self_->pos_ < self_->size_) 137   198 if(self_->pos_ < self_->size_)
HITCBC 168   161 self_->pos_ = (std::min)(self_->pos_ + self_->step_, self_->size_); 138   161 self_->pos_ = (std::min)(self_->pos_ + self_->step_, self_->size_);
169   else 139   else
HITCBC 170   37 ++self_->pos_; 140   37 ++self_->pos_;
HITCBC 171   198 return result; 141   198 return result;
172   } 142   }
173   }; 143   };
174   144  
175   /** Return the next split point. 145   /** Return the next split point.
176   146  
177   Returns an awaitable that yields the current (b1, b2) pair 147   Returns an awaitable that yields the current (b1, b2) pair
178   and advances to the next split point. 148   and advances to the next split point.
179   149  
180   @par Preconditions 150   @par Preconditions
181   `static_cast<bool>( *this )` is `true`. 151   `static_cast<bool>( *this )` is `true`.
182   152  
183   @return An awaitable that await-returns `split_type`. 153   @return An awaitable that await-returns `split_type`.
184   */ 154   */
185   next_awaitable 155   next_awaitable
HITCBC 186   198 next() noexcept 156   198 next() noexcept
187   { 157   {
HITCBC 188   198 return {this}; 158   198 return {this};
189   } 159   }
190   }; 160   };
191   161  
192   } // test 162   } // test
193   } // capy 163   } // capy
194   } // boost 164   } // boost
195   165  
196   #endif 166   #endif