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 +
namespace detail {
 
36 +

 
37 +
// satisfies Asio's buffer constructors, CANNOT be removed!
 
38 +
template<class T, std::size_t Extent = (std::size_t)(-1)>
 
39 +
class basic_buffer
 
40 +
{
 
41 +
    constexpr auto data() const noexcept ->
 
42 +
        std::conditional_t<std::is_const_v<T>, void const*, void*>
 
43 +
    {
 
44 +
        return p_;
 
45 +
    }
 
46 +

 
47 +
    constexpr std::size_t size() const noexcept
 
48 +
    {
 
49 +
        return n_;
 
50 +
    }
 
51 +

 
52 +
    friend class capy::const_buffer;
 
53 +
    friend class capy::mutable_buffer;
 
54 +
    friend class asio::const_buffer;
 
55 +
    friend class asio::mutable_buffer;
 
56 +
    basic_buffer() = default;
 
57 +
    constexpr basic_buffer(T* p, std::size_t n) noexcept : p_(p), n_(n) {}
 
58 +
    constexpr basic_buffer<T, (std::size_t)(-1)> subspan(
 
59 +
        std::size_t, std::size_t = (std::size_t)(-1)) const noexcept;
 
60 +

 
61 +
    T* p_ = nullptr;
 
62 +
    std::size_t n_ = 0;
 
63 +
};
 
64 +

 
65 +
} // detail
 
66 +

35  
//------------------------------------------------
67  
//------------------------------------------------
36  

68  

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

71  

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

74  

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

76  

45  
    Passed to `tag_invoke` overloads to specify which portion
77  
    Passed to `tag_invoke` overloads to specify which portion
46  
    of a buffer sequence to retain.
78  
    of a buffer sequence to retain.
47  
*/
79  
*/
48  
enum class slice_how
80  
enum class slice_how
49  
{
81  
{
50  
    /// Remove bytes from the front of the sequence.
82  
    /// Remove bytes from the front of the sequence.
51  
    remove_prefix,
83  
    remove_prefix,
52  

84  

53  
    /// Keep only the first N bytes.
85  
    /// Keep only the first N bytes.
54  
    keep_prefix
86  
    keep_prefix
55  
};
87  
};
56  

88  

57  
//------------------------------------------------
89  
//------------------------------------------------
58  

90  

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

92  

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

97  

66  
    @see const_buffer, MutableBufferSequence
98  
    @see const_buffer, MutableBufferSequence
67  
*/
99  
*/
68  
class mutable_buffer
100  
class mutable_buffer
 
101 +
    : public detail::basic_buffer<unsigned char>
69 -
    unsigned char* p_ = nullptr;
 
70 -
    std::size_t n_ = 0;
 
