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  

34  

 
35 +
//------------------------------------------------
 
36 +

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

39  

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

42  

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

44  

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

52  

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

56  

 
57 +
//------------------------------------------------
 
58 +

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

60  

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

65  

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

72  

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

76  

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

80  

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

84  

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

92  

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

98  

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

104  

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

106  

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

118  

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

130  

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

140  

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

148  

 
149 +
//------------------------------------------------
 
150 +

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

152  

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

157  

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

164  

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

168  

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

171  

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

175  

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

183  

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

191  

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

197  

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

203  

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

205  

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

217  

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

229  

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

239  

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

247  

 
248 +
//------------------------------------------------
 
249 +

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

251  

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

255  

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

259  

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

267  

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

269  

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

274  

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

278  

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

286  

 
287 +
//------------------------------------------------------------------------------
 
288 +

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

290  

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

301  

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

308  

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

316  

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

318  

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

329  

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

336  

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

344  

 
345 +
//------------------------------------------------------------------------------
 
346 +

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

359  

 
360 +
//------------------------------------------------------------------------------
 
361 +

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

363  

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

366  

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

382  

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

384  

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

 
406 +
//-----------------------------------------------
391  

407  

392  
namespace detail {
408  
namespace detail {
393  

409  

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

417  

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

430  

415  
} // detail
431  
} // detail
416  

432  

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

434  

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

438  

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

448  

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

454  

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

457  

442  
#endif
458  
#endif