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