71 -

 
72  
{
102  
{
73  
public:
103  
public:
74  
    /// Construct an empty buffer.
104  
    /// Construct an empty buffer.
75  
    mutable_buffer() = default;
105  
    mutable_buffer() = default;
76  

106  

77  
    /// Copy constructor.
107  
    /// Copy constructor.
78  
    mutable_buffer(
108  
    mutable_buffer(
79  
        mutable_buffer const&) = default;
109  
        mutable_buffer const&) = default;
80  

110  

81  
    /// Copy assignment.
111  
    /// Copy assignment.
82  
    mutable_buffer& operator=(
112  
    mutable_buffer& operator=(
83  
        mutable_buffer const&) = default;
113  
        mutable_buffer const&) = default;
84  

114  

85  
    /// Construct from pointer and size.
115  
    /// Construct from pointer and size.
86  
    constexpr mutable_buffer(
116  
    constexpr mutable_buffer(
87  
        void* data, std::size_t size) noexcept
117  
        void* data, std::size_t size) noexcept
88 -
        : p_(static_cast<unsigned char*>(data))
118 +
        : basic_buffer<unsigned char>(
89 -
        , n_(size)
119 +
            static_cast<unsigned char*>(data), size)
 
120 +
    {
 
121 +
    }
 
122 +

 
123 +
    /// Construct from Asio mutable_buffer.
 
124 +
    template<class MutableBuffer>
 
125 +
        requires std::same_as<MutableBuffer, asio::mutable_buffer>
 
126 +
    constexpr mutable_buffer(
 
127 +
        MutableBuffer const& b) noexcept
 
128 +
        : basic_buffer<unsigned char>(
 
129 +
            static_cast<unsigned char*>(
 
130 +
                b.data()), b.size())
90  
    {
131  
    {
91  
    }
132  
    }
92  

133  

93  
    /// Return a pointer to the memory region.
134  
    /// Return a pointer to the memory region.
94  
    constexpr void* data() const noexcept
135  
    constexpr void* data() const noexcept
95  
    {
136  
    {
96  
        return p_;
137  
        return p_;
97  
    }
138  
    }
98  

139  

99  
    /// Return the size in bytes.
140  
    /// Return the size in bytes.
100  
    constexpr std::size_t size() const noexcept
141  
    constexpr std::size_t size() const noexcept
101  
    {
142  
    {
102  
        return n_;
143  
        return n_;
103  
    }
144  
    }
104  

145  

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

147  

107  
        @param n Bytes to skip. Clamped to `size()`.
148  
        @param n Bytes to skip. Clamped to `size()`.
108  
    */
149  
    */
109  
    mutable_buffer&
150  
    mutable_buffer&
110  
    operator+=(std::size_t n) noexcept
151  
    operator+=(std::size_t n) noexcept
111  
    {
152  
    {
112  
        if( n > n_)
153  
        if( n > n_)
113  
            n = n_;
154  
            n = n_;
114  
        p_ += n;
155  
        p_ += n;
115  
        n_ -= n;
156  
        n_ -= n;
116  
        return *this;
157  
        return *this;
117  
    }
158  
    }
118  

159  

119  
    /// Slice customization point for `tag_invoke`.
160  
    /// Slice customization point for `tag_invoke`.
120  
    friend
161  
    friend
121  
    void
162  
    void
122  
    tag_invoke(
163  
    tag_invoke(
123  
        slice_tag const&,
164  
        slice_tag const&,
124  
        mutable_buffer& b,
165  
        mutable_buffer& b,
125  
        slice_how how,
166  
        slice_how how,
126  
        std::size_t n) noexcept
167  
        std::size_t n) noexcept
127  
    {
168  
    {
128  
        b.do_slice(how, n);
169  
        b.do_slice(how, n);
129  
    }
170  
    }
130  

171  

131  
private:
172  
private:
132  
    void do_slice(
173  
    void do_slice(
133  
        slice_how how, std::size_t n) noexcept
174  
        slice_how how, std::size_t n) noexcept
134  
    {
175  
    {
135  
        switch(how)
176  
        switch(how)
136  
        {
177  
        {
137  
        case slice_how::remove_prefix:
178  
        case slice_how::remove_prefix:
138  
            *this += n;
179  
            *this += n;
139  
            return;
180  
            return;
140  

181  

141  
        case slice_how::keep_prefix:
182  
        case slice_how::keep_prefix:
142  
            if( n < n_)
183  
            if( n < n_)
143  
                n_ = n;
184  
                n_ = n;
144  
            return;
185  
            return;
145  
        }
186  
        }
146  
    }
187  
    }
147  
};
188  
};
148  

189  

149  
//------------------------------------------------
190  
//------------------------------------------------
150  

191  

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

193  

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

198  

158  
    @see mutable_buffer, ConstBufferSequence
199  
    @see mutable_buffer, ConstBufferSequence
159  
*/
200  
*/
160  
class const_buffer
201  
class const_buffer
 
202 +
    : public detail::basic_buffer<unsigned char const>
161 -
    unsigned char const* p_ = nullptr;
 
162 -
    std::size_t n_ = 0;
 
