0.00% Lines (0/0)
0.00% Functions (0/0)
| TLA | Baseline | Branch | ||||||
|---|---|---|---|---|---|---|---|---|
| Line | Hits | Code | Line | Hits | Code | |||
| 1 | - | // | ||||||
| 2 | - | // Copyright (c) 2026 Michael Vandeberg | ||||||
| 3 | - | // Copyright (c) 2026 Steve Gerbino | ||||||
| 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_BUFFERS_BUFFER_SLICE_HPP | ||||||
| 12 | - | #define BOOST_CAPY_BUFFERS_BUFFER_SLICE_HPP | ||||||
| 13 | - | |||||||
| 14 | - | #include <boost/capy/detail/config.hpp> | ||||||
| 15 | - | #include <boost/capy/buffers.hpp> | ||||||
| 16 | - | #include <boost/capy/detail/slice_of.hpp> | ||||||
| 17 | - | |||||||
| 18 | - | #include <concepts> | ||||||
| 19 | - | #include <cstddef> | ||||||
| 20 | - | #include <limits> | ||||||
| 21 | - | #include <type_traits> | ||||||
| 22 | - | |||||||
| 23 | - | namespace boost { | ||||||
| 24 | - | namespace capy { | ||||||
| 25 | - | |||||||
| 26 | - | /** Names whichever buffer type `buffer_slice` returns for a sequence `BS`. | ||||||
| 27 | - | |||||||
| 28 | - | A single buffer is closed under sub-ranging, so slicing it yields a | ||||||
| 29 | - | buffer of the same kind. Any other sequence yields the generic | ||||||
| 30 | - | `detail::slice_of<BS>` borrowed view. In both cases the result is itself | ||||||
| 31 | - | a buffer sequence — `slice_type<BS> ∈ { buffer, slice_of<BS> }`. | ||||||
| 32 | - | */ | ||||||
| 33 | - | template<class BS> | ||||||
| 34 | - | using slice_type = std::conditional_t< | ||||||
| 35 | - | ConstBuffer<BS>, | ||||||
| 36 | - | buffer_type<BS>, | ||||||
| 37 | - | detail::slice_of<BS>>; | ||||||
| 38 | - | |||||||
| 39 | - | /** Return a byte sub-range of a buffer sequence, as a value. | ||||||
| 40 | - | |||||||
| 41 | - | The result is itself a buffer sequence (`slice_type<BS>`), so pass it | ||||||
| 42 | - | directly to any operation expecting a buffer sequence. There is no | ||||||
| 43 | - | `.data()` and no separate concept to bind. For a single buffer the | ||||||
| 44 | - | result is an adjusted buffer; for any other sequence it is a borrowed | ||||||
| 45 | - | `slice_of<BS>` view. | ||||||
| 46 | - | |||||||
| 47 | - | @par Lifetime | ||||||
| 48 | - | Except for the single-buffer case, the result borrows `seq`: it stores | ||||||
| 49 | - | iterators into the sequence, not a copy. `seq` must outlive the result. | ||||||
| 50 | - | The rvalue overload is deleted so a temporary cannot be sliced into a | ||||||
| 51 | - | dangling view. | ||||||
| 52 | - | |||||||
| 53 | - | @par Complexity | ||||||
| 54 | - | Single forward pass to the cut points; never sums the whole sequence. | ||||||
| 55 | - | |||||||
| 56 | - | @param seq The sequence to slice. Must outlive the result. | ||||||
| 57 | - | @param offset Bytes skipped from the front. Clamped to the total size. | ||||||
| 58 | - | @param length Bytes exposed, starting at `offset`. Defaults to the end. | ||||||
| 59 | - | |||||||
| 60 | - | @return A `slice_type<BS>` value modeling the same buffer-sequence | ||||||
| 61 | - | concept as `seq` (mutable if `seq` is mutable). | ||||||
| 62 | - | |||||||
| 63 | - | @par Example | ||||||
| 64 | - | @par !example example | ||||||
| 65 | - | |||||||
| 66 | - | |||||||
| 67 | - | @see @ref slice_type, @ref consuming_buffers | ||||||
| 68 | - | */ | ||||||
| 69 | - | template<class BufferSequence> | ||||||
| 70 | - | requires ConstBufferSequence<BufferSequence> | ||||||
| 71 | - | slice_type<BufferSequence> | ||||||
| DCB | 72 | - | 2672 | buffer_slice( | ||||
| 73 | - | BufferSequence const& seq, | ||||||
| 74 | - | std::size_t offset = 0, | ||||||
| 75 | - | std::size_t length = | ||||||
| 76 | - | (std::numeric_limits<std::size_t>::max)()) noexcept | ||||||
| 77 | - | { | ||||||
| 78 | - | if constexpr (ConstBuffer<BufferSequence>) | ||||||
| 79 | - | { | ||||||
| 80 | - | // A single buffer is its own slice: advance and (maybe) truncate. | ||||||
| DCB | 81 | - | 385 | buffer_type<BufferSequence> b = seq; | ||||
| DCB | 82 | - | 385 | b += offset; // operator+= clamps to size() | ||||
| DCB | 83 | - | 385 | if (length < b.size()) | ||||
| DCB | 84 | - | 156 | b = buffer_type<BufferSequence>(b.data(), length); | ||||
| DCB | 85 | - | 385 | return b; | ||||
| 86 | - | } | ||||||
| 87 | - | else | ||||||
| 88 | - | { | ||||||
| DCB | 89 | - | 2287 | return detail::slice_of<BufferSequence>(seq, offset, length); | ||||
| 90 | - | } | ||||||
| 91 | - | } | ||||||
| 92 | - | |||||||
| 93 | - | /** Rejects a temporary sequence at compile time, since the result would dangle. | ||||||
| 94 | - | |||||||
| 95 | - | Slicing a temporary would yield an immediately dangling view (the | ||||||
| 96 | - | result borrows the sequence). Hoist the sequence into a named variable | ||||||
| 97 | - | first. | ||||||
| 98 | - | */ | ||||||
| 99 | - | template<class BufferSequence> | ||||||
| 100 | - | requires ConstBufferSequence<BufferSequence> | ||||||
| 101 | - | slice_type<BufferSequence> | ||||||
| 102 | - | buffer_slice( | ||||||
| 103 | - | BufferSequence const&& seq, | ||||||
| 104 | - | std::size_t offset = 0, | ||||||
| 105 | - | std::size_t length = | ||||||
| 106 | - | (std::numeric_limits<std::size_t>::max)()) = delete; | ||||||
| 107 | - | |||||||
| 108 | - | } // namespace capy | ||||||
| 109 | - | } // namespace boost | ||||||
| 110 | - | |||||||
| 111 | - | #endif | ||||||