1  
//
1  
//
2  
// Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2023 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_MAKE_BUFFER_HPP
10  
#ifndef BOOST_CAPY_BUFFERS_MAKE_BUFFER_HPP
11  
#define BOOST_CAPY_BUFFERS_MAKE_BUFFER_HPP
11  
#define BOOST_CAPY_BUFFERS_MAKE_BUFFER_HPP
12  

12  

13  
#include <boost/capy/detail/config.hpp>
13  
#include <boost/capy/detail/config.hpp>
14  
#include <boost/capy/buffers.hpp>
14  
#include <boost/capy/buffers.hpp>
15  
#include <array>
15  
#include <array>
16  
#include <cstdlib>
16  
#include <cstdlib>
17  
#include <iterator>
17  
#include <iterator>
18  
#include <ranges>
18  
#include <ranges>
19  
#include <span>
19  
#include <span>
20  
#include <string>
20  
#include <string>
21  
#include <string_view>
21  
#include <string_view>
22  
#include <type_traits>
22  
#include <type_traits>
23  
#include <vector>
23  
#include <vector>
24  

24  

25  
#ifdef _MSC_VER
25  
#ifdef _MSC_VER
26  
#pragma warning(push)
26  
#pragma warning(push)
27  
#pragma warning(disable: 4459)
27  
#pragma warning(disable: 4459)
28  
#endif
28  
#endif
29  

29  