163 -

 
164  
{
203  
{
165  
public:
204  
public:
166  
    /// Construct an empty buffer.
205  
    /// Construct an empty buffer.
167  
    const_buffer() = default;
206  
    const_buffer() = default;
168  

207  

169  
    /// Copy constructor.
208  
    /// Copy constructor.
170  
    const_buffer(const_buffer const&) = default;
209  
    const_buffer(const_buffer const&) = default;
171  

210  

172  
    /// Copy assignment.
211  
    /// Copy assignment.
173  
    const_buffer& operator=(
212  
    const_buffer& operator=(
174  
        const_buffer const& other) = default;
213  
        const_buffer const& other) = default;
175  

214  

176  
    /// Construct from pointer and size.
215  
    /// Construct from pointer and size.
177  
    constexpr const_buffer(
216  
    constexpr const_buffer(
178  
        void const* data, std::size_t size) noexcept
217  
        void const* data, std::size_t size) noexcept
179 -
        : p_(static_cast<unsigned char const*>(data))
218 +
        : basic_buffer<unsigned char const>(
180 -
        , n_(size)
219 +
            static_cast<unsigned char const*>(data), size)
181  
    {
220  
    {
182  
    }
221  
    }
183  

222  

184  
    /// Construct from mutable_buffer.
223  
    /// Construct from mutable_buffer.
185  
    constexpr const_buffer(
224  
    constexpr const_buffer(
186  
        mutable_buffer const& b) noexcept
225  
        mutable_buffer const& b) noexcept
187 -
        : p_(static_cast<unsigned char const*>(b.data()))
226 +
        : basic_buffer<unsigned char const>(
188 -
        , n_(b.size())
227 +
            static_cast<unsigned char const*>(b.data()), b.size())
 
228 +
    {
 
229 +
    }
 
230 +

 
231 +
    /// Construct from Asio buffer types.
 
232 +
    template<class ConstBuffer>
 
233 +
        requires (std::same_as<ConstBuffer, asio::const_buffer> ||
 
234 +
                  std::same_as<ConstBuffer, asio::mutable_buffer>)
 
235 +
    constexpr const_buffer(
 
236 +
        ConstBuffer const& b) noexcept
 
237 +
        : basic_buffer<unsigned char const>(
 
238 +
            static_cast<unsigned char const*>(
 
239 +
                b.data()), b.size())
189  
    {
240  
    {
190  
    }
241  
    }
191  

242  

192  
    /// Return a pointer to the memory region.
243  
    /// Return a pointer to the memory region.
193  
    constexpr void const* data() const noexcept
244  
    constexpr void const* data() const noexcept
194  
    {
245  
    {
195  
        return p_;
246  
        return p_;
196  
    }
247  
    }
197  

248  

198  
    /// Return the size in bytes.
249  
    /// Return the size in bytes.
199  
    constexpr std::size_t size() const noexcept
250  
    constexpr std::size_t size() const noexcept
200  
    {
251  
    {
201  
        return n_;
252  
        return n_;
202  
    }
253  
    }
203  

254  

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

256  

206  
        @param n Bytes to skip. Clamped to `size()`.
257  
        @param n Bytes to skip. Clamped to `size()`.
207  
    */
258  
    */
208  
    const_buffer&
259  
    const_buffer&
209  
    operator+=(std::size_t n) noexcept
260  
    operator+=(std::size_t n) noexcept
210  
    {
261  
    {
211  
        if( n > n_)
262  
        if( n > n_)
212  
            n = n_;
263  
            n = n_;
213  
        p_ += n;
264  
        p_ += n;
214  
        n_ -= n;
265  
        n_ -= n;
215  
        return *this;
266  
        return *this;
216  
    }
267  
    }
217  

268  

218  
    /// Slice customization point for `tag_invoke`.
269  
    /// Slice customization point for `tag_invoke`.
219  
    friend
270  
    friend
220  
    void
271  
    void
221  
    tag_invoke(
272  
    tag_invoke(
222  
        slice_tag const&,
273  
        slice_tag const&,
223  
        const_buffer& b,
274  
        const_buffer& b,
224  
        slice_how how,
275  
        slice_how how,
225  
        std::size_t n) noexcept
276  
        std::size_t n) noexcept
226  
    {
277  
    {
227  
        b.do_slice(how, n);
278  
        b.do_slice(how, n);
228  
    }
279  
    }
229  

280  

230  
private:
281  
private:
231  
    void do_slice(
282  
    void do_slice(
232  
        slice_how how, std::size_t n) noexcept
283  
        slice_how how, std::size_t n) noexcept
233  
    {
284  
    {
234  
        switch(how)
285  
        switch(how)
235  
        {
286  
        {
236  
        case slice_how::remove_prefix:
287  
        case slice_how::remove_prefix:
237  
            *this += n;
288  
            *this += n;
238  
            return;
289  
            return;
239  

290  

240  
        case slice_how::keep_prefix:
291  
        case slice_how::keep_prefix:
241  
            if( n < n_)
292  
            if( n < n_)
242  
                n_ = n;
293  
                n_ = n;
243  
            return;
294  
            return;
244  
        }
295  
        }
245  
    }
296  
    }
246  
};
297  
};
247  

