Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions doc/modules/ROOT/pages/5.buffers/5e.rationale.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
112 changes: 75 additions & 37 deletions include/boost/capy/buffers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -228,78 +229,115 @@ 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
Comment thread
akrzemi1 marked this conversation as resolved.
Resolved
`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`.
Comment thread
akrzemi1 marked this conversation as resolved.
Resolved
@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
*/
template<typename T>
concept ConstBuffer =
std::convertible_to<T const&, const_buffer>;

/** Requires a type to convert to both `mutable_buffer` and `const_buffer`.
Comment thread
akrzemi1 marked this conversation as resolved.
Resolved
/** 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.
Comment thread
akrzemi1 marked this conversation as resolved.
Resolved
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
*/
template<typename T>
concept MutableBuffer =
ConstBuffer<T> && std::convertible_to<T const&, mutable_buffer>;

/** 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(
Comment thread
akrzemi1 marked this conversation as resolved.
Resolved
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<typename T>
concept ConstBufferSequence =
ConstBuffer<T> || (
(ConstBuffer<T> || (
std::ranges::bidirectional_range<T> &&
ConstBuffer<std::ranges::range_value_t<T>>);
ConstBuffer<std::ranges::range_value_t<T>>))
&& std::copy_constructible<T>;
// 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
*/
Expand Down
4 changes: 2 additions & 2 deletions include/boost/capy/detail/buffer_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ class buffer_array
@param bs The buffer sequence to copy from.
*/
template<class BS>
requires (IsConst ? ConstBufferSequence<BS> : MutableBufferSequence<BS>)
&& (!std::same_as<std::remove_cvref_t<BS>, buffer_array>)
requires (!std::same_as<std::remove_cvref_t<BS>, buffer_array>)
&& (!std::same_as<std::remove_cvref_t<BS>, value_type>)
&& (IsConst ? ConstBufferSequence<BS> : MutableBufferSequence<BS>)
Comment thread
akrzemi1 marked this conversation as resolved.
Resolved
buffer_array(BS const& bs) noexcept
: dummy_(0)
{
Expand Down
24 changes: 17 additions & 7 deletions test/unit/buffers/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ static_assert( ConstBufferSequence<std::array<mutable_buffer const, 3>>);
static_assert(! MutableBufferSequence<std::array<const_buffer const, 3>>);
static_assert( MutableBufferSequence<std::array<mutable_buffer const, 3>>);

static_assert( ConstBufferSequence<const_buffer[3]>);
static_assert( ConstBufferSequence<mutable_buffer[3]>);
static_assert(! ConstBufferSequence<const_buffer[3]>);
Comment thread
akrzemi1 marked this conversation as resolved.
Resolved
static_assert(! ConstBufferSequence<mutable_buffer[3]>);
static_assert(! MutableBufferSequence<const_buffer[3]>);
static_assert( MutableBufferSequence<mutable_buffer[3]>);
static_assert(! MutableBufferSequence<mutable_buffer[3]>);

// User types with conversion operators

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -456,8 +468,6 @@ struct buffer_test
testBuffer<std::span<mutable_buffer,3>>();
testBuffer<std::array<const_buffer,3>>();
testBuffer<std::array<mutable_buffer,3>>();
testBuffer<const_buffer[3]>();
testBuffer<mutable_buffer[3]>();
}

//--------------------------------------------
Expand Down Expand Up @@ -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<user_buffer, 2> 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";
Expand Down
Loading