diff --git a/doc/modules/ROOT/pages/5.buffers/5e.rationale.adoc b/doc/modules/ROOT/pages/5.buffers/5e.rationale.adoc index 7211c6c81..e6ce51f61 100644 --- a/doc/modules/ROOT/pages/5.buffers/5e.rationale.adoc +++ b/doc/modules/ROOT/pages/5.buffers/5e.rationale.adoc @@ -11,6 +11,19 @@ = Rationale :page-mode: explanation +== Why not cpp:span[std::span]? + +The C++ Standard Library already provides a type for representing a handle +to a contiguous memory region: cpp:std::span[]. Why create a new type cpp:const_buffer[]? +The answer is in undesired implicit conversions that cpp:std::span[] offers. + +One of the important guarantees that concepts cpp:ConstBuffer[] and cpp:ConstBufferSequence[] offer +is that copying their models does not change the location of the contiguous memory region(s). +cpp:std::span[] is very liberal about allowing implicit conversions. If we used it, +it would make cpp:std::vector[] satisfy cpp:ConstBuffer[] without +meeting its semantic requirements (shallow copy), and thus causing the programs that rely on these +semantic requirements to behave incorrectly. + == Why `void*` rather than `std::byte`? Two concrete forces decide it: diff --git a/include/boost/capy/buffers.hpp b/include/boost/capy/buffers.hpp index 5d93afb11..66c0478eb 100644 --- a/include/boost/capy/buffers.hpp +++ b/include/boost/capy/buffers.hpp @@ -2,6 +2,7 @@ // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) // Copyright (c) 2026 Michael Vandeberg // Copyright (c) 2026 Steve Gerbino +// Copyright (c) 2026 Andrzej Krzemieński // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -228,18 +229,22 @@ class const_buffer } }; -/** Requires a type to convert to `const_buffer`. +/** Type recognized by the library as a _buffer_ (a handle to contiguous memory region) with const access to data. - This is the element requirement of @ref ConstBufferSequence. + @par Semantic Requirements - For a `T const` lvalue `x`, both `const_buffer b = x;` and - `const_buffer(x)` must be valid, since algorithms convert elements - through a const reference and may use either form. A conversion - operator must therefore be `const`. + After the conversion to @ref const_buffer `b`, `b.data()` and `b.size()` + designate a valid contiguous memory region. The region shall remain valid + for the duration of an operation represented by the concept-constrained + function template call. - @note Algorithms that read buffer sequences are `noexcept`, so a - conversion that throws terminates the program. Conversion operators - should be `noexcept`. + @note Function templates in this library constrained by this concept, + such as @ref buffer_size, are often `noexcept`, so if this conversion + throws, this results in calling `std::terminate()`. + + @note `std::convertible_to` implies that @ref const_buffer + can also be explicitly constructed from `T const&` with the same effect as the + implicit conversion. @see @ref const_buffer, @ref MutableBuffer */ @@ -247,17 +252,26 @@ template concept ConstBuffer = std::convertible_to; -/** Requires a type to convert to both `mutable_buffer` and `const_buffer`. +/** Type recognized by the library as a _buffer_ (a handle to contiguous memory region) with mutable access to data. + + @par Semantic Requirements + + After the conversion to @ref mutable_buffer or @ref const_buffer `b`, `b.data()` and `b.size()` + designate a valid contiguous memory region. The region shall remain valid + for the duration of an operation represented by the concept-constrained + function template call. - This is the element requirement of @ref MutableBufferSequence. + @note Function templates in this library constrained by this concept, + such as @ref buffer_size, are often `noexcept`, so if this conversion + throws, this results in calling `std::terminate()`. - A type that converts only to `mutable_buffer` does not qualify. - Reaching `const_buffer` from it takes two user-defined conversions, - and an implicit conversion sequence allows one. Such a type must - also provide a conversion to `const_buffer`. + @note `std::convertible_to` implies that @ref mutable_buffer + can also be explicitly constructed from `T const&` with the same effect as the + implicit conversion. - As with @ref ConstBuffer, the conversion is from a `T const` lvalue, - and conversion operators should be `noexcept`. + @note A type that has a non-explicit conversion operator to `mutable_buffer` + does not automatically satisfy the requirement of being convertible to + `const_buffer`. @see @ref mutable_buffer, @ref ConstBuffer */ @@ -265,41 +279,65 @@ template concept MutableBuffer = ConstBuffer && std::convertible_to; -/** Requires a type to be a @ref ConstBuffer, or a range of them. +/** Type recognized by the library as a _sequence_ of buffers with const access to data: either a type modelling @ref ConstBuffer or a range thereof. + + @par Semantic Requirements + + A copy-constructed buffer sequence shall refer to the same contiguous memory regions as the original. + This can be expressed as: + + @code + bool copy_postcondition(ConstBufferSequence auto const& x) + { + ConstBufferSequence auto y = x; + return std::ranges::equal( + buffer_sequence_begin(x), buffer_sequence_end(x), + buffer_sequence_begin(y), buffer_sequence_end(y), + [](const const_buffer& bx, const const_buffer& by) + { + return bx.data() == by.data() + && bx.size() == by.size(); + } + ); + } + @endcode + + A move-constructed buffer sequence shall refer to the same contiguous memory regions + as the original did prior to the move. - A type satisfies `ConstBufferSequence` if it represents one or more - contiguous memory regions that can be read. This includes single - buffers and ranges of buffers. + If a type models both @ref ConstBuffer and a range thereof, + then it is treated as @ref ConstBuffer. - @par Syntactic Requirements - @li @ref ConstBuffer, OR - @li A bidirectional range whose value type is a @ref ConstBuffer + @note Function templates in this library constrained by this concept, + such as @ref buffer_size, are often `noexcept`, so if the operations used + to iterate over the buffer range — such as `.begin()` or the iterator API — + throw, this results in calling `std::terminate()`. @see @ref const_buffer, @ref MutableBufferSequence */ // tag::const_buffer_sequence_concept[] template concept ConstBufferSequence = - ConstBuffer || ( + (ConstBuffer || ( std::ranges::bidirectional_range && - ConstBuffer>); + ConstBuffer>)) + && std::copy_constructible; // end::const_buffer_sequence_concept[] -/** Requires a type to be a @ref MutableBuffer, or a range of them. +/** Type recognized by the library as a _sequence_ of buffers with mutable access to data: either a type modelling @ref MutableBuffer or a range thereof. - A type satisfies `MutableBufferSequence` if it represents one or more - contiguous memory regions that can be written. This includes single - buffers and ranges of buffers. + @par Semantic Requirements - Every `MutableBufferSequence` is also a @ref ConstBufferSequence. + If a type models both @ref MutableBuffer and a range thereof, + then it is treated as @ref MutableBuffer. - A @ref ConstBuffer is a single buffer, even if it is also a range, - so only its own conversion to `mutable_buffer` is considered. + @note A type satisfying @ref ConstBuffer is never considered to be a range of + mutable buffers. - @par Syntactic Requirements - @li @ref MutableBuffer, OR - @li A bidirectional range, not a @ref ConstBuffer, whose value type - is a @ref MutableBuffer + @note Function templates in this library constrained by this concept, + such as @ref buffer_size, are often `noexcept`, so if the operations used + to iterate over the buffer range — such as `.begin()` or the iterator API — + throw, this results in calling `std::terminate()`. @see @ref mutable_buffer, @ref ConstBufferSequence */ diff --git a/include/boost/capy/detail/buffer_array.hpp b/include/boost/capy/detail/buffer_array.hpp index 4691042ff..8e863c25a 100644 --- a/include/boost/capy/detail/buffer_array.hpp +++ b/include/boost/capy/detail/buffer_array.hpp @@ -103,9 +103,9 @@ class buffer_array @param bs The buffer sequence to copy from. */ template - requires (IsConst ? ConstBufferSequence : MutableBufferSequence) - && (!std::same_as, buffer_array>) + requires (!std::same_as, buffer_array>) && (!std::same_as, value_type>) + && (IsConst ? ConstBufferSequence : MutableBufferSequence) buffer_array(BS const& bs) noexcept : dummy_(0) { diff --git a/test/unit/buffers/buffer.cpp b/test/unit/buffers/buffer.cpp index 87f64a955..1025f80d9 100644 --- a/test/unit/buffers/buffer.cpp +++ b/test/unit/buffers/buffer.cpp @@ -50,10 +50,10 @@ static_assert( ConstBufferSequence>); static_assert(! MutableBufferSequence>); static_assert( MutableBufferSequence>); -static_assert( ConstBufferSequence); -static_assert( ConstBufferSequence); +static_assert(! ConstBufferSequence); +static_assert(! ConstBufferSequence); static_assert(! MutableBufferSequence); -static_assert( MutableBufferSequence); +static_assert(! MutableBufferSequence); // User types with conversion operators @@ -111,6 +111,18 @@ struct user_buffer operator mutable_buffer() const noexcept { return { p, n }; } operator const_buffer() const noexcept { return { p, n }; } + + + // The following is to have API without default constructors or copy/move assignments. + // this is to make sure that constrained templates use only the API advertised + // by the concepts. + + struct init_t {}; + user_buffer(init_t, char* p_, std::size_t n_) : p{p_}, n{n_} {} + user_buffer(user_buffer&&) = default; + user_buffer(user_buffer const&) = default; + user_buffer& operator=(user_buffer&&) = delete; + user_buffer& operator=(user_buffer const&) = delete; }; } // namespace @@ -456,8 +468,6 @@ struct buffer_test testBuffer>(); testBuffer>(); testBuffer>(); - testBuffer(); - testBuffer(); } //-------------------------------------------- @@ -611,11 +621,11 @@ struct buffer_test void testUserBuffer() { char data[8] = {}; - user_buffer const ub{ data, 4 }; + user_buffer const ub{ user_buffer::init_t{}, data, 4 }; BOOST_TEST_EQ(buffer_size(ub), 4); std::array const arr{{ - { data, 4 }, { data + 4, 4 } }}; + { user_buffer::init_t{}, data, 4 }, { user_buffer::init_t{}, data + 4, 4 } }}; BOOST_TEST_EQ(buffer_size(arr), 8); char const src[] = "abcdefgh";