298  

248  
//------------------------------------------------
299  
//------------------------------------------------
249  

300  

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

302  

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

306  

256  
    @par Syntactic Requirements
307  
    @par Syntactic Requirements
257  
    @li Convertible to `const_buffer`, OR
308  
    @li Convertible to `const_buffer`, OR
258  
    @li A bidirectional range with value type convertible to `const_buffer`
309  
    @li A bidirectional range with value type convertible to `const_buffer`
259  

310  

260  
    @see const_buffer, MutableBufferSequence
311  
    @see const_buffer, MutableBufferSequence
261  
*/
312  
*/
262  
template<typename T>
313  
template<typename T>
263  
concept ConstBufferSequence =
314  
concept ConstBufferSequence =
264  
    std::is_convertible_v<T, const_buffer> || (
315  
    std::is_convertible_v<T, const_buffer> || (
265  
        std::ranges::bidirectional_range<T> &&
316  
        std::ranges::bidirectional_range<T> &&
266  
        std::is_convertible_v<std::ranges::range_value_t<T>, const_buffer>);
317  
        std::is_convertible_v<std::ranges::range_value_t<T>, const_buffer>);
267  

318  

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

320  

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

325  

275  
    @par Syntactic Requirements
326  
    @par Syntactic Requirements
276  
    @li Convertible to `mutable_buffer`, OR
327  
    @li Convertible to `mutable_buffer`, OR
277  
    @li A bidirectional range with value type convertible to `mutable_buffer`
328  
    @li A bidirectional range with value type convertible to `mutable_buffer`
278  

329  

279  
    @see mutable_buffer, ConstBufferSequence
330  
    @see mutable_buffer, ConstBufferSequence
280  
*/
331  
*/
281  
template<typename T>
332  
template<typename T>
282  
concept MutableBufferSequence =
333  
concept MutableBufferSequence =
283  
    std::is_convertible_v<T, mutable_buffer> || (
334  
    std::is_convertible_v<T, mutable_buffer> || (
284  
        std::ranges::bidirectional_range<T> &&
335  
        std::ranges::bidirectional_range<T> &&
285  
        std::is_convertible_v<std::ranges::range_value_t<T>, mutable_buffer>);
336  
        std::is_convertible_v<std::ranges::range_value_t<T>, mutable_buffer>);
286  

337  

287  
//------------------------------------------------------------------------------
338  
//------------------------------------------------------------------------------
288  

339  

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

341  

291  
    Handles single buffers and ranges uniformly. For a single buffer,
342  
    Handles single buffers and ranges uniformly. For a single buffer,
292  
    returns a pointer to it (forming a one-element range).
343  
    returns a pointer to it (forming a one-element range).
293  
*/
344  
*/
294  
constexpr struct begin_mrdocs_workaround_t
345  
constexpr struct begin_mrdocs_workaround_t
295  
{
346  
{
296  
    template<std::convertible_to<const_buffer> ConvertibleToBuffer>
347  
    template<std::convertible_to<const_buffer> ConvertibleToBuffer>
297  
    auto operator()(ConvertibleToBuffer const& b) const noexcept -> ConvertibleToBuffer const*
348  
    auto operator()(ConvertibleToBuffer const& b) const noexcept -> ConvertibleToBuffer const*
298  
    {
349  
    {
299  
        return std::addressof(b);
350  
        return std::addressof(b);
300  
    }
351  
    }
301  

352  

302  
    template<ConstBufferSequence BS>
353  
    template<ConstBufferSequence BS>
303  
        requires (!std::convertible_to<BS, const_buffer>)
354  
        requires (!std::convertible_to<BS, const_buffer>)
304  
    auto operator()(BS const& bs) const noexcept
355  
    auto operator()(BS const& bs) const noexcept
305  
    {
356  
    {
306  
        return std::ranges::begin(bs);
357  
        return std::ranges::begin(bs);
307  
    }
358  
    }
308  

359  

309  
    template<ConstBufferSequence BS>
360  
    template<ConstBufferSequence BS>
310  
        requires (!std::convertible_to<BS, const_buffer>)
361  
        requires (!std::convertible_to<BS, const_buffer>)
311  
    auto operator()(BS& bs) const noexcept
362  
    auto operator()(BS& bs) const noexcept
312  
    {
363  
    {
313  
        return std::ranges::begin(bs);
364  
        return std::ranges::begin(bs);
314  
    }
365  
    }
315  
} begin {};
366  
} begin {};
316  