30  
namespace boost {
30  
namespace boost {
31  
namespace capy {
31  
namespace capy {
32  

32  

33  
/** Return a buffer.
33  
/** Return a buffer.
34  
*/
34  
*/
35  
[[nodiscard]] inline
35  
[[nodiscard]] inline
36  
mutable_buffer
36  
mutable_buffer
37  
make_buffer(
37  
make_buffer(
38  
    mutable_buffer const& b) noexcept
38  
    mutable_buffer const& b) noexcept
39  
{
39  
{
40  
    return b;
40  
    return b;
41  
}
41  
}
42  

42  

43  
/** Return a buffer with a maximum size.
43  
/** Return a buffer with a maximum size.
44  
*/
44  
*/
45  
[[nodiscard]] inline
45  
[[nodiscard]] inline
46  
mutable_buffer
46  
mutable_buffer
47  
make_buffer(
47  
make_buffer(
48  
    mutable_buffer const& b,
48  
    mutable_buffer const& b,
49  
    std::size_t max_size) noexcept
49  
    std::size_t max_size) noexcept
50  
{
50  
{
51  
    return mutable_buffer(
51  
    return mutable_buffer(
52  
        b.data(),
52  
        b.data(),
53  
        b.size() < max_size ? b.size() : max_size);
53  
        b.size() < max_size ? b.size() : max_size);
54  
}
54  
}
55  

55  

56  
/** Return a buffer.
56  
/** Return a buffer.
57  
*/
57  
*/
58  
[[nodiscard]] inline
58  
[[nodiscard]] inline
59  
mutable_buffer
59  
mutable_buffer
60  
make_buffer(
60  
make_buffer(
61  
    void* data,
61  
    void* data,
62  
    std::size_t size) noexcept
62  
    std::size_t size) noexcept
63  
{
63  
{
64  
    return mutable_buffer(data, size);
64  
    return mutable_buffer(data, size);
65  
}
65  
}
66  

66  

67  
/** Return a buffer with a maximum size.
67  
/** Return a buffer with a maximum size.
68  
*/
68  
*/
69  
[[nodiscard]] inline
69  
[[nodiscard]] inline
70  
mutable_buffer
70  
mutable_buffer
71  
make_buffer(
71  
make_buffer(
72  
    void* data,
72  
    void* data,
73  
    std::size_t size,
73  
    std::size_t size,
74  
    std::size_t max_size) noexcept
74  
    std::size_t max_size) noexcept
75  
{
75  
{
76  
    return mutable_buffer(
76  
    return mutable_buffer(
77  
        data,
77  
        data,
78  
        size < max_size ? size : max_size);
78  
        size < max_size ? size : max_size);
79  
}
79  
}
80  

80  

81  
/** Return a buffer.
81  
/** Return a buffer.
82  
*/
82  
*/
83  
[[nodiscard]] inline
83  
[[nodiscard]] inline
84  
const_buffer
84  
const_buffer
85  
make_buffer(
85  
make_buffer(
86  
    const_buffer const& b) noexcept
86  
    const_buffer const& b) noexcept
87  
{
87  
{
88  
    return b;
88  
    return b;
89  
}
89  
}
90  

90  

91  
/** Return a buffer with a maximum size.
91  
/** Return a buffer with a maximum size.
92  
*/
92  
*/
93  
[[nodiscard]] inline
93  
[[nodiscard]] inline
94  
const_buffer
94  
const_buffer
95  
make_buffer(
95  
make_buffer(
96  
    const_buffer const& b,
96  
    const_buffer const& b,
97  
    std::size_t max_size) noexcept
97  
    std::size_t max_size) noexcept
98  
{
98  
{
99  
    return const_buffer(
99  
    return const_buffer(
100  
        b.data(),
100  
        b.data(),
101  
        b.size() < max_size ? b.size() : max_size);
101  
        b.size() < max_size ? b.size() : max_size);
102  
}
102  
}
103  

103  

104  
/** Return a buffer.
104  
/** Return a buffer.
105  
*/
105  
*/
106  
[[nodiscard]] inline
106  
[[nodiscard]] inline
107  
const_buffer
107  
const_buffer
108  
make_buffer(
108  
make_buffer(
109  
    void const* data,
109  
    void const* data,
110  
    std::size_t size) noexcept
110  
    std::size_t size) noexcept
111  
{
111  
{
112  
    return const_buffer(data, size);
112  
    return const_buffer(data, size);
113  
}
113  
}
114  

114  

115  
/** Return a buffer with a maximum size.
115  
/** Return a buffer with a maximum size.
116  
*/
116  
*/
117  
[[nodiscard]] inline
117  
[[nodiscard]] inline
118  
const_buffer
118  
const_buffer
119  
make_buffer(
119  
make_buffer(
120  
    void const* data,
120  
    void const* data,
121  
    std::size_t size,
121  
    std::size_t size,
122  
    std::size_t max_size) noexcept
122  
    std::size_t max_size) noexcept
123  
{
123  
{
124  
    return const_buffer(
124  
    return const_buffer(
125  
        data,
125  
        data,
126  
        size < max_size ? size : max_size);
126  
        size < max_size ? size : max_size);
127  
}
127  
}
128  

128  

129  
/** Return a buffer from a C-style array.
129  
/** Return a buffer from a C-style array.
130  
*/
130  
*/
131  
template<class T, std::size_t N>
131  
template<class T, std::size_t N>
132  
    requires std::is_trivially_copyable_v<T>
132  
    requires std::is_trivially_copyable_v<T>
133  
[[nodiscard]]
133  
[[nodiscard]]
134  
mutable_buffer
134  
mutable_buffer
135  
make_buffer(
135  
make_buffer(
136  
    T (&data)[N]) noexcept
136  
    T (&data)[N]) noexcept
137  
{
137  
{
138  
    return mutable_buffer(
138  
    return mutable_buffer(
139  
        data, N * sizeof(T));
139  
        data, N * sizeof(T));
140  
}
140  
}
141  

141  

142  
/** Return a buffer from a C-style array with a maximum size.
142  
/** Return a buffer from a C-style array with a maximum size.
143  
*/
143  
*/
144  
template<class T, std::size_t N>
144  
template<class T, std::size_t N>
145  
    requires std::is_trivially_copyable_v<T>
145  
    requires std::is_trivially_copyable_v<T>
146  
[[nodiscard]]
146  
[[nodiscard]]
147  
mutable_buffer
147  
mutable_buffer
148  
make_buffer(
148  
make_buffer(
149  
    T (&data)[N],
149  
    T (&data)[N],
150  
    std::size_t max_size) noexcept
150  
    std::size_t max_size) noexcept
151  
{
151  
{
152  
    return mutable_buffer(
152  
    return mutable_buffer(
153  
        data,
153  
        data,
154  
        N * sizeof(T) < max_size ? N * sizeof(T) : max_size);
154  
        N * sizeof(T) < max_size ? N * sizeof(T) : max_size);
155  
}
155  
}
156  

156  

157  
/** Return a buffer from a const C-style array.
157  
/** Return a buffer from a const C-style array.
158  
*/
158  
*/
159  
template<class T, std::size_t N>
159  
template<class T, std::size_t N>
160  
    requires std::is_trivially_copyable_v<T>
160  
    requires std::is_trivially_copyable_v<T>
161  
[[nodiscard]]
161  
[[nodiscard]]
162  
const_buffer
162  
const_buffer
163  
make_buffer(
163  
make_buffer(
164  
    T const (&data)[N]) noexcept
164  
    T const (&data)[N]) noexcept
165  
{
165  
{
166  
    return const_buffer(
166  
    return const_buffer(
167  
        data, N * sizeof(T));
167  
        data, N * sizeof(T));
168  
}
168  
}
169  

169  

170  
/** Return a buffer from a const C-style array with a maximum size.
170  
/** Return a buffer from a const C-style array with a maximum size.
171  
*/
171  
*/
172  
template<class T, std::size_t N>
172  
template<class T, std::size_t N>
173  
    requires std::is_trivially_copyable_v<T>
173  
    requires std::is_trivially_copyable_v<T>
174  
[[nodiscard]]
174  
[[nodiscard]]
175  
const_buffer
175  
const_buffer
176  
make_buffer(
176  
make_buffer(
177  
    T const (&data)[N],
177  
    T const (&data)[N],
178  
    std::size_t max_size) noexcept
178  
    std::size_t max_size) noexcept
179  
{
179  
{
180  
    return const_buffer(
180  
    return const_buffer(
181  
        data,
181  
        data,
182  
        N * sizeof(T) < max_size ? N * sizeof(T) : max_size);
182  
        N * sizeof(T) < max_size ? N * sizeof(T) : max_size);
183  
}
183  
}
184 -
//------------------------------------------------
 
185  

184  

186 -
//------------------------------------------------
 
187  
// std::array
185  
// std::array
188  

186  

189  
/** Return a buffer from a std::array.
187  
/** Return a buffer from a std::array.
190  
*/
188  
*/
191  
template<class T, std::size_t N>
189  
template<class T, std::size_t N>
192  
    requires std::is_trivially_copyable_v<T>
190  
    requires std::is_trivially_copyable_v<T>
193  
[[nodiscard]]
191  
[[nodiscard]]
194  
mutable_buffer
192  
mutable_buffer
195  
make_buffer(
193  
make_buffer(
196  
    std::array<T, N>& data) noexcept
194  
    std::array<T, N>& data) noexcept
197  
{
195  
{
198  
    return mutable_buffer(
196  
    return mutable_buffer(
199  
        data.data(), data.size() * sizeof(T));
197  
        data.data(), data.size() * sizeof(T));
200  
}
198  
}
201  

199  

202  
/** Return a buffer from a std::array with a maximum size.
200  
/** Return a buffer from a std::array with a maximum size.
203  
*/
201  
*/
204  
template<class T, std::size_t N>
202  
template<class T, std::size_t N>
205  
    requires std::is_trivially_copyable_v<T>
203  
    requires std::is_trivially_copyable_v<T>
206  
[[nodiscard]]
204  
[[nodiscard]]
207  
mutable_buffer
205  
mutable_buffer
208  
make_buffer(
206  
make_buffer(
209  
    std::array<T, N>& data,
207  
    std::array<T, N>& data,
210  
    std::size_t max_size) noexcept
208  
    std::size_t max_size) noexcept
211  
{
209  
{
212  
    return mutable_buffer(
210  
    return mutable_buffer(
213  
        data.data(),
211  
        data.data(),
214  
        data.size() * sizeof(T) < max_size
212  
        data.size() * sizeof(T) < max_size
215  
            ? data.size() * sizeof(T) : max_size);
213  
            ? data.size() * sizeof(T) : max_size);
216  
}
214  
}
217  

215  

218  
/** Return a buffer from a const std::array.
216  
/** Return a buffer from a const std::array.
219  
*/
217  
*/
220  
template<class T, std::size_t N>
218  
template<class T, std::size_t N>
221  
    requires std::is_trivially_copyable_v<T>
219  
    requires std::is_trivially_copyable_v<T>
222  
[[nodiscard]]
220  
[[nodiscard]]
223  
const_buffer
221  
const_buffer
224  
make_buffer(
222  
make_buffer(
225  
    std::array<T, N> const& data) noexcept
223  
    std::array<T, N> const& data) noexcept
226  
{
224  
{
227  
    return const_buffer(
225  
    return const_buffer(
228  
        data.data(), data.size() * sizeof(T));
226  
        data.data(), data.size() * sizeof(T));
229  
}
227  
}
230  

228  

231  
/** Return a buffer from a const std::array with a maximum size.
229  
/** Return a buffer from a const std::array with a maximum size.
232  
*/
230  
*/
233  
template<class T, std::size_t N>
231  
template<class T, std::size_t N>
234  
    requires std::is_trivially_copyable_v<T>
232  
    requires std::is_trivially_copyable_v<T>
235  
[[nodiscard]]
233  
[[nodiscard]]
236  
const_buffer
234  
const_buffer
237  
make_buffer(
235  
make_buffer(
238  
    std::array<T, N> const& data,
236  
    std::array<T, N> const& data,
239  
    std::size_t max_size) noexcept
237  
    std::size_t max_size) noexcept
240  
{
238  
{
241  
    return const_buffer(
239  
    return const_buffer(
242  
        data.data(),
240  
        data.data(),
243  
        data.size() * sizeof(T) < max_size
241  
        data.size() * sizeof(T) < max_size
244  
            ? data.size() * sizeof(T) : max_size);
242  
            ? data.size() * sizeof(T) : max_size);
245  
}
243  
}
246 -
//------------------------------------------------
 
247  

244  

248 -
//------------------------------------------------
 
249  
// std::vector
245  
// std::vector
250  

246  

251  
/** Return a buffer from a std::vector.
247  
/** Return a buffer from a std::vector.
252  
*/
248  
*/
253  
template<class T, class Allocator>
249  
template<class T, class Allocator>
254  
    requires std::is_trivially_copyable_v<T>
250  
    requires std::is_trivially_copyable_v<T>
255  
[[nodiscard]]
251  
[[nodiscard]]
256  
mutable_buffer
252  
mutable_buffer
257  
make_buffer(
253  
make_buffer(
258  
    std::vector<T, Allocator>& data) noexcept
254  
    std::vector<T, Allocator>& data) noexcept
259  
{
255  
{
260  
    return mutable_buffer(
256  
    return mutable_buffer(
261  
        data.size() ? data.data() : nullptr,
257  
        data.size() ? data.data() : nullptr,
262  
        data.size() * sizeof(T));
258  
        data.size() * sizeof(T));
263  
}
259  
}
264  

260  

265  
/** Return a buffer from a std::vector with a maximum size.
261  
/** Return a buffer from a std::vector with a maximum size.
266  
*/
262  
*/
267  
template<class T, class Allocator>
263  
template<class T, class Allocator>
268  
    requires std::is_trivially_copyable_v<T>
264  
    requires std::is_trivially_copyable_v<T>
269  
[[nodiscard]]
265  
[[nodiscard]]
270  
mutable_buffer
266  
mutable_buffer
271  
make_buffer(
267  
make_buffer(
272  
    std::vector<T, Allocator>& data,
268  
    std::vector<T, Allocator>& data,
273  
    std::size_t max_size) noexcept
269  
    std::size_t max_size) noexcept
274  
{
270  
{
275  
    return mutable_buffer(
271  
    return mutable_buffer(
276  
        data.size() ? data.data() : nullptr,
272  
        data.size() ? data.data() : nullptr,
277  
        data.size() * sizeof(T) < max_size
273  
        data.size() * sizeof(T) < max_size
278  
            ? data.size() * sizeof(T) : max_size);
274  
            ? data.size() * sizeof(T) : max_size);
279  
}
275  
}
280  

276  

281  
/** Return a buffer from a const std::vector.
277  
/** Return a buffer from a const std::vector.
282  
*/
278  
*/
283  
template<class T, class Allocator>
279  
template<class T, class Allocator>
284  
    requires std::is_trivially_copyable_v<T>
280  
    requires std::is_trivially_copyable_v<T>
285  
[[nodiscard]]
281  
[[nodiscard]]
286  
const_buffer
282  
const_buffer
287  
make_buffer(
283  
make_buffer(
288  
    std::vector<T, Allocator> const& data) noexcept
284  
    std::vector<T, Allocator> const& data) noexcept
289  
{
285  
{
290  
    return const_buffer(
286  
    return const_buffer(
291  
        data.size() ? data.data() : nullptr,
287  
        data.size() ? data.data() : nullptr,
292  
        data.size() * sizeof(T));
288  
        data.size() * sizeof(T));
293  
}
289  
}
294  

290  

295  
/** Return a buffer from a const std::vector with a maximum size.
291  
/** Return a buffer from a const std::vector with a maximum size.
296  
*/
292  
*/
297  
template<class T, class Allocator>
293  
template<class T, class Allocator>
298  
    requires std::is_trivially_copyable_v<T>
294  
    requires std::is_trivially_copyable_v<T>
299  
[[nodiscard]]
295  
[[nodiscard]]
300  
const_buffer
296  
const_buffer
301  
make_buffer(
297  
make_buffer(
302  
    std::vector<T, Allocator> const& data,
298  
    std::vector<T, Allocator> const& data,
303  
    std::size_t max_size) noexcept
299  
    std::size_t max_size) noexcept
304  
{
300  
{
305  
    return const_buffer(
301  
    return const_buffer(
306  
        data.size() ? data.data() : nullptr,
302  
        data.size() ? data.data() : nullptr,
307  
        data.size() * sizeof(T) < max_size
303  
        data.size() * sizeof(T) < max_size
308  
            ? data.size() * sizeof(T) : max_size);
304  
            ? data.size() * sizeof(T) : max_size);
309  
}
305  
}
310 -
//------------------------------------------------
 
311  

306  

312 -
//------------------------------------------------
 
313  
// std::basic_string
307  
// std::basic_string
314  

308  

315  
/** Return a buffer from a std::basic_string.
309  
/** Return a buffer from a std::basic_string.
316  
*/
310  
*/
317  
template<class CharT, class Traits, class Allocator>
311  
template<class CharT, class Traits, class Allocator>
318  
[[nodiscard]]
312  
[[nodiscard]]
319  
mutable_buffer
313  
mutable_buffer
320  
make_buffer(
314  
make_buffer(
321  
    std::basic_string<CharT, Traits, Allocator>& data) noexcept
315  
    std::basic_string<CharT, Traits, Allocator>& data) noexcept
322  
{
316  
{
323  
    return mutable_buffer(
317  
    return mutable_buffer(
324  
        data.size() ? &data[0] : nullptr,
318  
        data.size() ? &data[0] : nullptr,
325  
        data.size() * sizeof(CharT));
319  
        data.size() * sizeof(CharT));
326  
}
320  
}
327  

321  

328  
/** Return a buffer from a std::basic_string with a maximum size.
322  
/** Return a buffer from a std::basic_string with a maximum size.
329  
*/
323  
*/
330  
template<class CharT, class Traits, class Allocator>
324  
template<class CharT, class Traits, class Allocator>
331  
[[nodiscard]]
325  
[[nodiscard]]
332  
mutable_buffer
326  
mutable_buffer
333  
make_buffer(
327  
make_buffer(
334  
    std::basic_string<CharT, Traits, Allocator>& data,
328  
    std::basic_string<CharT, Traits, Allocator>& data,
335  
    std::size_t max_size) noexcept
329  
    std::size_t max_size) noexcept
336  
{
330  
{
337  
    return mutable_buffer(
331  
    return mutable_buffer(
338  
        data.size() ? &data[0] : nullptr,
332  
        data.size() ? &data[0] : nullptr,
339  
        data.size() * sizeof(CharT) < max_size
333  
        data.size() * sizeof(CharT) < max_size
340  
            ? data.size() * sizeof(CharT) : max_size);
334  
            ? data.size() * sizeof(CharT) : max_size);
341  
}
335  
}
342  

336  

343  
/** Return a buffer from a const std::basic_string.
337  
/** Return a buffer from a const std::basic_string.
344  
*/
338  
*/
345  
template<class CharT, class Traits, class Allocator>
339  
template<class CharT, class Traits, class Allocator>
346  
[[nodiscard]]
340  
[[nodiscard]]
347  
const_buffer
341  
const_buffer
348  
make_buffer(
342  
make_buffer(
349  
    std::basic_string<CharT, Traits, Allocator> const& data) noexcept
343  
    std::basic_string<CharT, Traits, Allocator> const& data) noexcept
350  
{
344  
{
351  
    return const_buffer(
345  
    return const_buffer(
352  
        data.data(),
346  
        data.data(),
353  
        data.size() * sizeof(CharT));
347  
        data.size() * sizeof(CharT));
354  
}
348  
}
355  

349  

356  
/** Return a buffer from a const std::basic_string with a maximum size.
350  
/** Return a buffer from a const std::basic_string with a maximum size.
357  
*/
351  
*/
358  
template<class CharT, class Traits, class Allocator>
352  
template<class CharT, class Traits, class Allocator>
359  
[[nodiscard]]
353  
[[nodiscard]]
360  
const_buffer
354  
const_buffer
361  
make_buffer(
355  
make_buffer(
362  
    std::basic_string<CharT, Traits, Allocator> const& data,
356  
    std::basic_string<CharT, Traits, Allocator> const& data,
363  
    std::size_t max_size) noexcept
357  
    std::size_t max_size) noexcept
364  
{
358  
{
365  
    return const_buffer(
359  
    return const_buffer(
366  
        data.data(),
360  
        data.data(),
367  
        data.size() * sizeof(CharT) < max_size
361  
        data.size() * sizeof(CharT) < max_size
368  
            ? data.size() * sizeof(CharT) : max_size);
362  
            ? data.size() * sizeof(CharT) : max_size);
369  
}
363  
}
370 -
//------------------------------------------------
 
371  

364  

372 -
//------------------------------------------------
 
373  
// std::basic_string_view
365  
// std::basic_string_view
374  

366  

375  
/** Return a buffer from a std::basic_string_view.
367  
/** Return a buffer from a std::basic_string_view.
376  
*/
368  
*/
377  
template<class CharT, class Traits>
369  
template<class CharT, class Traits>
378  
[[nodiscard]]
370  
[[nodiscard]]
379  
const_buffer
371  
const_buffer
380  
make_buffer(
372  
make_buffer(
381  
    std::basic_string_view<CharT, Traits> data) noexcept
373  
    std::basic_string_view<CharT, Traits> data) noexcept
382  
{
374  
{
383  
    return const_buffer(
375  
    return const_buffer(
384  
        data.size() ? data.data() : nullptr,
376  
        data.size() ? data.data() : nullptr,
385  
        data.size() * sizeof(CharT));
377  
        data.size() * sizeof(CharT));
386  
}
378  
}
387  

379  

388  
/** Return a buffer from a std::basic_string_view with a maximum size.
380  
/** Return a buffer from a std::basic_string_view with a maximum size.
389  
*/
381  
*/
390  
template<class CharT, class Traits>
382  
template<class CharT, class Traits>
391  
[[nodiscard]]
383  
[[nodiscard]]
392  
const_buffer
384  
const_buffer
393  
make_buffer(
385  
make_buffer(
394  
    std::basic_string_view<CharT, Traits> data,
386  
    std::basic_string_view<CharT, Traits> data,
395  
    std::size_t max_size) noexcept
387  
    std::size_t max_size) noexcept
396  
{
388  
{
397  
    return const_buffer(
389  
    return const_buffer(
398  
        data.size() ? data.data() : nullptr,
390  
        data.size() ? data.data() : nullptr,
399  
        data.size() * sizeof(CharT) < max_size
391  
        data.size() * sizeof(CharT) < max_size
400  
            ? data.size() * sizeof(CharT) : max_size);
392  
            ? data.size() * sizeof(CharT) : max_size);
401  
}
393  
}
402 -
//------------------------------------------------
 
403  

394  

404 -
//------------------------------------------------
 
405  
// std::span
395  
// std::span
406  

396  

407  
/** Return a buffer from a mutable std::span.
397  
/** Return a buffer from a mutable std::span.
408  
*/
398  
*/
409  
template<class T, std::size_t Extent>
399  
template<class T, std::size_t Extent>
410  
    requires (!std::is_const_v<T> && sizeof(T) == 1)
400  
    requires (!std::is_const_v<T> && sizeof(T) == 1)
411  
[[nodiscard]]
401  
[[nodiscard]]
412  
mutable_buffer
402  
mutable_buffer
413  
make_buffer(
403  
make_buffer(
414  
    std::span<T, Extent> data) noexcept
404  
    std::span<T, Extent> data) noexcept
415  
{
405  
{
416  
    return mutable_buffer(data.data(), data.size());
406  
    return mutable_buffer(data.data(), data.size());
417  
}
407  
}
418  

408  

419  
/** Return a buffer from a mutable std::span with a maximum size.
409  
/** Return a buffer from a mutable std::span with a maximum size.
420  
*/
410  
*/
421  
template<class T, std::size_t Extent>
411  
template<class T, std::size_t Extent>
422  
    requires (!std::is_const_v<T> && sizeof(T) == 1)
412  
    requires (!std::is_const_v<T> && sizeof(T) == 1)
423  
[[nodiscard]]
413  
[[nodiscard]]
424  
mutable_buffer
414  
mutable_buffer
425  
make_buffer(
415  
make_buffer(
426  
    std::span<T, Extent> data,
416  
    std::span<T, Extent> data,
427  
    std::size_t max_size) noexcept
417  
    std::size_t max_size) noexcept
428  
{
418  
{
429  
    return mutable_buffer(
419  
    return mutable_buffer(
430  
        data.data(),
420  
        data.data(),
431  
        data.size() < max_size ? data.size() : max_size);
421  
        data.size() < max_size ? data.size() : max_size);
432  
}
422  
}
433  

423  

434  
/** Return a buffer from a const std::span.
424  
/** Return a buffer from a const std::span.
435  
*/
425  
*/
436  
template<class T, std::size_t Extent>
426  
template<class T, std::size_t Extent>
437  
    requires (sizeof(T) == 1)
427  
    requires (sizeof(T) == 1)
438  
[[nodiscard]]
428  
[[nodiscard]]
439  
const_buffer
429  
const_buffer
440  
make_buffer(
430  
make_buffer(
441  
    std::span<T const, Extent> data) noexcept
431  
    std::span<T const, Extent> data) noexcept
442  
{
432  
{
443  
    return const_buffer(data.data(), data.size());
433  
    return const_buffer(data.data(), data.size());
444  
}
434  
}
445  

435  

446  
/** Return a buffer from a const std::span with a maximum size.
436  
/** Return a buffer from a const std::span with a maximum size.
447  
*/
437  
*/
448  
template<class T, std::size_t Extent>
438  
template<class T, std::size_t Extent>
449  
    requires (sizeof(T) == 1)
439  
    requires (sizeof(T) == 1)
450  
[[nodiscard]]
440  
[[nodiscard]]
451  
const_buffer
441  
const_buffer
452  
make_buffer(
442  
make_buffer(
453  
    std::span<T const, Extent> data,
443  
    std::span<T const, Extent> data,
454  
    std::size_t max_size) noexcept
444  
    std::size_t max_size) noexcept
455  
{
445  
{
456  
    return const_buffer(
446  
    return const_buffer(
457  
        data.data(),
447  
        data.data(),
458  
        data.size() < max_size ? data.size() : max_size);
448  
        data.size() < max_size ? data.size() : max_size);
459  
}
449  
}
460 -
//------------------------------------------------
 
461  

450  

462 -
//------------------------------------------------
 
463  
// Contiguous ranges
451  
// Contiguous ranges
464  

452  

465  
namespace detail {
453  
namespace detail {
466  

454  

467  
template<class T>
455  
template<class T>
468  
concept non_buffer_contiguous_range =
456  
concept non_buffer_contiguous_range =
469  
    std::ranges::contiguous_range<T> &&
457  
    std::ranges::contiguous_range<T> &&
470  
    std::ranges::sized_range<T> &&
458  
    std::ranges::sized_range<T> &&
471  
    !std::convertible_to<T, const_buffer> &&
459  
    !std::convertible_to<T, const_buffer> &&
472  
    !std::convertible_to<T, mutable_buffer> &&
460  
    !std::convertible_to<T, mutable_buffer> &&
473  
    std::is_trivially_copyable_v<std::ranges::range_value_t<T>>;
461  
    std::is_trivially_copyable_v<std::ranges::range_value_t<T>>;
474  

462  

475  
template<class T>
463  
template<class T>
476  
concept mutable_contiguous_range =
464  
concept mutable_contiguous_range =
477  
    non_buffer_contiguous_range<T> &&
465  
    non_buffer_contiguous_range<T> &&
478  
    !std::is_const_v<std::remove_reference_t<
466  
    !std::is_const_v<std::remove_reference_t<
479  
        std::ranges::range_reference_t<T>>>;
467  
        std::ranges::range_reference_t<T>>>;
480  

468  

481  
template<class T>
469  
template<class T>
482  
concept const_contiguous_range =
470  
concept const_contiguous_range =
483  
    non_buffer_contiguous_range<T> &&
471  
    non_buffer_contiguous_range<T> &&
484  
    std::is_const_v<std::remove_reference_t<
472  
    std::is_const_v<std::remove_reference_t<
485  
        std::ranges::range_reference_t<T>>>;
473  
        std::ranges::range_reference_t<T>>>;
486  

474  

487  
} // detail
475  
} // detail
488  

476  

489  
/** Return a buffer from a mutable contiguous range.
477  
/** Return a buffer from a mutable contiguous range.
490  
*/
478  
*/
491  
template<detail::mutable_contiguous_range T>
479  
template<detail::mutable_contiguous_range T>
492  
[[nodiscard]]
480  
[[nodiscard]]
493  
mutable_buffer
481  
mutable_buffer
494  
make_buffer(T& data) noexcept
482  
make_buffer(T& data) noexcept
495  
{
483  
{
496  
    return mutable_buffer(
484  
    return mutable_buffer(
497  
        std::ranges::size(data) ? std::ranges::data(data) : nullptr,
485  
        std::ranges::size(data) ? std::ranges::data(data) : nullptr,
498  
        std::ranges::size(data) * sizeof(std::ranges::range_value_t<T>));
486  
        std::ranges::size(data) * sizeof(std::ranges::range_value_t<T>));
499  
}
487  
}
500  

488  

501  
/** Return a buffer from a mutable contiguous range with a maximum size.
489  
/** Return a buffer from a mutable contiguous range with a maximum size.
502  
*/
490  
*/
503  
template<detail::mutable_contiguous_range T>
491  
template<detail::mutable_contiguous_range T>
504  
[[nodiscard]]
492  
[[nodiscard]]
505  
mutable_buffer
493  
mutable_buffer
506  
make_buffer(
494  
make_buffer(
507  
    T& data,
495  
    T& data,
508  
    std::size_t max_size) noexcept
496  
    std::size_t max_size) noexcept
509  
{
497  
{
510  
    auto const n = std::ranges::size(data) * sizeof(std::ranges::range_value_t<T>);
498  
    auto const n = std::ranges::size(data) * sizeof(std::ranges::range_value_t<T>);
511  
    return mutable_buffer(
499  
    return mutable_buffer(
512  
        std::ranges::size(data) ? std::ranges::data(data) : nullptr,
500  
        std::ranges::size(data) ? std::ranges::data(data) : nullptr,
513  
        n < max_size ? n : max_size);
501  
        n < max_size ? n : max_size);
514  
}
502  
}
515  

503  

516  
/** Return a buffer from a const contiguous range.
504  
/** Return a buffer from a const contiguous range.
517  
*/
505  
*/
518  
template<detail::non_buffer_contiguous_range T>
506  
template<detail::non_buffer_contiguous_range T>
519  
[[nodiscard]]
507  
[[nodiscard]]
520  
const_buffer
508  
const_buffer
521  
make_buffer(T const& data) noexcept
509  
make_buffer(T const& data) noexcept
522  
{
510  
{
523  
    return const_buffer(
511  
    return const_buffer(
524  
        std::ranges::size(data) ? std::ranges::data(data) : nullptr,
512  
        std::ranges::size(data) ? std::ranges::data(data) : nullptr,
525  
        std::ranges::size(data) * sizeof(std::ranges::range_value_t<T>));
513  
        std::ranges::size(data) * sizeof(std::ranges::range_value_t<T>));
526  
}
514  
}
527  

515  

528  
/** Return a buffer from a const contiguous range with a maximum size.
516  
/** Return a buffer from a const contiguous range with a maximum size.
529  
*/
517  
*/
530  
template<detail::non_buffer_contiguous_range T>
518  
template<detail::non_buffer_contiguous_range T>
531  
[[nodiscard]]
519  
[[nodiscard]]
532  
const_buffer
520  
const_buffer
533  
make_buffer(
521  
make_buffer(
534  
    T const& data,
522  
    T const& data,
535  
    std::size_t max_size) noexcept
523  
    std::size_t max_size) noexcept
536  
{
524  
{
537  
    auto const n = std::ranges::size(data) * sizeof(std::ranges::range_value_t<T>);
525  
    auto const n = std::ranges::size(data) * sizeof(std::ranges::range_value_t<T>);
538  
    return const_buffer(
526  
    return const_buffer(
539  
        std::ranges::size(data) ? std::ranges::data(data) : nullptr,
527  
        std::ranges::size(data) ? std::ranges::data(data) : nullptr,
540  
        n < max_size ? n : max_size);
528  
        n < max_size ? n : max_size);
541  
}
529  
}
542  

530  

543  
} // capy
531  
} // capy
544  
} // boost
532  
} // boost
545  

533  

546  
#ifdef _MSC_VER
534  
#ifdef _MSC_VER
547  
#pragma warning(pop)
535  
#pragma warning(pop)
548  
#endif
536  
#endif
549  

537  

550  
#endif
538  
#endif