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