367  

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

369  

319  
    Handles single buffers and ranges uniformly. For a single buffer,
370  
    Handles single buffers and ranges uniformly. For a single buffer,
320  
    returns a pointer one past it.
371  
    returns a pointer one past it.
321  
*/
372  
*/
322  
constexpr struct end_mrdocs_workaround_t
373  
constexpr struct end_mrdocs_workaround_t
323  
{
374  
{
324  
    template<std::convertible_to<const_buffer> ConvertibleToBuffer>
375  
    template<std::convertible_to<const_buffer> ConvertibleToBuffer>
325  
    auto operator()(ConvertibleToBuffer const& b) const noexcept -> ConvertibleToBuffer const*
376  
    auto operator()(ConvertibleToBuffer const& b) const noexcept -> ConvertibleToBuffer const*
326  
    {
377  
    {
327  
        return std::addressof(b) + 1;
378  
        return std::addressof(b) + 1;
328  
    }
379  
    }
329  

380  

330  
    template<ConstBufferSequence BS>
381  
    template<ConstBufferSequence BS>
331  
        requires (!std::convertible_to<BS, const_buffer>)
382  
        requires (!std::convertible_to<BS, const_buffer>)
332  
    auto operator()(BS const& bs) const noexcept
383  
    auto operator()(BS const& bs) const noexcept
333  
    {
384  
    {
334  
        return std::ranges::end(bs);
385  
        return std::ranges::end(bs);
335  
    }
386  
    }
336  

387  

337  
    template<ConstBufferSequence BS>
388  
    template<ConstBufferSequence BS>
338  
        requires (!std::convertible_to<BS, const_buffer>)
389  
        requires (!std::convertible_to<BS, const_buffer>)
339  
    auto operator()(BS& bs) const noexcept
390  
    auto operator()(BS& bs) const noexcept
340  
    {
391  
    {
341  
        return std::ranges::end(bs);
392  
        return std::ranges::end(bs);
342  
    }
393  
    }
343  
} end {};
394  
} end {};
344  

395  

345  
//------------------------------------------------------------------------------
396  
//------------------------------------------------------------------------------
346  

397  

347  
template<ConstBufferSequence CB>
398  
template<ConstBufferSequence CB>
348  
std::size_t
399  
std::size_t
349  
tag_invoke(
400  
tag_invoke(
350  
    size_tag const&,
401  
    size_tag const&,
351  
    CB const& bs) noexcept
402  
    CB const& bs) noexcept
352  
{
403  
{
353  
    std::size_t n = 0;
404  
    std::size_t n = 0;
354  
    auto const e = end(bs);
405  
    auto const e = end(bs);
355  
    for(auto it = begin(bs); it != e; ++it)
406  
    for(auto it = begin(bs); it != e; ++it)
356  
        n += const_buffer(*it).size();
407  
        n += const_buffer(*it).size();
357  
    return n;
408  
    return n;
358  
}
409  
}
359  

410  

360  
//------------------------------------------------------------------------------
411  
//------------------------------------------------------------------------------
361  

412  

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

414  

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

417  

367  
    @par Example
418  
    @par Example
368  
    @code
419  
    @code
369  
    std::array<mutable_buffer, 2> bufs = { ... };
420  
    std::array<mutable_buffer, 2> bufs = { ... };
370  
    std::size_t total = buffer_size( bufs );  // sum of both sizes
421  
    std::size_t total = buffer_size( bufs );  // sum of both sizes
371  
    @endcode
422  
    @endcode
