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