100.00% Lines (65/65) 100.00% Functions (21/21)
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   // Copyright (c) 2026 Steve Gerbino 4   // Copyright (c) 2026 Steve Gerbino
5   // 5   //
6   // Distributed under the Boost Software License, Version 1.0. (See accompanying 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) 7   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8   // 8   //
9   // Official repository: https://github.com/cppalliance/capy 9   // Official repository: https://github.com/cppalliance/capy
10   // 10   //
11   11  
12   #ifndef BOOST_CAPY_BUFFERS_HPP 12   #ifndef BOOST_CAPY_BUFFERS_HPP
13   #define BOOST_CAPY_BUFFERS_HPP 13   #define BOOST_CAPY_BUFFERS_HPP
14   14  
15   #include <boost/capy/detail/config.hpp> 15   #include <boost/capy/detail/config.hpp>
16   #include <concepts> 16   #include <concepts>
17   #include <cstddef> 17   #include <cstddef>
18   #include <iterator> 18   #include <iterator>
19   #include <memory> 19   #include <memory>
20   #include <ranges> 20   #include <ranges>
21   #include <type_traits> 21   #include <type_traits>
22   22  
23   // https://www.boost.org/doc/libs/1_65_0/doc/html/boost_asio/reference/ConstBufferSequence.html 23   // https://www.boost.org/doc/libs/1_65_0/doc/html/boost_asio/reference/ConstBufferSequence.html
24   24  
25   namespace boost { 25   namespace boost {
26   26  
27   namespace asio { 27   namespace asio {
28   class const_buffer; 28   class const_buffer;
29   class mutable_buffer; 29   class mutable_buffer;
30   } // asio 30   } // asio
31   31  
32   namespace capy { 32   namespace capy {
33   33  
34   class const_buffer; 34   class const_buffer;
35   class mutable_buffer; 35   class mutable_buffer;
36   36  
37   /** A reference to a contiguous region of writable memory. 37   /** A reference to a contiguous region of writable memory.
38   38  
39   Represents a pointer and size pair for a modifiable byte range. 39   Represents a pointer and size pair for a modifiable byte range.
40 - Does not own the memory. Satisfies `MutableBufferSequence` (as a 40 + Does not own, allocate, or copy the memory: copying a buffer copies
41 - single-element sequence) and is implicitly convertible to 41 + the pointer and size only. The memory must stay valid until every
42 - `const_buffer`. 42 + operation using the buffer completes. Satisfies
  43 + `MutableBufferSequence` (as a single-element sequence) and is
  44 + implicitly convertible to `const_buffer`.
43   45  
44   @see @ref const_buffer, @ref MutableBufferSequence 46   @see @ref const_buffer, @ref MutableBufferSequence
45   */ 47   */
46   class mutable_buffer 48   class mutable_buffer
47   { 49   {
48   unsigned char* p_ = nullptr; 50   unsigned char* p_ = nullptr;
49   std::size_t n_ = 0; 51   std::size_t n_ = 0;
50   52  
51   public: 53   public:
52   /// Construct an empty buffer. 54   /// Construct an empty buffer.
HITCBC 53   22 mutable_buffer() = default; 55   22 mutable_buffer() = default;
54   56  
55   /** Construct a copy. 57   /** Construct a copy.
56   58  
57   @param other The buffer to copy. 59   @param other The buffer to copy.
58   */ 60   */
59   mutable_buffer( 61   mutable_buffer(
60   mutable_buffer const& other) = default; 62   mutable_buffer const& other) = default;
61   63  
62   /** Assign by copying. 64   /** Assign by copying.
63   65  
64   @param other The buffer to copy. 66   @param other The buffer to copy.
65   67  
66   @return A reference to `*this`. 68   @return A reference to `*this`.
67   */ 69   */
68   mutable_buffer& operator=( 70   mutable_buffer& operator=(
69   mutable_buffer const& other) = default; 71   mutable_buffer const& other) = default;
70   72  
71   /** Construct from a pointer and size. 73   /** Construct from a pointer and size.
72   74  
73   Takes `void*` so a pointer to any object type binds without a 75   Takes `void*` so a pointer to any object type binds without a
74   cast, since the buffer represents a raw, untyped writable 76   cast, since the buffer represents a raw, untyped writable
75   region. Stored internally as `unsigned char*` for byte-wise 77   region. Stored internally as `unsigned char*` for byte-wise
76   pointer arithmetic (see `operator+=`). 78   pointer arithmetic (see `operator+=`).
77   79  
78   @param data A pointer to the first byte of the region. 80   @param data A pointer to the first byte of the region.
79   81  
80   @param size The size of the region, in bytes. 82   @param size The size of the region, in bytes.
81   */ 83   */
HITCBC 82   35265 constexpr mutable_buffer( 84   35265 constexpr mutable_buffer(
83   void* data, std::size_t size) noexcept 85   void* data, std::size_t size) noexcept
HITCBC 84   35265 : p_(static_cast<unsigned char*>(data)) 86   35265 : p_(static_cast<unsigned char*>(data))
HITCBC 85   35265 , n_(size) 87   35265 , n_(size)
86   { 88   {
HITCBC 87   35265 } 89   35265 }
88   90  
89   /** Return a pointer to the memory region. 91   /** Return a pointer to the memory region.
90   92  
91   Returns `void*`, symmetric with the constructor, so the 93   Returns `void*`, symmetric with the constructor, so the
92   caller can reinterpret the raw region as whatever type it needs. 94   caller can reinterpret the raw region as whatever type it needs.
93   95  
94   @return A pointer to the first byte of the region. 96   @return A pointer to the first byte of the region.
95   */ 97   */
HITCBC 96   53984 constexpr void* data() const noexcept 98   53984 constexpr void* data() const noexcept
97   { 99   {
HITCBC 98   53984 return p_; 100   53984 return p_;
99   } 101   }
100   102  
101   /** Return the size in bytes. 103   /** Return the size in bytes.
102   104  
103   @return The size of the region, in bytes. 105   @return The size of the region, in bytes.
104   */ 106   */
HITCBC 105   80483 constexpr std::size_t size() const noexcept 107   80483 constexpr std::size_t size() const noexcept
106   { 108   {
HITCBC 107   80483 return n_; 109   80483 return n_;
108   } 110   }
109   111  
110   /** Advance the buffer start, shrinking the region. 112   /** Advance the buffer start, shrinking the region.
111   113  
112   @param n Bytes to skip. Clamped to `size()`. 114   @param n Bytes to skip. Clamped to `size()`.
113   115  
114   @return A reference to `*this`. 116   @return A reference to `*this`.
115   */ 117   */
116   mutable_buffer& 118   mutable_buffer&
HITCBC 117   17714 operator+=(std::size_t n) noexcept 119   17714 operator+=(std::size_t n) noexcept
118   { 120   {
HITCBC 119   17714 if( n > n_) 121   17714 if( n > n_)
HITCBC 120   1 n = n_; 122   1 n = n_;
HITCBC 121   17714 p_ += n; 123   17714 p_ += n;
HITCBC 122   17714 n_ -= n; 124   17714 n_ -= n;
HITCBC 123   17714 return *this; 125   17714 return *this;
124   } 126   }
125   }; 127   };
126   128  
127   /** A reference to a contiguous region of read-only memory. 129   /** A reference to a contiguous region of read-only memory.
128   130  
129   Represents a pointer and size pair for a non-modifiable byte range. 131   Represents a pointer and size pair for a non-modifiable byte range.
130 - Does not own the memory. Satisfies `ConstBufferSequence` (as a 132 + Does not own, allocate, or copy the memory: copying a buffer copies
131 - single-element sequence). Implicitly constructible from 133 + the pointer and size only. The memory must stay valid until every
132 - `mutable_buffer`. 134 + operation using the buffer completes. Satisfies
  135 + `ConstBufferSequence` (as a single-element sequence). Implicitly
  136 + constructible from `mutable_buffer`.
133   137  
134   @see @ref mutable_buffer, @ref ConstBufferSequence 138   @see @ref mutable_buffer, @ref ConstBufferSequence
135   */ 139   */
136   class const_buffer 140   class const_buffer
137   { 141   {
138   unsigned char const* p_ = nullptr; 142   unsigned char const* p_ = nullptr;
139   std::size_t n_ = 0; 143   std::size_t n_ = 0;
140   144  
141   public: 145   public:
142   /// Construct an empty buffer. 146   /// Construct an empty buffer.
HITCBC 143   58 const_buffer() = default; 147   58 const_buffer() = default;
144   148  
145   /** Construct a copy. 149   /** Construct a copy.
146   150  
147   @param other The buffer to copy. 151   @param other The buffer to copy.
148   */ 152   */
149   const_buffer(const_buffer const& other) = default; 153   const_buffer(const_buffer const& other) = default;
150   154  
151   /** Assign by copying. 155   /** Assign by copying.
152   156  
153   @param other The buffer to copy. 157   @param other The buffer to copy.
154   158  
155   @return A reference to `*this`. 159   @return A reference to `*this`.
156   */ 160   */
157   const_buffer& operator=( 161   const_buffer& operator=(
158   const_buffer const& other) = default; 162   const_buffer const& other) = default;
159   163  
160   /** Construct from a pointer and size. 164   /** Construct from a pointer and size.
161   165  
162   Takes `void const*` so a pointer to any object type binds 166   Takes `void const*` so a pointer to any object type binds
163   without a cast, since the buffer represents a raw, untyped 167   without a cast, since the buffer represents a raw, untyped
164   read-only region. Stored internally as `unsigned char const*` 168   read-only region. Stored internally as `unsigned char const*`
165   for byte-wise pointer arithmetic (see `operator+=`). 169   for byte-wise pointer arithmetic (see `operator+=`).
166   170  
167   @param data A pointer to the first byte of the region. 171   @param data A pointer to the first byte of the region.
168   172  
169   @param size The size of the region, in bytes. 173   @param size The size of the region, in bytes.
170   */ 174   */
HITCBC 171   31999 constexpr const_buffer( 175   31999 constexpr const_buffer(
172   void const* data, std::size_t size) noexcept 176   void const* data, std::size_t size) noexcept
HITCBC 173   31999 : p_(static_cast<unsigned char const*>(data)) 177   31999 : p_(static_cast<unsigned char const*>(data))
HITCBC 174   31999 , n_(size) 178   31999 , n_(size)
175   { 179   {
HITCBC 176   31999 } 180   31999 }
177   181  
178   /** Construct from mutable_buffer. 182   /** Construct from mutable_buffer.
179   183  
180   @param b The writable buffer whose region is referenced. 184   @param b The writable buffer whose region is referenced.
181   */ 185   */
HITCBC 182   7860 constexpr const_buffer( 186   7860 constexpr const_buffer(
183   mutable_buffer const& b) noexcept 187   mutable_buffer const& b) noexcept
HITCBC 184   7860 : p_(static_cast<unsigned char const*>(b.data())) 188   7860 : p_(static_cast<unsigned char const*>(b.data()))
HITCBC 185   7860 , n_(b.size()) 189   7860 , n_(b.size())
186   { 190   {
HITCBC 187   7860 } 191   7860 }
188   192  
189   /** Return a pointer to the memory region. 193   /** Return a pointer to the memory region.
190   194  
191   Returns `void const*`, symmetric with the constructor, so the 195   Returns `void const*`, symmetric with the constructor, so the
192   caller can reinterpret the raw region as whatever type it needs. 196   caller can reinterpret the raw region as whatever type it needs.
193   197  
194   @return A pointer to the first byte of the region. 198   @return A pointer to the first byte of the region.
195   */ 199   */
HITCBC 196   46538 constexpr void const* data() const noexcept 200   46538 constexpr void const* data() const noexcept
197   { 201   {
HITCBC 198   46538 return p_; 202   46538 return p_;
199   } 203   }
200   204  
201   /** Return the size in bytes. 205   /** Return the size in bytes.
202   206  
203   @return The size of the region, in bytes. 207   @return The size of the region, in bytes.
204   */ 208   */
HITCBC 205   78393 constexpr std::size_t size() const noexcept 209   78393 constexpr std::size_t size() const noexcept
206   { 210   {
HITCBC 207   78393 return n_; 211   78393 return n_;
208   } 212   }
209   213  
210   /** Advance the buffer start, shrinking the region. 214   /** Advance the buffer start, shrinking the region.
211   215  
212   @param n Bytes to skip. Clamped to `size()`. 216   @param n Bytes to skip. Clamped to `size()`.
213   217  
214   @return A reference to `*this`. 218   @return A reference to `*this`.
215   */ 219   */
216   const_buffer& 220   const_buffer&
HITCBC 217   17359 operator+=(std::size_t n) noexcept 221   17359 operator+=(std::size_t n) noexcept
218   { 222   {
HITCBC 219   17359 if( n > n_) 223   17359 if( n > n_)
HITCBC 220   1 n = n_; 224   1 n = n_;
HITCBC 221   17359 p_ += n; 225   17359 p_ += n;
HITCBC 222   17359 n_ -= n; 226   17359 n_ -= n;
HITCBC 223   17359 return *this; 227   17359 return *this;
224   } 228   }
225   }; 229   };
226   230  
227   /** Requires a type to convert to `const_buffer`. 231   /** Requires a type to convert to `const_buffer`.
228   232  
229   This is the element requirement of @ref ConstBufferSequence. 233   This is the element requirement of @ref ConstBufferSequence.
230   234  
231   For a `T const` lvalue `x`, both `const_buffer b = x;` and 235   For a `T const` lvalue `x`, both `const_buffer b = x;` and
232   `const_buffer(x)` must be valid, since algorithms convert elements 236   `const_buffer(x)` must be valid, since algorithms convert elements
233   through a const reference and may use either form. A conversion 237   through a const reference and may use either form. A conversion
234   operator must therefore be `const`. 238   operator must therefore be `const`.
235   239  
236   @note Algorithms that read buffer sequences are `noexcept`, so a 240   @note Algorithms that read buffer sequences are `noexcept`, so a
237   conversion that throws terminates the program. Conversion operators 241   conversion that throws terminates the program. Conversion operators
238   should be `noexcept`. 242   should be `noexcept`.
239   243  
240   @see @ref const_buffer, @ref MutableBuffer 244   @see @ref const_buffer, @ref MutableBuffer
241   */ 245   */
242   template<typename T> 246   template<typename T>
243   concept ConstBuffer = 247   concept ConstBuffer =
244   std::convertible_to<T const&, const_buffer>; 248   std::convertible_to<T const&, const_buffer>;
245   249  
246   /** Requires a type to convert to both `mutable_buffer` and `const_buffer`. 250   /** Requires a type to convert to both `mutable_buffer` and `const_buffer`.
247   251  
248   This is the element requirement of @ref MutableBufferSequence. 252   This is the element requirement of @ref MutableBufferSequence.
249   253  
250   A type that converts only to `mutable_buffer` does not qualify. 254   A type that converts only to `mutable_buffer` does not qualify.
251   Reaching `const_buffer` from it takes two user-defined conversions, 255   Reaching `const_buffer` from it takes two user-defined conversions,
252   and an implicit conversion sequence allows one. Such a type must 256   and an implicit conversion sequence allows one. Such a type must
253   also provide a conversion to `const_buffer`. 257   also provide a conversion to `const_buffer`.
254   258  
255   As with @ref ConstBuffer, the conversion is from a `T const` lvalue, 259   As with @ref ConstBuffer, the conversion is from a `T const` lvalue,
256   and conversion operators should be `noexcept`. 260   and conversion operators should be `noexcept`.
257   261  
258   @see @ref mutable_buffer, @ref ConstBuffer 262   @see @ref mutable_buffer, @ref ConstBuffer
259   */ 263   */
260   template<typename T> 264   template<typename T>
261   concept MutableBuffer = 265   concept MutableBuffer =
262   ConstBuffer<T> && std::convertible_to<T const&, mutable_buffer>; 266   ConstBuffer<T> && std::convertible_to<T const&, mutable_buffer>;
263   267  
264   /** Requires a type to be a @ref ConstBuffer, or a range of them. 268   /** Requires a type to be a @ref ConstBuffer, or a range of them.
265   269  
266   A type satisfies `ConstBufferSequence` if it represents one or more 270   A type satisfies `ConstBufferSequence` if it represents one or more
267   contiguous memory regions that can be read. This includes single 271   contiguous memory regions that can be read. This includes single
268   buffers and ranges of buffers. 272   buffers and ranges of buffers.
269   273  
270   @par Syntactic Requirements 274   @par Syntactic Requirements
271   @li @ref ConstBuffer, OR 275   @li @ref ConstBuffer, OR
272   @li A bidirectional range whose value type is a @ref ConstBuffer 276   @li A bidirectional range whose value type is a @ref ConstBuffer
273   277  
274   @see @ref const_buffer, @ref MutableBufferSequence 278   @see @ref const_buffer, @ref MutableBufferSequence
275   */ 279   */
276   // tag::const_buffer_sequence_concept[] 280   // tag::const_buffer_sequence_concept[]
277   template<typename T> 281   template<typename T>
278   concept ConstBufferSequence = 282   concept ConstBufferSequence =
279   ConstBuffer<T> || ( 283   ConstBuffer<T> || (
280   std::ranges::bidirectional_range<T> && 284   std::ranges::bidirectional_range<T> &&
281   ConstBuffer<std::ranges::range_value_t<T>>); 285   ConstBuffer<std::ranges::range_value_t<T>>);
282   // end::const_buffer_sequence_concept[] 286   // end::const_buffer_sequence_concept[]
283   287  
284   /** Requires a type to be a @ref MutableBuffer, or a range of them. 288   /** Requires a type to be a @ref MutableBuffer, or a range of them.
285   289  
286   A type satisfies `MutableBufferSequence` if it represents one or more 290   A type satisfies `MutableBufferSequence` if it represents one or more
287   contiguous memory regions that can be written. This includes single 291   contiguous memory regions that can be written. This includes single
288   buffers and ranges of buffers. 292   buffers and ranges of buffers.
289   293  
290   Every `MutableBufferSequence` is also a @ref ConstBufferSequence. 294   Every `MutableBufferSequence` is also a @ref ConstBufferSequence.
291   295  
292   A @ref ConstBuffer is a single buffer, even if it is also a range, 296   A @ref ConstBuffer is a single buffer, even if it is also a range,
293   so only its own conversion to `mutable_buffer` is considered. 297   so only its own conversion to `mutable_buffer` is considered.
294   298  
295   @par Syntactic Requirements 299   @par Syntactic Requirements
296   @li @ref MutableBuffer, OR 300   @li @ref MutableBuffer, OR
297   @li A bidirectional range, not a @ref ConstBuffer, whose value type 301   @li A bidirectional range, not a @ref ConstBuffer, whose value type
298   is a @ref MutableBuffer 302   is a @ref MutableBuffer
299   303  
300   @see @ref mutable_buffer, @ref ConstBufferSequence 304   @see @ref mutable_buffer, @ref ConstBufferSequence
301   */ 305   */
302   // tag::mutable_buffer_sequence_concept[] 306   // tag::mutable_buffer_sequence_concept[]
303   template<typename T> 307   template<typename T>
304   concept MutableBufferSequence = 308   concept MutableBufferSequence =
305   ConstBufferSequence<T> && (MutableBuffer<T> || ( 309   ConstBufferSequence<T> && (MutableBuffer<T> || (
306   !ConstBuffer<T> && 310   !ConstBuffer<T> &&
307   std::ranges::bidirectional_range<T> && 311   std::ranges::bidirectional_range<T> &&
308   MutableBuffer<std::ranges::range_value_t<T>>)); 312   MutableBuffer<std::ranges::range_value_t<T>>));
309   // end::mutable_buffer_sequence_concept[] 313   // end::mutable_buffer_sequence_concept[]
310   314  
311   /** Return an iterator to the first buffer in a sequence. 315   /** Return an iterator to the first buffer in a sequence.
312   316  
313   @functionobject 317   @functionobject
314   */ 318   */
315   constexpr struct 319   constexpr struct
316   { 320   {
317   /** Return a pointer to a single buffer, forming a one-element range. 321   /** Return a pointer to a single buffer, forming a one-element range.
318   322  
319   @param b A single buffer. 323   @param b A single buffer.
320   324  
321   @return A pointer to `b`. 325   @return A pointer to `b`.
322   */ 326   */
323   template<ConstBuffer ConvertibleToBuffer> 327   template<ConstBuffer ConvertibleToBuffer>
HITCBC 324   6650 auto operator()(ConvertibleToBuffer const& b) const noexcept -> ConvertibleToBuffer const* 328   6650 auto operator()(ConvertibleToBuffer const& b) const noexcept -> ConvertibleToBuffer const*
325   { 329   {
HITCBC 326   6650 return std::addressof(b); 330   6650 return std::addressof(b);
327   } 331   }
328   332  
329   /** Return an iterator to the first buffer of a sequence. 333   /** Return an iterator to the first buffer of a sequence.
330   334  
331   @param bs The buffer sequence. 335   @param bs The buffer sequence.
332   336  
333   @return An iterator to the first buffer of `bs`. 337   @return An iterator to the first buffer of `bs`.
334   */ 338   */
335   template<ConstBufferSequence BS> 339   template<ConstBufferSequence BS>
336   requires (!ConstBuffer<BS>) 340   requires (!ConstBuffer<BS>)
HITCBC 337   33657 auto operator()(BS const& bs) const noexcept 341   33657 auto operator()(BS const& bs) const noexcept
338   { 342   {
HITCBC 339   33657 return std::ranges::begin(bs); 343   33657 return std::ranges::begin(bs);
340   } 344   }
341   345  
342   /** Return an iterator to the first buffer of a sequence. 346   /** Return an iterator to the first buffer of a sequence.
343   347  
344   @param bs The buffer sequence. 348   @param bs The buffer sequence.
345   349  
346   @return An iterator to the first buffer of `bs`. 350   @return An iterator to the first buffer of `bs`.
347   */ 351   */
348   template<ConstBufferSequence BS> 352   template<ConstBufferSequence BS>
349   requires (!ConstBuffer<BS>) 353   requires (!ConstBuffer<BS>)
HITCBC 350   9196 auto operator()(BS& bs) const noexcept 354   9196 auto operator()(BS& bs) const noexcept
351   { 355   {
HITCBC 352   9196 return std::ranges::begin(bs); 356   9196 return std::ranges::begin(bs);
353   } 357   }
354   } buffer_sequence_begin {}; 358   } buffer_sequence_begin {};
355   359  
356   /** Return an iterator past the last buffer in a sequence. 360   /** Return an iterator past the last buffer in a sequence.
357   361  
358   @functionobject 362   @functionobject
359   */ 363   */
360   constexpr struct 364   constexpr struct
361   { 365   {
362   /** Return a pointer one past a single buffer, forming a one-element range. 366   /** Return a pointer one past a single buffer, forming a one-element range.
363   367  
364   @param b A single buffer. 368   @param b A single buffer.
365   369  
366   @return A pointer one past `b`. 370   @return A pointer one past `b`.
367   */ 371   */
368   template<ConstBuffer ConvertibleToBuffer> 372   template<ConstBuffer ConvertibleToBuffer>
HITCBC 369   6653 auto operator()(ConvertibleToBuffer const& b) const noexcept -> ConvertibleToBuffer const* 373   6653 auto operator()(ConvertibleToBuffer const& b) const noexcept -> ConvertibleToBuffer const*
370   { 374   {
HITCBC 371   6653 return std::addressof(b) + 1; 375   6653 return std::addressof(b) + 1;
372   } 376   }
373   377  
374   /** Return an iterator past the last buffer of a sequence. 378   /** Return an iterator past the last buffer of a sequence.
375   379  
376   @param bs The buffer sequence. 380   @param bs The buffer sequence.
377   381  
378   @return An iterator one past the last buffer of `bs`. 382   @return An iterator one past the last buffer of `bs`.
379   */ 383   */
380   template<ConstBufferSequence BS> 384   template<ConstBufferSequence BS>
381   requires (!ConstBuffer<BS>) 385   requires (!ConstBuffer<BS>)
HITCBC 382   33696 auto operator()(BS const& bs) const noexcept 386   33696 auto operator()(BS const& bs) const noexcept
383   { 387   {
HITCBC 384   33696 return std::ranges::end(bs); 388   33696 return std::ranges::end(bs);
385   } 389   }
386   390  
387   /** Return an iterator past the last buffer of a sequence. 391   /** Return an iterator past the last buffer of a sequence.
388   392  
389   @param bs The buffer sequence. 393   @param bs The buffer sequence.
390   394  
391   @return An iterator one past the last buffer of `bs`. 395   @return An iterator one past the last buffer of `bs`.
392   */ 396   */
393   template<ConstBufferSequence BS> 397   template<ConstBufferSequence BS>
394   requires (!ConstBuffer<BS>) 398   requires (!ConstBuffer<BS>)
HITCBC 395   9196 auto operator()(BS& bs) const noexcept 399   9196 auto operator()(BS& bs) const noexcept
396   { 400   {
HITCBC 397   9196 return std::ranges::end(bs); 401   9196 return std::ranges::end(bs);
398   } 402   }
399   } buffer_sequence_end {}; 403   } buffer_sequence_end {};
400   404  
401   /** Return the total byte count across all buffers in a sequence. 405   /** Return the total byte count across all buffers in a sequence.
402   406  
403   @functionobject 407   @functionobject
404   */ 408   */
405   constexpr struct 409   constexpr struct
406   { 410   {
407   // GCC 13 falsely flags reads of arr_[i].n_ in detail::buffer_array 411   // GCC 13 falsely flags reads of arr_[i].n_ in detail::buffer_array
408   // when iterating here. The class uses union storage with placement 412   // when iterating here. The class uses union storage with placement
409   // new for slots 0..n_-1, so reads inside this bounded loop are 413   // new for slots 0..n_-1, so reads inside this bounded loop are
410   // well-defined, but the optimizer can't prove the loop bound and 414   // well-defined, but the optimizer can't prove the loop bound and
411   // warns. The runtime cost of value-initializing all N slots is 415   // warns. The runtime cost of value-initializing all N slots is
412   // non-trivial for non-trivial value types, so we suppress instead. 416   // non-trivial for non-trivial value types, so we suppress instead.
413   #if defined(__GNUC__) && !defined(__clang__) 417   #if defined(__GNUC__) && !defined(__clang__)
414   #pragma GCC diagnostic push 418   #pragma GCC diagnostic push
415   #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" 419   #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
416   #endif 420   #endif
417   /** Return the total byte count across all buffers in a sequence. 421   /** Return the total byte count across all buffers in a sequence.
418   422  
419   Sums the `size()` of each buffer in the sequence. This differs 423   Sums the `size()` of each buffer in the sequence. This differs
420   from `buffers_count` which counts the number of buffer elements. 424   from `buffers_count` which counts the number of buffer elements.
421   425  
422   @param bs The buffer sequence. 426   @param bs The buffer sequence.
423   427  
424   @return The sum of the sizes of all buffers in `bs`. 428   @return The sum of the sizes of all buffers in `bs`.
425   429  
426   @par Example 430   @par Example
427   @par !example example 431   @par !example example
428   432  
429   */ 433   */
430   template<ConstBufferSequence CB> 434   template<ConstBufferSequence CB>
HITCBC 431   6290 constexpr std::size_t operator()( 435   6290 constexpr std::size_t operator()(
432   CB const& bs) const noexcept 436   CB const& bs) const noexcept
433   { 437   {
HITCBC 434   6290 std::size_t n = 0; 438   6290 std::size_t n = 0;
HITCBC 435   6290 auto const e = capy::buffer_sequence_end(bs); 439   6290 auto const e = capy::buffer_sequence_end(bs);
HITCBC 436   14579 for(auto it = capy::buffer_sequence_begin(bs); it != e; ++it) 440   14579 for(auto it = capy::buffer_sequence_begin(bs); it != e; ++it)
HITCBC 437   8289 n += const_buffer(*it).size(); 441   8289 n += const_buffer(*it).size();
HITCBC 438   6290 return n; 442   6290 return n;
439   } 443   }
440   #if defined(__GNUC__) && !defined(__clang__) 444   #if defined(__GNUC__) && !defined(__clang__)
441   #pragma GCC diagnostic pop 445   #pragma GCC diagnostic pop
442   #endif 446   #endif
443   } buffer_size {}; 447   } buffer_size {};
444   448  
445   /** Check if a buffer sequence contains no data. 449   /** Check if a buffer sequence contains no data.
446   450  
447   @functionobject 451   @functionobject
448   */ 452   */
449   constexpr struct 453   constexpr struct
450   { 454   {
451   // See note on buffer_size above — same union-storage false positive. 455   // See note on buffer_size above — same union-storage false positive.
452   #if defined(__GNUC__) && !defined(__clang__) 456   #if defined(__GNUC__) && !defined(__clang__)
453   #pragma GCC diagnostic push 457   #pragma GCC diagnostic push
454   #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" 458   #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
455   #endif 459   #endif
456   /** Check if a buffer sequence contains no data. 460   /** Check if a buffer sequence contains no data.
457   461  
458   @param bs The buffer sequence. 462   @param bs The buffer sequence.
459   463  
460   @return `true` if all buffers have size zero or the sequence 464   @return `true` if all buffers have size zero or the sequence
461   is empty. 465   is empty.
462   */ 466   */
463   template<ConstBufferSequence CB> 467   template<ConstBufferSequence CB>
HITCBC 464   1567 constexpr bool operator()( 468   1567 constexpr bool operator()(
465   CB const& bs) const noexcept 469   CB const& bs) const noexcept
466   { 470   {
HITCBC 467   1567 auto it = buffer_sequence_begin(bs); 471   1567 auto it = buffer_sequence_begin(bs);
HITCBC 468   1567 auto const end_ = buffer_sequence_end(bs); 472   1567 auto const end_ = buffer_sequence_end(bs);
HITCBC 469   1615 while(it != end_) 473   1615 while(it != end_)
470   { 474   {
HITCBC 471   1579 const_buffer b(*it++); 475   1579 const_buffer b(*it++);
HITCBC 472   1579 if(b.size() != 0) 476   1579 if(b.size() != 0)
HITCBC 473   1531 return false; 477   1531 return false;
474   } 478   }
HITCBC 475   36 return true; 479   36 return true;
476   } 480   }
477   #if defined(__GNUC__) && !defined(__clang__) 481   #if defined(__GNUC__) && !defined(__clang__)
478   #pragma GCC diagnostic pop 482   #pragma GCC diagnostic pop
479   #endif 483   #endif
480   } buffers_empty {}; 484   } buffers_empty {};
481   485  
482   namespace detail { 486   namespace detail {
483   487  
484   template<class It> 488   template<class It>
485   auto 489   auto
HITCBC 486   8 length_impl(It first, It last, int) 490   8 length_impl(It first, It last, int)
487   -> decltype(static_cast<std::size_t>(last - first)) 491   -> decltype(static_cast<std::size_t>(last - first))
488   { 492   {
HITCBC 489   8 return static_cast<std::size_t>(last - first); 493   8 return static_cast<std::size_t>(last - first);
490   } 494   }
491   495  
492   template<class It> 496   template<class It>
493   std::size_t 497   std::size_t
494   length_impl(It first, It last, long) 498   length_impl(It first, It last, long)
495   { 499   {
496   std::size_t n = 0; 500   std::size_t n = 0;
497   while(first != last) 501   while(first != last)
498   { 502   {
499   ++first; 503   ++first;
500   ++n; 504   ++n;
501   } 505   }
502   return n; 506   return n;
503   } 507   }
504   508  
505   } // detail 509   } // detail
506   510  
507   /** Return the number of buffer elements in a sequence. 511   /** Return the number of buffer elements in a sequence.
508   512  
509   Counts the number of individual buffer objects, not bytes. 513   Counts the number of individual buffer objects, not bytes.
510   For a single buffer, returns 1. For a range, returns the 514   For a single buffer, returns 1. For a range, returns the
511   distance from `buffer_sequence_begin` to `buffer_sequence_end`. 515   distance from `buffer_sequence_begin` to `buffer_sequence_end`.
512   516  
513   @param bs The buffer sequence. 517   @param bs The buffer sequence.
514   518  
515   @return The number of buffers in `bs`. 519   @return The number of buffers in `bs`.
516   520  
517   @see @ref buffer_size 521   @see @ref buffer_size
518   */ 522   */
519   template<ConstBufferSequence CB> 523   template<ConstBufferSequence CB>
520   std::size_t 524   std::size_t
HITCBC 521   8 buffers_count(CB const& bs) 525   8 buffers_count(CB const& bs)
522   { 526   {
HITCBC 523   8 return detail::length_impl( 527   8 return detail::length_impl(
HITCBC 524   8 buffer_sequence_begin(bs), buffer_sequence_end(bs), 0); 528   8 buffer_sequence_begin(bs), buffer_sequence_end(bs), 0);
525   } 529   }
526   530  
527   /// Names `mutable_buffer` for a mutable sequence, `const_buffer` otherwise. 531   /// Names `mutable_buffer` for a mutable sequence, `const_buffer` otherwise.
528   template<typename BS> 532   template<typename BS>
529   using buffer_type = std::conditional_t< 533   using buffer_type = std::conditional_t<
530   MutableBufferSequence<BS>, 534   MutableBufferSequence<BS>,
531   mutable_buffer, const_buffer>; 535   mutable_buffer, const_buffer>;
532   536  
533   } // capy 537   } // capy
534   } // boost 538   } // boost
535   539  
536   #endif 540   #endif