372  
*/
423  
*/
373  
constexpr struct buffer_size_mrdocs_workaround_t
424  
constexpr struct buffer_size_mrdocs_workaround_t
374  
{
425  
{
375  
    template<ConstBufferSequence CB>
426  
    template<ConstBufferSequence CB>
376  
    constexpr std::size_t operator()(
427  
    constexpr std::size_t operator()(
377  
        CB const& bs) const noexcept
428  
        CB const& bs) const noexcept
378  
    {
429  
    {
379  
        return tag_invoke(size_tag{}, bs);
430  
        return tag_invoke(size_tag{}, bs);
380  
    }
431  
    }
381  
} buffer_size {};
432  
} buffer_size {};
382  

433  

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

435  

385  
    @return `true` if all buffers have size zero or the sequence
436  
    @return `true` if all buffers have size zero or the sequence
386  
        is empty.
437  
        is empty.
387  
*/
438  
*/
388  
constexpr struct buffer_empty_mrdocs_workaround_t
439  
constexpr struct buffer_empty_mrdocs_workaround_t
389  
{
440  
{
390  
    template<ConstBufferSequence CB>
441  
    template<ConstBufferSequence CB>
391  
    constexpr bool operator()(
442  
    constexpr bool operator()(
392  
        CB const& bs) const noexcept
443  
        CB const& bs) const noexcept
393  
    {
444  
    {
394  
        auto it = begin(bs);
445  
        auto it = begin(bs);
395  
        auto const end_ = end(bs);
446  
        auto const end_ = end(bs);
396  
        while(it != end_)
447  
        while(it != end_)
397  
        {
448  
        {
398  
            const_buffer b(*it++);
449  
            const_buffer b(*it++);
399  
            if(b.size() != 0)
450  
            if(b.size() != 0)
400  
                return false;
451  
                return false;
401  
        }
452  
        }
402  
        return true;
453  
        return true;
403  
    }
454  
    }
404  
} buffer_empty {};
455  
} buffer_empty {};
405  

456  

406  
//-----------------------------------------------
457  
//-----------------------------------------------
407  

458  

408  
namespace detail {
459  
namespace detail {
409  

460  

410  
template<class It>
461  
template<class It>
411  
auto
462  
auto
412  
length_impl(It first, It last, int)
463  
length_impl(It first, It last, int)
413  
    -> decltype(static_cast<std::size_t>(last - first))
464  
    -> decltype(static_cast<std::size_t>(last - first))
414  
{
465  
{
415  
    return static_cast<std::size_t>(last - first);
466  
    return static_cast<std::size_t>(last - first);
416  
}
467  
}
417  

468  

418  
template<class It>
469  
template<class It>
419  
std::size_t
470  
std::size_t
420  
length_impl(It first, It last, long)
471  
length_impl(It first, It last, long)
421  
{
472  
{
422  
    std::size_t n = 0;
473  
    std::size_t n = 0;
423  
    while(first != last)
474  
    while(first != last)
424  
    {
475  
    {
425  
        ++first;
476  
        ++first;
426  
        ++n;
477  
        ++n;
427  
    }
478  
    }
428  
    return n;
479  
    return n;
429  
}
480  
}
430  

481  

431  
} // detail
482  
} // detail
432  

483  

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

485  

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

489  

439  
    @see buffer_size
490  
    @see buffer_size
440  
*/
491  
*/
441  
template<ConstBufferSequence CB>
492  
template<ConstBufferSequence CB>
442  
std::size_t
493  
std::size_t
443  
buffer_length(CB const& bs)
494  
buffer_length(CB const& bs)
444  
{
495  
{
445  
    return detail::length_impl(
496  
    return detail::length_impl(
446  
        begin(bs), end(bs), 0);
497  
        begin(bs), end(bs), 0);
447  
}
498  
}
448  

499  

449  
/// Alias for `mutable_buffer` or `const_buffer` based on sequence type.
500  
/// Alias for `mutable_buffer` or `const_buffer` based on sequence type.
450  
template<typename BS>
501  
template<typename BS>
451  
using buffer_type = std::conditional_t<
502  
using buffer_type = std::conditional_t<
452  
    MutableBufferSequence<BS>,
503  
    MutableBufferSequence<BS>,
453  
    mutable_buffer, const_buffer>;
504  
    mutable_buffer, const_buffer>;
454  

505  

455  
} // capy
506  
} // capy
456  
} // boost
507  
} // boost
457  

508  

458  
#endif
509  
#endif