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