TLA Line data 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 HIT 1 : make_buffer(
40 : mutable_buffer const& b) noexcept
41 : {
42 1 : 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 2 : make_buffer(
56 : mutable_buffer const& b,
57 : std::size_t max_size) noexcept
58 : {
59 5 : return mutable_buffer(
60 : b.data(),
61 5 : 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 727 : make_buffer(
74 : void* data,
75 : std::size_t size) noexcept
76 : {
77 727 : 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 2 : make_buffer(
93 : void* data,
94 : std::size_t size,
95 : std::size_t max_size) noexcept
96 : {
97 2 : return mutable_buffer(
98 : data,
99 2 : 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 1 : make_buffer(
110 : const_buffer const& b) noexcept
111 : {
112 1 : 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 2 : make_buffer(
126 : const_buffer const& b,
127 : std::size_t max_size) noexcept
128 : {
129 5 : return const_buffer(
130 : b.data(),
131 5 : 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 1 : make_buffer(
144 : void const* data,
145 : std::size_t size) noexcept
146 : {
147 1 : 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 2 : make_buffer(
163 : void const* data,
164 : std::size_t size,
165 : std::size_t max_size) noexcept
166 : {
167 2 : return const_buffer(
168 : data,
169 2 : 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 57 : make_buffer(
186 : std::basic_string_view<CharT, Traits> data) noexcept
187 : {
188 167 : return const_buffer(
189 111 : data.size() ? data.data() : nullptr,
190 58 : 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 2 : make_buffer(
207 : std::basic_string_view<CharT, Traits> data,
208 : std::size_t max_size) noexcept
209 : {
210 6 : return const_buffer(
211 4 : data.size() ? data.data() : nullptr,
212 2 : data.size() * sizeof(CharT) < max_size
213 3 : ? 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 727 : make_buffer(T&& data) noexcept
265 : {
266 2171 : return mutable_buffer(
267 1450 : std::ranges::size(data) ? std::ranges::data(data) : nullptr,
268 733 : 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 41 : make_buffer(
286 : T&& data,
287 : std::size_t max_size) noexcept
288 : {
289 41 : auto const n = std::ranges::size(data) * sizeof(std::ranges::range_value_t<T>);
290 87 : return mutable_buffer(
291 82 : std::ranges::size(data) ? std::ranges::data(data) : nullptr,
292 82 : 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 55 : make_buffer(T const& data) noexcept
316 : {
317 165 : return const_buffer(
318 110 : std::ranges::size(data) ? std::ranges::data(data) : nullptr,
319 55 : 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 367 : make_buffer(
337 : T const& data,
338 : std::size_t max_size) noexcept
339 : {
340 367 : auto const n = std::ranges::size(data) * sizeof(std::ranges::range_value_t<T>);
341 739 : return const_buffer(
342 734 : std::ranges::size(data) ? std::ranges::data(data) : nullptr,
343 734 : n < max_size ? n : max_size);
344 : }
345 :
346 : } // capy
347 : } // boost
348 :
349 : BOOST_CAPY_MSVC_WARNING_POP
350 :
351 : #endif
|