1  
//
1  
//
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/cppalliance/capy
7  
// Official repository: https://github.com/cppalliance/capy
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_CAPY_BUFFERS_HPP
10  
#ifndef BOOST_CAPY_BUFFERS_HPP
11  
#define BOOST_CAPY_BUFFERS_HPP
11  
#define BOOST_CAPY_BUFFERS_HPP
12  

12  

13  
#include <boost/capy/detail/config.hpp>
13  
#include <boost/capy/detail/config.hpp>
14  
#include <concepts>
14  
#include <concepts>
15  
#include <cstddef>
15  
#include <cstddef>
16  
#include <iterator>
16  
#include <iterator>
17  
#include <memory>
17  
#include <memory>
18  
#include <ranges>
18  
#include <ranges>
19  
#include <type_traits>
19  
#include <type_traits>
20  

20  

21  
// https://www.boost.org/doc/libs/1_65_0/doc/html/boost_asio/reference/ConstBufferSequence.html
21  
// https://www.boost.org/doc/libs/1_65_0/doc/html/boost_asio/reference/ConstBufferSequence.html
22  

22  

23  
namespace boost {
23  
namespace boost {
24  

24  

25  
namespace asio {
25  
namespace asio {
26  
class const_buffer;
26  
class const_buffer;
27  
class mutable_buffer;
27  
class mutable_buffer;
28  
} // asio
28  
} // asio
29  

29  

30  
namespace capy {
30  
namespace capy {
31  

31  

32  
class const_buffer;
32  
class const_buffer;
33  
class mutable_buffer;
33  
class mutable_buffer;
34 -
//------------------------------------------------
 
35 -

 
36  

34  

37  
/// Tag type for customizing `buffer_size` via `tag_invoke`.
35  
/// Tag type for customizing `buffer_size` via `tag_invoke`.
38  
struct size_tag {};
36  
struct size_tag {};
39  

37  

40  
/// Tag type for customizing slice operations via `tag_invoke`.
38  
/// Tag type for customizing slice operations via `tag_invoke`.
41  
struct slice_tag {};
39  
struct slice_tag {};
42  

40  

43  
/** Constants for slice customization.
41  
/** Constants for slice customization.
44  

42  

45  
    Passed to `tag_invoke` overloads to specify which portion
43  
    Passed to `tag_invoke` overloads to specify which portion
46  
    of a buffer sequence to retain.
44  
    of a buffer sequence to retain.
47  
*/
45  
*/
48  
enum class slice_how
46  
enum class slice_how
49  
{
47  
{
50  
    /// Remove bytes from the front of the sequence.
48  
    /// Remove bytes from the front of the sequence.
51  
    remove_prefix,
49  
    remove_prefix,
52  

50  

53  
    /// Keep only the first N bytes.
51  
    /// Keep only the first N bytes.
54  
    keep_prefix
52  
    keep_prefix
55  
};
53  
};
56 -
//------------------------------------------------
 
57 -

 
58  

54  

59  
/** A reference to a contiguous region of writable memory.
55  
/** A reference to a contiguous region of writable memory.
60  

56  

61  
    Represents a pointer and size pair for a modifiable byte range.
57  
    Represents a pointer and size pair for a modifiable byte range.
62  
    Does not own the memory. Satisfies `MutableBufferSequence` (as a
58  
    Does not own the memory. Satisfies `MutableBufferSequence` (as a
63  
    single-element sequence) and is implicitly convertible to
59  
    single-element sequence) and is implicitly convertible to
64  
    `const_buffer`.
60  
    `const_buffer`.
65  

61  

66  
    @see const_buffer, MutableBufferSequence
62  
    @see const_buffer, MutableBufferSequence
67  
*/
63  
*/
68  
class mutable_buffer
64  
class mutable_buffer
69  
{
65  
{
70  
    unsigned char* p_ = nullptr;
66  
    unsigned char* p_ = nullptr;
71  
    std::size_t n_ = 0;
67  
    std::size_t n_ = 0;
72  

68  

73  
public:
69  
public:
74  
    /// Construct an empty buffer.
70  
    /// Construct an empty buffer.
75  
    mutable_buffer() = default;
71  
    mutable_buffer() = default;
76  

72  

77 -
    /// Copy constructor.
73 +
    /// Construct a copy.
78  
    mutable_buffer(
74  
    mutable_buffer(
79  
        mutable_buffer const&) = default;
75  
        mutable_buffer const&) = default;
80  

76  

81 -
    /// Copy assignment.
77 +
    /// Assign by copying.
82  
    mutable_buffer& operator=(
78  
    mutable_buffer& operator=(
83  
        mutable_buffer const&) = default;
79  
        mutable_buffer const&) = default;
84  

80  

85  
    /// Construct from pointer and size.
81  
    /// Construct from pointer and size.
86  
    constexpr mutable_buffer(
82  
    constexpr mutable_buffer(
87  
        void* data, std::size_t size) noexcept
83  
        void* data, std::size_t size) noexcept
88  
        : p_(static_cast<unsigned char*>(data))
84  
        : p_(static_cast<unsigned char*>(data))
89  
        , n_(size)
85  
        , n_(size)
90  
    {
86  
    {
91  
    }
87  
    }
92  

88  

93  
    /// Return a pointer to the memory region.
89  
    /// Return a pointer to the memory region.
94  
    constexpr void* data() const noexcept
90  
    constexpr void* data() const noexcept
95  
    {
91  
    {
96  
        return p_;
92  
        return p_;
97  
    }
93  
    }
98  

94  

99  
    /// Return the size in bytes.
95  
    /// Return the size in bytes.
100  
    constexpr std::size_t size() const noexcept
96  
    constexpr std::size_t size() const noexcept
101  
    {
97  
    {
102  
        return n_;
98  
        return n_;
103  
    }
99  
    }
104  

100  

105  
    /** Advance the buffer start, shrinking the region.
101  
    /** Advance the buffer start, shrinking the region.
106  

102  

107  
        @param n Bytes to skip. Clamped to `size()`.
103  
        @param n Bytes to skip. Clamped to `size()`.
108  
    */
104  
    */
109  
    mutable_buffer&
105  
    mutable_buffer&
110  
    operator+=(std::size_t n) noexcept
106  
    operator+=(std::size_t n) noexcept
111  
    {
107  
    {
112  
        if( n > n_)
108  
        if( n > n_)
113  
            n = n_;
109  
            n = n_;
114  
        p_ += n;
110  
        p_ += n;
115  
        n_ -= n;
111  
        n_ -= n;
116  
        return *this;
112  
        return *this;
117  
    }
113  
    }
118  

114  

119  
    /// Slice customization point for `tag_invoke`.
115  
    /// Slice customization point for `tag_invoke`.
120  
    friend
116  
    friend
121  
    void
117  
    void
122  
    tag_invoke(
118  
    tag_invoke(
123  
        slice_tag const&,
119  
        slice_tag const&,
124  
        mutable_buffer& b,
120  
        mutable_buffer& b,
125  
        slice_how how,
121  
        slice_how how,
126  
        std::size_t n) noexcept
122  
        std::size_t n) noexcept
127  
    {
123  
    {
128  
        b.do_slice(how, n);
124  
        b.do_slice(how, n);
129  
    }
125  
    }
130  

126  

131  
private:
127  
private:
132  
    void do_slice(
128  
    void do_slice(
133  
        slice_how how, std::size_t n) noexcept
129  
        slice_how how, std::size_t n) noexcept
134  
    {
130  
    {
135  
        switch(how)
131  
        switch(how)
136  
        {
132  
        {
137  
        case slice_how::remove_prefix:
133  
        case slice_how::remove_prefix:
138  
            *this += n;
134  
            *this += n;
139  
            return;
135  
            return;
140  

136  

141  
        case slice_how::keep_prefix:
137  
        case slice_how::keep_prefix:
142  
            if( n < n_)
138  
            if( n < n_)
143  
                n_ = n;
139  
                n_ = n;
144  
            return;
140  
            return;
145  
        }
141  
        }
146  
    }
142  
    }
147  
};
143  
};
148 -
//------------------------------------------------
 
149 -

 
150  

144  

151  
/** A reference to a contiguous region of read-only memory.
145  
/** A reference to a contiguous region of read-only memory.
152  

146  

153  
    Represents a pointer and size pair for a non-modifiable byte range.
147  
    Represents a pointer and size pair for a non-modifiable byte range.
154  
    Does not own the memory. Satisfies `ConstBufferSequence` (as a
148  
    Does not own the memory. Satisfies `ConstBufferSequence` (as a
155  
    single-element sequence). Implicitly constructible from
149  
    single-element sequence). Implicitly constructible from
156  
    `mutable_buffer`.
150  
    `mutable_buffer`.
157  

151  

158  
    @see mutable_buffer, ConstBufferSequence
152  
    @see mutable_buffer, ConstBufferSequence
159  
*/
153  
*/
160  
class const_buffer
154  
class const_buffer
161  
{
155  
{
162  
    unsigned char const* p_ = nullptr;
156  
    unsigned char const* p_ = nullptr;
163  
    std::size_t n_ = 0;
157  
    std::size_t n_ = 0;
164  

158  

165  
public:
159  
public:
166  
    /// Construct an empty buffer.
160  
    /// Construct an empty buffer.
167  
    const_buffer() = default;
161  
    const_buffer() = default;
168  

162  

169 -
    /// Copy constructor.
163 +
    /// Construct a copy.
170  
    const_buffer(const_buffer const&) = default;
164  
    const_buffer(const_buffer const&) = default;
171  

165  

172 -
    /// Copy assignment.
166 +
    /// Assign by copying.
173  
    const_buffer& operator=(
167  
    const_buffer& operator=(
174  
        const_buffer const& other) = default;
168  
        const_buffer const& other) = default;
175  

169  

176  
    /// Construct from pointer and size.
170  
    /// Construct from pointer and size.
177  
    constexpr const_buffer(
171  
    constexpr const_buffer(
178  
        void const* data, std::size_t size) noexcept
172  
        void const* data, std::size_t size) noexcept
179  
        : p_(static_cast<unsigned char const*>(data))
173  
        : p_(static_cast<unsigned char const*>(data))
180  
        , n_(size)
174  
        , n_(size)
181  
    {
175  
    {
182  
    }
176  
    }
183  

177  

184  
    /// Construct from mutable_buffer.
178  
    /// Construct from mutable_buffer.
185  
    constexpr const_buffer(
179  
    constexpr const_buffer(
186  
        mutable_buffer const& b) noexcept
180  
        mutable_buffer const& b) noexcept
187  
        : p_(static_cast<unsigned char const*>(b.data()))
181  
        : p_(static_cast<unsigned char const*>(b.data()))
188  
        , n_(b.size())
182  
        , n_(b.size())
189  
    {
183  
    {
190  
    }
184  
    }
191  

185  

192  
    /// Return a pointer to the memory region.
186  
    /// Return a pointer to the memory region.
193  
    constexpr void const* data() const noexcept
187  
    constexpr void const* data() const noexcept
194  
    {
188  
    {
195  
        return p_;
189  
        return p_;
196  
    }
190  
    }
197  

191  

198  
    /// Return the size in bytes.
192  
    /// Return the size in bytes.
199  
    constexpr std::size_t size() const noexcept
193  
    constexpr std::size_t size() const noexcept
200  
    {
194  
    {
201  
        return n_;
195  
        return n_;
202  
    }
196  
    }
203  

197  

204  
    /** Advance the buffer start, shrinking the region.
198  
    /** Advance the buffer start, shrinking the region.
205  

199  

206  
        @param n Bytes to skip. Clamped to `size()`.
200  
        @param n Bytes to skip. Clamped to `size()`.
207  
    */
201  
    */
208  
    const_buffer&
202  
    const_buffer&
209  
    operator+=(std::size_t n) noexcept
203  
    operator+=(std::size_t n) noexcept
210  
    {
204  
    {
211  
        if( n > n_)
205  
        if( n > n_)
212  
            n = n_;
206  
            n = n_;
213  
        p_ += n;
207  
        p_ += n;
214  
        n_ -= n;
208  
        n_ -= n;
215  
        return *this;
209  
        return *this;
216  
    }
210  
    }
217  

211  

218  
    /// Slice customization point for `tag_invoke`.
212  
    /// Slice customization point for `tag_invoke`.
219  
    friend
213  
    friend
220  
    void
214  
    void
221  
    tag_invoke(
215  
    tag_invoke(
222  
        slice_tag const&,
216  
        slice_tag const&,
223  
        const_buffer& b,
217  
        const_buffer& b,
224  
        slice_how how,
218  
        slice_how how,
225  
        std::size_t n) noexcept
219  
        std::size_t n) noexcept
226  
    {
220  
    {
227  
        b.do_slice(how, n);
221  
        b.do_slice(how, n);
228  
    }
222  
    }
229  

223  

230  
private:
224  
private:
231  
    void do_slice(
225  
    void do_slice(
232  
        slice_how how, std::size_t n) noexcept
226  
        slice_how how, std::size_t n) noexcept
233  
    {
227  
    {
234  
        switch(how)
228  
        switch(how)
235  
        {
229  
        {
236  
        case slice_how::remove_prefix:
230  
        case slice_how::remove_prefix:
237  
            *this += n;
231  
            *this += n;
238  
            return;
232  
            return;
239  

233  

240  
        case slice_how::keep_prefix:
234  
        case slice_how::keep_prefix:
241  
            if( n < n_)
235  
            if( n < n_)
242  
                n_ = n;
236  
                n_ = n;
243  
            return;
237  
            return;
244  
        }
238  
        }
245  
    }
239  
    }
246  
};
240  
};
247 -
//------------------------------------------------
 
248 -

 
249  

241  

250  
/** Concept for sequences of read-only buffer regions.
242  
/** Concept for sequences of read-only buffer regions.
251  

243  

252  
    A type satisfies `ConstBufferSequence` if it represents one or more
244  
    A type satisfies `ConstBufferSequence` if it represents one or more
253  
    contiguous memory regions that can be read. This includes single
245  
    contiguous memory regions that can be read. This includes single
254  
    buffers (convertible to `const_buffer`) and ranges of buffers.
246  
    buffers (convertible to `const_buffer`) and ranges of buffers.
255  

247  

256  
    @par Syntactic Requirements
248  
    @par Syntactic Requirements
257  
    @li Convertible to `const_buffer`, OR
249  
    @li Convertible to `const_buffer`, OR
258  
    @li A bidirectional range with value type convertible to `const_buffer`
250  
    @li A bidirectional range with value type convertible to `const_buffer`
259  

251  

260  
    @see const_buffer, MutableBufferSequence
252  
    @see const_buffer, MutableBufferSequence
261  
*/
253  
*/
262  
template<typename T>
254  
template<typename T>
263  
concept ConstBufferSequence =
255  
concept ConstBufferSequence =
264  
    std::is_convertible_v<T, const_buffer> || (
256  
    std::is_convertible_v<T, const_buffer> || (
265  
        std::ranges::bidirectional_range<T> &&
257  
        std::ranges::bidirectional_range<T> &&
266  
        std::is_convertible_v<std::ranges::range_value_t<T>, const_buffer>);
258  
        std::is_convertible_v<std::ranges::range_value_t<T>, const_buffer>);
267  

259  

268  
/** Concept for sequences of writable buffer regions.
260  
/** Concept for sequences of writable buffer regions.
269  

261  

270  
    A type satisfies `MutableBufferSequence` if it represents one or more
262  
    A type satisfies `MutableBufferSequence` if it represents one or more
271  
    contiguous memory regions that can be written. This includes single
263  
    contiguous memory regions that can be written. This includes single
272  
    buffers (convertible to `mutable_buffer`) and ranges of buffers.
264  
    buffers (convertible to `mutable_buffer`) and ranges of buffers.
273  
    Every `MutableBufferSequence` also satisfies `ConstBufferSequence`.
265  
    Every `MutableBufferSequence` also satisfies `ConstBufferSequence`.
274  

266  

275  
    @par Syntactic Requirements
267  
    @par Syntactic Requirements
276  
    @li Convertible to `mutable_buffer`, OR
268  
    @li Convertible to `mutable_buffer`, OR
277  
    @li A bidirectional range with value type convertible to `mutable_buffer`
269  
    @li A bidirectional range with value type convertible to `mutable_buffer`
278  

270  

279  
    @see mutable_buffer, ConstBufferSequence
271  
    @see mutable_buffer, ConstBufferSequence
280  
*/
272  
*/
281  
template<typename T>
273  
template<typename T>
282  
concept MutableBufferSequence =
274  
concept MutableBufferSequence =
283  
    std::is_convertible_v<T, mutable_buffer> || (
275  
    std::is_convertible_v<T, mutable_buffer> || (
284  
        std::ranges::bidirectional_range<T> &&
276  
        std::ranges::bidirectional_range<T> &&
285  
        std::is_convertible_v<std::ranges::range_value_t<T>, mutable_buffer>);
277  
        std::is_convertible_v<std::ranges::range_value_t<T>, mutable_buffer>);
286 -
//------------------------------------------------------------------------------
 
287 -

 
288  

278  

289  
/** Return an iterator to the first buffer in a sequence.
279  
/** Return an iterator to the first buffer in a sequence.
290  

280  

291  
    Handles single buffers and ranges uniformly. For a single buffer,
281  
    Handles single buffers and ranges uniformly. For a single buffer,
292  
    returns a pointer to it (forming a one-element range).
282  
    returns a pointer to it (forming a one-element range).
293  
*/
283  
*/
294  
constexpr struct begin_mrdocs_workaround_t
284  
constexpr struct begin_mrdocs_workaround_t
295  
{
285  
{
296  
    template<std::convertible_to<const_buffer> ConvertibleToBuffer>
286  
    template<std::convertible_to<const_buffer> ConvertibleToBuffer>
297  
    auto operator()(ConvertibleToBuffer const& b) const noexcept -> ConvertibleToBuffer const*
287  
    auto operator()(ConvertibleToBuffer const& b) const noexcept -> ConvertibleToBuffer const*
298  
    {
288  
    {
299  
        return std::addressof(b);
289  
        return std::addressof(b);
300  
    }
290  
    }
301  

291  

302  
    template<ConstBufferSequence BS>
292  
    template<ConstBufferSequence BS>
303  
        requires (!std::convertible_to<BS, const_buffer>)
293  
        requires (!std::convertible_to<BS, const_buffer>)
304  
    auto operator()(BS const& bs) const noexcept
294  
    auto operator()(BS const& bs) const noexcept
305  
    {
295  
    {
306  
        return std::ranges::begin(bs);
296  
        return std::ranges::begin(bs);
307  
    }
297  
    }
308  

298  

309  
    template<ConstBufferSequence BS>
299  
    template<ConstBufferSequence BS>
310  
        requires (!std::convertible_to<BS, const_buffer>)
300  
        requires (!std::convertible_to<BS, const_buffer>)
311  
    auto operator()(BS& bs) const noexcept
301  
    auto operator()(BS& bs) const noexcept
312  
    {
302  
    {
313  
        return std::ranges::begin(bs);
303  
        return std::ranges::begin(bs);
314  
    }
304  
    }
315  
} begin {};
305  
} begin {};
316  

306  

317  
/** Return an iterator past the last buffer in a sequence.
307  
/** Return an iterator past the last buffer in a sequence.
318  

308  

319  
    Handles single buffers and ranges uniformly. For a single buffer,
309  
    Handles single buffers and ranges uniformly. For a single buffer,
320  
    returns a pointer one past it.
310  
    returns a pointer one past it.
321  
*/
311  
*/
322  
constexpr struct end_mrdocs_workaround_t
312  
constexpr struct end_mrdocs_workaround_t
323  
{
313  
{
324  
    template<std::convertible_to<const_buffer> ConvertibleToBuffer>
314  
    template<std::convertible_to<const_buffer> ConvertibleToBuffer>
325  
    auto operator()(ConvertibleToBuffer const& b) const noexcept -> ConvertibleToBuffer const*
315  
    auto operator()(ConvertibleToBuffer const& b) const noexcept -> ConvertibleToBuffer const*
326  
    {
316  
    {
327  
        return std::addressof(b) + 1;
317  
        return std::addressof(b) + 1;
328  
    }
318  
    }
329  

319  

330  
    template<ConstBufferSequence BS>
320  
    template<ConstBufferSequence BS>
331  
        requires (!std::convertible_to<BS, const_buffer>)
321  
        requires (!std::convertible_to<BS, const_buffer>)
332  
    auto operator()(BS const& bs) const noexcept
322  
    auto operator()(BS const& bs) const noexcept
333  
    {
323  
    {
334  
        return std::ranges::end(bs);
324  
        return std::ranges::end(bs);
335  
    }
325  
    }
336  

326  

337  
    template<ConstBufferSequence BS>
327  
    template<ConstBufferSequence BS>
338  
        requires (!std::convertible_to<BS, const_buffer>)
328  
        requires (!std::convertible_to<BS, const_buffer>)
339  
    auto operator()(BS& bs) const noexcept
329  
    auto operator()(BS& bs) const noexcept
340  
    {
330  
    {
341  
        return std::ranges::end(bs);
331  
        return std::ranges::end(bs);
342  
    }
332  
    }
343  
} end {};
333  
} end {};
344 -
//------------------------------------------------------------------------------
 
345 -

 
346  

334  

347  
template<ConstBufferSequence CB>
335  
template<ConstBufferSequence CB>
348  
std::size_t
336  
std::size_t
349  
tag_invoke(
337  
tag_invoke(
350  
    size_tag const&,
338  
    size_tag const&,
351  
    CB const& bs) noexcept
339  
    CB const& bs) noexcept
352  
{
340  
{
353  
    std::size_t n = 0;
341  
    std::size_t n = 0;
354  
    auto const e = end(bs);
342  
    auto const e = end(bs);
355  
    for(auto it = begin(bs); it != e; ++it)
343  
    for(auto it = begin(bs); it != e; ++it)
356  
        n += const_buffer(*it).size();
344  
        n += const_buffer(*it).size();
357  
    return n;
345  
    return n;
358  
}
346  
}
359 -
//------------------------------------------------------------------------------
 
360 -

 
361  

347  

362  
/** Return the total byte count across all buffers in a sequence.
348  
/** Return the total byte count across all buffers in a sequence.
363  

349  

364  
    Sums the `size()` of each buffer in the sequence. This differs
350  
    Sums the `size()` of each buffer in the sequence. This differs
365  
    from `buffer_length` which counts the number of buffer elements.
351  
    from `buffer_length` which counts the number of buffer elements.
366  

352  

367  
    @par Example
353  
    @par Example
368  
    @code
354  
    @code
369  
    std::array<mutable_buffer, 2> bufs = { ... };
355  
    std::array<mutable_buffer, 2> bufs = { ... };
370  
    std::size_t total = buffer_size( bufs );  // sum of both sizes
356  
    std::size_t total = buffer_size( bufs );  // sum of both sizes
371  
    @endcode
357  
    @endcode
372  
*/
358  
*/
373  
constexpr struct buffer_size_mrdocs_workaround_t
359  
constexpr struct buffer_size_mrdocs_workaround_t
374  
{
360  
{
375  
    template<ConstBufferSequence CB>
361  
    template<ConstBufferSequence CB>
376  
    constexpr std::size_t operator()(
362  
    constexpr std::size_t operator()(
377  
        CB const& bs) const noexcept
363  
        CB const& bs) const noexcept
378  
    {
364  
    {
379  
        return tag_invoke(size_tag{}, bs);
365  
        return tag_invoke(size_tag{}, bs);
380  
    }
366  
    }
381  
} buffer_size {};
367  
} buffer_size {};
382  

368  

383  
/** Check if a buffer sequence contains no data.
369  
/** Check if a buffer sequence contains no data.
384  

370  

385  
    @return `true` if all buffers have size zero or the sequence
371  
    @return `true` if all buffers have size zero or the sequence
386  
        is empty.
372  
        is empty.
387  
*/
373  
*/
388  
constexpr struct buffer_empty_mrdocs_workaround_t
374  
constexpr struct buffer_empty_mrdocs_workaround_t
389  
{
375  
{
390  
    template<ConstBufferSequence CB>
376  
    template<ConstBufferSequence CB>
391  
    constexpr bool operator()(
377  
    constexpr bool operator()(
392  
        CB const& bs) const noexcept
378  
        CB const& bs) const noexcept
393  
    {
379  
    {
394  
        auto it = begin(bs);
380  
        auto it = begin(bs);
395  
        auto const end_ = end(bs);
381  
        auto const end_ = end(bs);
396  
        while(it != end_)
382  
        while(it != end_)
397  
        {
383  
        {
398  
            const_buffer b(*it++);
384  
            const_buffer b(*it++);
399  
            if(b.size() != 0)
385  
            if(b.size() != 0)
400  
                return false;
386  
                return false;
401  
        }
387  
        }
402  
        return true;
388  
        return true;
403  
    }
389  
    }
404 -

 
405 -
//-----------------------------------------------
 
406  
} buffer_empty {};
390  
} buffer_empty {};
407  

391  

408  
namespace detail {
392  
namespace detail {
409  

393  

410  
template<class It>
394  
template<class It>
411  
auto
395  
auto
412  
length_impl(It first, It last, int)
396  
length_impl(It first, It last, int)
413  
    -> decltype(static_cast<std::size_t>(last - first))
397  
    -> decltype(static_cast<std::size_t>(last - first))
414  
{
398  
{
415  
    return static_cast<std::size_t>(last - first);
399  
    return static_cast<std::size_t>(last - first);
416  
}
400  
}
417  

401  

418  
template<class It>
402  
template<class It>
419  
std::size_t
403  
std::size_t
420  
length_impl(It first, It last, long)
404  
length_impl(It first, It last, long)
421  
{
405  
{
422  
    std::size_t n = 0;
406  
    std::size_t n = 0;
423  
    while(first != last)
407  
    while(first != last)
424  
    {
408  
    {
425  
        ++first;
409  
        ++first;
426  
        ++n;
410  
        ++n;
427  
    }
411  
    }
428  
    return n;
412  
    return n;
429  
}
413  
}
430  

414  

431  
} // detail
415  
} // detail
432  

416  

433  
/** Return the number of buffer elements in a sequence.
417  
/** Return the number of buffer elements in a sequence.
434  

418  

435  
    Counts the number of individual buffer objects, not bytes.
419  
    Counts the number of individual buffer objects, not bytes.
436  
    For a single buffer, returns 1. For a range, returns the
420  
    For a single buffer, returns 1. For a range, returns the
437  
    distance from `begin` to `end`.
421  
    distance from `begin` to `end`.
438  

422  

439  
    @see buffer_size
423  
    @see buffer_size
440  
*/
424  
*/
441  
template<ConstBufferSequence CB>
425  
template<ConstBufferSequence CB>
442  
std::size_t
426  
std::size_t
443  
buffer_length(CB const& bs)
427  
buffer_length(CB const& bs)
444  
{
428  
{
445  
    return detail::length_impl(
429  
    return detail::length_impl(
446  
        begin(bs), end(bs), 0);
430  
        begin(bs), end(bs), 0);
447  
}
431  
}
448  

432  

449  
/// Alias for `mutable_buffer` or `const_buffer` based on sequence type.
433  
/// Alias for `mutable_buffer` or `const_buffer` based on sequence type.
450  
template<typename BS>
434  
template<typename BS>
451  
using buffer_type = std::conditional_t<
435  
using buffer_type = std::conditional_t<
452  
    MutableBufferSequence<BS>,
436  
    MutableBufferSequence<BS>,
453  
    mutable_buffer, const_buffer>;
437  
    mutable_buffer, const_buffer>;
454  

438  

455  
} // capy
439  
} // capy
456  
} // boost
440  
} // boost
457  

441  

458  
#endif
442  
#endif