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 -
namespace detail {
 
35 -

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

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

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

 
60 -
    T* p_ = nullptr;
 
61 -
    std::size_t n_ = 0;
 
62 -
};
 
63 -

 
64 -
} // detail
 
65 -

 
66  

34  

67  
//------------------------------------------------
35  
//------------------------------------------------
68  

36  

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

39  

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

42  

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

44  

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

52  

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

56  

89  
//------------------------------------------------
57  
//------------------------------------------------
90  

58  

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

60  

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

65  

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

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

76  

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

80  

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

84  

115  
    /// Construct from pointer and size.
85  
    /// Construct from pointer and size.
116  
    constexpr mutable_buffer(
86  
    constexpr mutable_buffer(
117  
        void* data, std::size_t size) noexcept
87  
        void* data, std::size_t size) noexcept
118 -
        : basic_buffer<unsigned char>(
88 +
        : p_(static_cast<unsigned char*>(data))
119 -
            static_cast<unsigned char*>(data), size)
89 +
        , n_(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())
 
131  
    {
90  
    {
132  
    }
91  
    }
133  

92  

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

98  

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

104  

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

106  

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

118  

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

130  

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

140  

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

148  

190  
//------------------------------------------------
149  
//------------------------------------------------
191  

150  

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

152  

194  
    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.
195  
    Does not own the memory. Satisfies `ConstBufferSequence` (as a
154  
    Does not own the memory. Satisfies `ConstBufferSequence` (as a
196  
    single-element sequence). Implicitly constructible from
155  
    single-element sequence). Implicitly constructible from
197  
    `mutable_buffer`.
156  
    `mutable_buffer`.
198  

157  

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

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

168  

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

171  

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

175  

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

183  

223  
    /// Construct from mutable_buffer.
184  
    /// Construct from mutable_buffer.
224  
    constexpr const_buffer(
185  
    constexpr const_buffer(
225  
        mutable_buffer const& b) noexcept
186  
        mutable_buffer const& b) noexcept
226 -
        : basic_buffer<unsigned char const>(
187 +
        : p_(static_cast<unsigned char const*>(b.data()))
227 -
            static_cast<unsigned char const*>(b.data()), b.size())
188 +
        , n_(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())
 
240  
    {
189  
    {
241  
    }
190  
    }
242  

191  

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

197  

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

203  

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

205  

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

217  

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

229  

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

239  

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

247  

299  
//------------------------------------------------
248  
//------------------------------------------------
300  

249  

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

251  

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

255  

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

259  

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

267  

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

269  

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

274  

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

278  

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

286  

338  
//------------------------------------------------------------------------------
287  
//------------------------------------------------------------------------------
339  

288  

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

290  

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

301  

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

308  

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

316  

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

318  

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

329  

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

336  

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

344  

396  
//------------------------------------------------------------------------------
345  
//------------------------------------------------------------------------------
397  

346  

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

359  

411  
//------------------------------------------------------------------------------
360  
//------------------------------------------------------------------------------
412  

361  

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

363  

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

366  

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

382  

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

384  

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

405  

457  
//-----------------------------------------------
406  
//-----------------------------------------------
458  

407  

459  
namespace detail {
408  
namespace detail {
460  

409  

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

417  

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

430  

482  
} // detail
431  
} // detail
483  

432  

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

434  

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

438  

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

448  

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

454  

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

457  

509  
#endif
458  
#endif