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