include/boost/capy/buffers/make_buffer.hpp

100.0% Lines (47 / 47) 97.7% Functions (42 / 43)
make_buffer.hpp
f(x) Functions (43)
Function Calls Lines Blocks
boost::capy::make_buffer(boost::capy::mutable_buffer const&) :39 1x 100.0% 100.0% boost::capy::make_buffer(boost::capy::mutable_buffer const&, unsigned long) :55 2x 100.0% 100.0% boost::capy::make_buffer(void*, unsigned long) :73 727x 100.0% 100.0% boost::capy::make_buffer(void*, unsigned long, unsigned long) :92 2x 100.0% 100.0% boost::capy::make_buffer(boost::capy::const_buffer const&) :109 1x 100.0% 100.0% boost::capy::make_buffer(boost::capy::const_buffer const&, unsigned long) :125 2x 100.0% 100.0% boost::capy::make_buffer(void const*, unsigned long) :143 1x 100.0% 100.0% boost::capy::make_buffer(void const*, unsigned long, unsigned long) :162 2x 100.0% 100.0% boost::capy::const_buffer boost::capy::make_buffer<char, std::char_traits<char> >(std::basic_string_view<char, std::char_traits<char> >) :185 56x 100.0% 100.0% boost::capy::const_buffer boost::capy::make_buffer<wchar_t, std::char_traits<wchar_t> >(std::basic_string_view<wchar_t, std::char_traits<wchar_t> >) :185 1x 100.0% 88.0% boost::capy::const_buffer boost::capy::make_buffer<char, std::char_traits<char> >(std::basic_string_view<char, std::char_traits<char> >, unsigned long) :206 2x 100.0% 91.0% boost::capy::mutable_buffer boost::capy::make_buffer<boost::span<char, 18446744073709551615ul> >(boost::span<char, 18446744073709551615ul>&&) :264 1x 100.0% 88.0% boost::capy::mutable_buffer boost::capy::make_buffer<boost::span<char, 18446744073709551615ul>&>(boost::span<char, 18446744073709551615ul>&) :264 1x 100.0% 88.0% boost::capy::mutable_buffer boost::capy::make_buffer<char (&) [1024]>(char (&) [1024]) :264 0 0.0% 0.0% boost::capy::mutable_buffer boost::capy::make_buffer<char (&) [10]>(char (&) [10]) :264 1x 100.0% 88.0% boost::capy::mutable_buffer boost::capy::make_buffer<char (&) [32]>(char (&) [32]) :264 137x 100.0% 88.0% boost::capy::mutable_buffer boost::capy::make_buffer<char (&) [3]>(char (&) [3]) :264 38x 100.0% 88.0% boost::capy::mutable_buffer boost::capy::make_buffer<char (&) [4]>(char (&) [4]) :264 39x 100.0% 88.0% boost::capy::mutable_buffer boost::capy::make_buffer<char (&) [5]>(char (&) [5]) :264 32x 100.0% 88.0% boost::capy::mutable_buffer boost::capy::make_buffer<int (&) [5]>(int (&) [5]) :264 1x 100.0% 88.0% boost::capy::mutable_buffer boost::capy::make_buffer<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) :264 169x 100.0% 100.0% boost::capy::mutable_buffer boost::capy::make_buffer<std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&>(std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&) :264 1x 100.0% 88.0% boost::capy::mutable_buffer boost::capy::make_buffer<std::array<char, 10ul>&>(std::array<char, 10ul>&) :264 1x 100.0% 88.0% boost::capy::mutable_buffer boost::capy::make_buffer<std::array<int, 5ul>&>(std::array<int, 5ul>&) :264 1x 100.0% 88.0% boost::capy::mutable_buffer boost::capy::make_buffer<std::span<char, 10ul>&>(std::span<char, 10ul>&) :264 1x 100.0% 88.0% boost::capy::mutable_buffer boost::capy::make_buffer<std::span<char, 18446744073709551615ul> >(std::span<char, 18446744073709551615ul>&&) :264 1x 100.0% 88.0% boost::capy::mutable_buffer boost::capy::make_buffer<std::span<char, 18446744073709551615ul>&>(std::span<char, 18446744073709551615ul>&) :264 1x 100.0% 88.0% boost::capy::mutable_buffer boost::capy::make_buffer<std::vector<char, std::allocator<char> >&>(std::vector<char, std::allocator<char> >&) :264 2x 100.0% 100.0% boost::capy::mutable_buffer boost::capy::make_buffer<std::vector<int, std::allocator<int> >&>(std::vector<int, std::allocator<int> >&) :264 1x 100.0% 88.0% boost::capy::mutable_buffer boost::capy::make_buffer<boost::span<char, 18446744073709551615ul>&>(boost::span<char, 18446744073709551615ul>&, unsigned long) :285 1x 100.0% 82.0% boost::capy::mutable_buffer boost::capy::make_buffer<char (&) [10]>(char (&) [10], unsigned long) :285 2x 100.0% 91.0% boost::capy::mutable_buffer boost::capy::make_buffer<char (&) [4]>(char (&) [4], unsigned long) :285 30x 100.0% 82.0% boost::capy::mutable_buffer boost::capy::make_buffer<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long) :285 2x 100.0% 91.0% boost::capy::mutable_buffer boost::capy::make_buffer<std::array<char, 10ul>&>(std::array<char, 10ul>&, unsigned long) :285 2x 100.0% 91.0% boost::capy::mutable_buffer boost::capy::make_buffer<std::span<char, 18446744073709551615ul>&>(std::span<char, 18446744073709551615ul>&, unsigned long) :285 2x 100.0% 91.0% boost::capy::mutable_buffer boost::capy::make_buffer<std::vector<char, std::allocator<char> >&>(std::vector<char, std::allocator<char> >&, unsigned long) :285 2x 100.0% 91.0% boost::capy::const_buffer boost::capy::make_buffer<boost::span<char const, 18446744073709551615ul> >(boost::span<char const, 18446744073709551615ul> const&) :315 1x 100.0% 88.0% boost::capy::const_buffer boost::capy::make_buffer<char [10]>(char const (&) [10]) :315 1x 100.0% 88.0% boost::capy::const_buffer boost::capy::make_buffer<char [6]>(char const (&) [6]) :315 1x 100.0% 88.0% boost::capy::const_buffer boost::capy::make_buffer<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) :315 48x 100.0% 88.0% boost::capy::const_buffer boost::capy::make_buffer<std::array<char, 10ul> >(std::array<char, 10ul> const&) :315 1x 100.0% 88.0% boost::capy::const_buffer boost::capy::make_buffer<std::span<char const, 18446744073709551615ul> >(std::span<char const, 18446744073709551615ul> const&) :315 1x 100.0% 88.0% boost::capy::const_buffer boost::capy::make_buffer<std::vector<char, std::allocator<char> > >(std::vector<char, std::allocator<char> > const&) :315 1x 100.0% 88.0%
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2026 Michael Vandeberg
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/capy
9 //
10
11 #ifndef BOOST_CAPY_BUFFERS_MAKE_BUFFER_HPP
12 #define BOOST_CAPY_BUFFERS_MAKE_BUFFER_HPP
13
14 #include <boost/capy/detail/config.hpp>
15 #include <boost/capy/buffers.hpp>
16 #include <array>
17 #include <cstdlib>
18 #include <iterator>
19 #include <ranges>
20 #include <span>
21 #include <string>
22 #include <string_view>
23 #include <type_traits>
24 #include <vector>
25
26 BOOST_CAPY_MSVC_WARNING_PUSH
27 BOOST_CAPY_MSVC_WARNING_DISABLE(4459)
28
29 namespace boost {
30 namespace capy {
31
32 /** Return the buffer unchanged.
33
34 @param b The buffer to return.
35 @return A copy of `b`, referring to the same storage.
36 */
37 [[nodiscard]] inline
38 mutable_buffer
39 1x make_buffer(
40 mutable_buffer const& b) noexcept
41 {
42 1x return b;
43 }
44
45 /** Return the buffer, clamped to a maximum size.
46
47 The result's size is the smaller of `b.size()` and `max_size`.
48
49 @param b The buffer to return.
50 @param max_size The maximum size, in bytes, of the result.
51 @return A buffer referring to the storage of `b`.
52 */
53 [[nodiscard]] inline
54 mutable_buffer
55 2x make_buffer(
56 mutable_buffer const& b,
57 std::size_t max_size) noexcept
58 {
59 5x return mutable_buffer(
60 b.data(),
61 5x b.size() < max_size ? b.size() : max_size);
62 }
63
64 /** Return a buffer referring to a region of memory.
65
66 @param data A pointer to the start of the region. The region
67 must outlive the returned buffer.
68 @param size The size of the region, in bytes.
69 @return A buffer referring to `[data, data + size)`.
70 */
71 [[nodiscard]] inline
72 mutable_buffer
73 727x make_buffer(
74 void* data,
75 std::size_t size) noexcept
76 {
77 727x return mutable_buffer(data, size);
78 }
79
80 /** Return a buffer referring to a region of memory, clamped to a maximum size.
81
82 The result's size is the smaller of `size` and `max_size`.
83
84 @param data A pointer to the start of the region. The region
85 must outlive the returned buffer.
86 @param size The size of the region, in bytes.
87 @param max_size The maximum size, in bytes, of the result.
88 @return A buffer referring to `data`.
89 */
90 [[nodiscard]] inline
91 mutable_buffer
92 2x make_buffer(
93 void* data,
94 std::size_t size,
95 std::size_t max_size) noexcept
96 {
97 2x return mutable_buffer(
98 data,
99 2x size < max_size ? size : max_size);
100 }
101
102 /** Return the buffer unchanged.
103
104 @param b The buffer to return.
105 @return A copy of `b`, referring to the same storage.
106 */
107 [[nodiscard]] inline
108 const_buffer
109 1x make_buffer(
110 const_buffer const& b) noexcept
111 {
112 1x return b;
113 }
114
115 /** Return the buffer, clamped to a maximum size.
116
117 The result's size is the smaller of `b.size()` and `max_size`.
118
119 @param b The buffer to return.
120 @param max_size The maximum size, in bytes, of the result.
121 @return A buffer referring to the storage of `b`.
122 */
123 [[nodiscard]] inline
124 const_buffer
125 2x make_buffer(
126 const_buffer const& b,
127 std::size_t max_size) noexcept
128 {
129 5x return const_buffer(
130 b.data(),
131 5x b.size() < max_size ? b.size() : max_size);
132 }
133
134 /** Return a buffer referring to a region of memory.
135
136 @param data A pointer to the start of the region. The region
137 must outlive the returned buffer.
138 @param size The size of the region, in bytes.
139 @return A buffer referring to `[data, data + size)`.
140 */
141 [[nodiscard]] inline
142 const_buffer
143 1x make_buffer(
144 void const* data,
145 std::size_t size) noexcept
146 {
147 1x return const_buffer(data, size);
148 }
149
150 /** Return a buffer referring to a region of memory, clamped to a maximum size.
151
152 The result's size is the smaller of `size` and `max_size`.
153
154 @param data A pointer to the start of the region. The region
155 must outlive the returned buffer.
156 @param size The size of the region, in bytes.
157 @param max_size The maximum size, in bytes, of the result.
158 @return A buffer referring to `data`.
159 */
160 [[nodiscard]] inline
161 const_buffer
162 2x make_buffer(
163 void const* data,
164 std::size_t size,
165 std::size_t max_size) noexcept
166 {
167 2x return const_buffer(
168 data,
169 2x size < max_size ? size : max_size);
170 }
171
172 // std::basic_string_view
173
174 /** Return a buffer from a `std::basic_string_view`.
175
176 The result's size, in bytes, is `data.size() * sizeof(CharT)`.
177
178 @param data The view whose characters are referenced. The
179 underlying storage must outlive the returned buffer.
180 @return A buffer referring to the view's storage.
181 */
182 template<class CharT, class Traits>
183 [[nodiscard]]
184 const_buffer
185 57x make_buffer(
186 std::basic_string_view<CharT, Traits> data) noexcept
187 {
188 167x return const_buffer(
189 111x data.size() ? data.data() : nullptr,
190 58x data.size() * sizeof(CharT));
191 }
192
193 /** Return a buffer from a `std::basic_string_view`, clamped to a maximum size.
194
195 The result's size is the smaller of `data.size() * sizeof(CharT)`
196 and `max_size`.
197
198 @param data The view whose characters are referenced. The
199 underlying storage must outlive the returned buffer.
200 @param max_size The maximum size, in bytes, of the result.
201 @return A buffer referring to the view's storage.
202 */
203 template<class CharT, class Traits>
204 [[nodiscard]]
205 const_buffer
206 2x make_buffer(
207 std::basic_string_view<CharT, Traits> data,
208 std::size_t max_size) noexcept
209 {
210 6x return const_buffer(
211 4x data.size() ? data.data() : nullptr,
212 2x data.size() * sizeof(CharT) < max_size
213 3x ? data.size() * sizeof(CharT) : max_size);
214 }
215
216 // Contiguous ranges
217
218 namespace detail {
219
220 template<class T>
221 concept non_buffer_contiguous_range =
222 std::ranges::contiguous_range<T> &&
223 std::ranges::sized_range<T> &&
224 !std::convertible_to<T, const_buffer> &&
225 !std::convertible_to<T, mutable_buffer> &&
226 std::is_trivially_copyable_v<std::ranges::range_value_t<T>>;
227
228 template<class T>
229 concept mutable_contiguous_range =
230 non_buffer_contiguous_range<T> &&
231 !std::is_const_v<std::remove_reference_t<
232 std::ranges::range_reference_t<T>>>;
233
234 template<class T>
235 concept const_contiguous_range =
236 non_buffer_contiguous_range<T> &&
237 std::is_const_v<std::remove_reference_t<
238 std::ranges::range_reference_t<T>>>;
239
240 } // detail
241
242 /** Return a buffer from a mutable contiguous range.
243
244 Accepts any sized, contiguous range of trivially-copyable,
245 non-const elements, whether passed as an lvalue or a temporary.
246 That includes `std::vector`, `std::array`, `std::string`,
247 `std::span`, `boost::span`, and built-in arrays. The returned buffer
248 refers to the range's storage, which must outlive the buffer.
249 Its size, in bytes, is `size() * sizeof(element)`.
250
251 A temporary container's storage ends with the full-expression; keep
252 a buffer made from one only within that expression. A temporary
253 view such as `std::span` is safe when its underlying storage
254 outlives the buffer.
255
256 @param data The range whose storage is referenced. It must
257 outlive the returned buffer.
258
259 @return A buffer referring to the range's storage.
260 */
261 template<detail::mutable_contiguous_range T>
262 [[nodiscard]]
263 mutable_buffer
264 727x make_buffer(T&& data) noexcept
265 {
266 2171x return mutable_buffer(
267 1450x std::ranges::size(data) ? std::ranges::data(data) : nullptr,
268 733x std::ranges::size(data) * sizeof(std::ranges::range_value_t<T>));
269 }
270
271 /** Return a buffer from a mutable contiguous range, clamped to a maximum size.
272
273 Like the unclamped overload, but the result is no larger than
274 `max_size` bytes. Its size is the smaller of
275 `size() * sizeof(element)` and `max_size`.
276
277 @param data The range whose storage is referenced. It must
278 outlive the returned buffer.
279 @param max_size The maximum size, in bytes, of the result.
280 @return A buffer referring to the range's storage.
281 */
282 template<detail::mutable_contiguous_range T>
283 [[nodiscard]]
284 mutable_buffer
285 41x make_buffer(
286 T&& data,
287 std::size_t max_size) noexcept
288 {
289 41x auto const n = std::ranges::size(data) * sizeof(std::ranges::range_value_t<T>);
290 87x return mutable_buffer(
291 82x std::ranges::size(data) ? std::ranges::data(data) : nullptr,
292 82x n < max_size ? n : max_size);
293 }
294
295 /** Return a buffer from a const contiguous range.
296
297 Accepts any sized, contiguous range of trivially-copyable
298 elements with const access, including const `std::vector`,
299 `std::array`, `std::string`, `std::span`, `boost::span`, and
300 string literals. The returned buffer refers to the range's
301 storage, which must outlive the buffer. Its size, in bytes,
302 is `size() * sizeof(element)`.
303
304 For a string literal the buffer includes the terminating null
305 character. Use `std::string_view` to exclude it.
306
307 @param data The range whose storage is referenced. It must
308 outlive the returned buffer.
309
310 @return A buffer referring to the range's storage.
311 */
312 template<detail::non_buffer_contiguous_range T>
313 [[nodiscard]]
314 const_buffer
315 55x make_buffer(T const& data) noexcept
316 {
317 165x return const_buffer(
318 110x std::ranges::size(data) ? std::ranges::data(data) : nullptr,
319 55x std::ranges::size(data) * sizeof(std::ranges::range_value_t<T>));
320 }
321
322 /** Return a buffer from a const contiguous range, clamped to a maximum size.
323
324 Like the unclamped overload, but the result is no larger than
325 `max_size` bytes. Its size is the smaller of
326 `size() * sizeof(element)` and `max_size`.
327
328 @param data The range whose storage is referenced. It must
329 outlive the returned buffer.
330 @param max_size The maximum size, in bytes, of the result.
331 @return A buffer referring to the range's storage.
332 */
333 template<detail::non_buffer_contiguous_range T>
334 [[nodiscard]]
335 const_buffer
336 367x make_buffer(
337 T const& data,
338 std::size_t max_size) noexcept
339 {
340 367x auto const n = std::ranges::size(data) * sizeof(std::ranges::range_value_t<T>);
341 739x return const_buffer(
342 734x std::ranges::size(data) ? std::ranges::data(data) : nullptr,
343 734x n < max_size ? n : max_size);
344 }
345
346 } // capy
347 } // boost
348
349 BOOST_CAPY_MSVC_WARNING_POP
350
351 #endif
352