Line data Source code
1 : //
2 : // Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
3 : //
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)
6 : //
7 : // Official repository: https://github.com/cppalliance/buffers
8 : //
9 :
10 : #ifndef BOOST_BUFFERS_COPY_HPP
11 : #define BOOST_BUFFERS_COPY_HPP
12 :
13 : #include <boost/buffers/detail/config.hpp>
14 : #include <boost/buffers/buffer.hpp>
15 : #include <cstring>
16 : #include <utility>
17 :
18 : namespace boost {
19 : namespace buffers {
20 :
21 : /** Copy the contents of a buffer sequence into another buffer sequence
22 :
23 : This function copies no more than `at_most` bytes from the constant buffer
24 : sequence denoted by `src` into the mutable buffer sequence denoted by `dest`.
25 :
26 : @par Constraints
27 : @code
28 : requires is_mutable_buffer_sequence_v<decltype(dest)> &&
29 : is_const_buffer_sequence_v<decltype(src)>;
30 : @endcode
31 :
32 : @return The number of bytes actually copied, which will be exactly equal to
33 : `std::min( size(dest), size(src), at_most )`.
34 :
35 : @param dest The destination buffer sequence
36 :
37 : @param src The source buffer sequence
38 : */
39 : constexpr struct copy_mrdocs_workaround_t
40 : {
41 : template<
42 : class MutableBufferSequence,
43 : class ConstBufferSequence>
44 : auto
45 600354 : operator()(
46 : MutableBufferSequence const& dest,
47 : ConstBufferSequence const& src,
48 : std::size_t at_most = std::size_t(-1)) const noexcept -> typename std::enable_if<
49 : is_mutable_buffer_sequence<MutableBufferSequence>::value &&
50 : is_const_buffer_sequence<ConstBufferSequence>::value, std::size_t>::type
51 : {
52 600354 : std::size_t total = 0;
53 600354 : std::size_t pos0 = 0;
54 600354 : std::size_t pos1 = 0;
55 600354 : auto const end0 = end(src);
56 600354 : auto const end1 = end(dest);
57 600354 : auto it0 = begin(src);
58 600354 : auto it1 = begin(dest);
59 600354 : while(
60 1620408 : total < at_most &&
61 2940567 : it0 != end0 &&
62 2 : it1 != end1)
63 : {
64 1026516 : const_buffer b0 = *it0;
65 1026516 : mutable_buffer b1 = *it1;
66 1026516 : b0 += pos0;
67 1026516 : b1 += pos1;
68 : std::size_t const amount =
69 3079548 : [&]
70 : {
71 1026516 : std::size_t n = b0.size();
72 1026516 : if( n > b1.size())
73 23653 : n = b1.size();
74 1026516 : if( n > at_most - total)
75 4878 : n = at_most - total;
76 1026516 : if(n != 0)
77 772377 : std::memcpy(
78 : b1.data(),
79 : b0.data(),
80 : n);
81 1026516 : return n;
82 1026516 : }();
83 1026516 : total += amount;
84 1026516 : if(amount == b1.size())
85 : {
86 32093 : ++it1;
87 32093 : pos1 = 0;
88 : }
89 : else
90 : {
91 994423 : pos1 += amount;
92 : }
93 1026516 : if(amount == b0.size())
94 : {
95 999401 : ++it0;
96 999401 : pos0 = 0;
97 : }
98 : else
99 : {
100 27115 : pos0 += amount;
101 : }
102 : }
103 600560 : return total;
104 206 : }
105 : } copy {};
106 :
107 : } // buffers
108 : } // boost
109 :
110 : #endif
|