81
81
#include < span>
82
82
83
83
#include < utility> // std::move, std::swap
84
- #include < cstring > // std::memcpy
84
+ #include < algorithm > // std::copy
85
85
86
86
// TODO - add conecepts and / or requires
87
87
//
88
88
// modify the templated constructor that takes a buffer of any valid
89
- // byte type, add constraints; this makes the "reinterpret_cast" safe
89
+ // byte type, add constraints which makes the casting safer
90
90
91
91
namespace chops {
92
92
@@ -147,6 +147,16 @@ class mutable_shared_buffer {
147
147
mutable_shared_buffer () noexcept :
148
148
m_data{std::make_shared<byte_vec>(size_type (0 ))} { }
149
149
150
+ /* *
151
+ * @brief Construct by copying from a @c std::span of @c std::byte.
152
+ *
153
+ * @param sp @c std::byte span pointing to buffer of data. The data is
154
+ * copied into the internal buffer of the @c mutable_shared_buffer.
155
+ *
156
+ */
157
+ explicit mutable_shared_buffer (std::span<const std::byte> sp) :
158
+ m_data{std::make_shared<byte_vec>(sp.data (), sp.data ()+sp.size ())} { }
159
+
150
160
/* *
151
161
* @brief Move construct from a @c std::vector of @c std::bytes.
152
162
*
@@ -161,6 +171,7 @@ class mutable_shared_buffer {
161
171
m_data{std::make_shared<byte_vec>(size_type (0 ))} {
162
172
*m_data = std::move (bv);
163
173
}
174
+
164
175
/* *
165
176
* @brief Construct a @c mutable_shared_buffer with an initial size, contents
166
177
* set to zero.
@@ -171,18 +182,9 @@ class mutable_shared_buffer {
171
182
*
172
183
* @param sz Size for internal @c std::byte buffer.
173
184
*/
174
- explicit mutable_shared_buffer (size_type sz) noexcept :
185
+ explicit mutable_shared_buffer (size_type sz) :
175
186
m_data{std::make_shared<byte_vec>(sz)} { }
176
187
177
- /* *
178
- * @brief Construct by copying from a @c std::span of @c std::byte.
179
- *
180
- * @param sp @c std::byte span pointing to buffer of data. The data is
181
- * copied into the internal buffer of the @c mutable_shared_buffer.
182
- *
183
- */
184
- mutable_shared_buffer (std::span<const std::byte> sp) noexcept :
185
- m_data{std::make_shared<byte_vec>(sp.data (), sp.data ()+sp.size ())} { }
186
188
187
189
/* *
188
190
* @brief Construct by copying from a @c std::byte array.
@@ -192,15 +194,29 @@ class mutable_shared_buffer {
192
194
*
193
195
* @pre Size cannot be greater than the source buffer.
194
196
*
195
- * @param buf @c std::byte array containing buffer of data. The data is
196
- * copied into the internal buffer of the @c mutable_shared_buffer.
197
+ * @param buf Non-null pointer to a @c std::byte buffer of data. The
198
+ * data is copied into the internal buffer of the @c mutable_shared_buffer.
197
199
*
198
200
* @param sz Size of buffer.
199
201
*
200
202
*/
201
- mutable_shared_buffer (const std::byte* buf, size_type sz) noexcept :
203
+ mutable_shared_buffer (const std::byte* buf, size_type sz) :
202
204
mutable_shared_buffer (std::span<const std::byte>(buf, sz)) { }
203
205
206
+ /* *
207
+ * @brief Construct by copying bytes from a @c std::span.
208
+ *
209
+ * The type of the span must be convertible to or layout compatible with
210
+ * @c std::byte.
211
+ *
212
+ * @param sp @c std::span pointing to buffer of data. The @c std::span
213
+ * pointer is cast into a @c std::byte pointer and bytes are then copied.
214
+ *
215
+ */
216
+ template <typename T>
217
+ mutable_shared_buffer (std::span<const T> sp) :
218
+ mutable_shared_buffer (std::bit_cast<const std::byte *>(sp.data()), sp.size()) { }
219
+
204
220
/* *
205
221
* @brief Construct by copying bytes from an arbitrary pointer.
206
222
*
@@ -210,14 +226,13 @@ class mutable_shared_buffer {
210
226
*
211
227
* @pre Size cannot be greater than the source buffer.
212
228
*
213
- * @param buf Pointer to a buffer of data. The pointer must be convertible
214
- * to a @c void pointer and then to a @c std::byte pointer.
229
+ * @param buf Non-null pointer to a buffer of data.
215
230
*
216
231
* @param sz Size of buffer, in bytes.
217
232
*/
218
233
template <typename T>
219
234
mutable_shared_buffer (const T* buf, size_type sz) :
220
- mutable_shared_buffer (reinterpret_cast <const std::byte *>(buf), sz) { }
235
+ mutable_shared_buffer (std::bit_cast <const std::byte *>(buf), sz) { }
221
236
222
237
/* *
223
238
* @brief Construct from input iterators.
@@ -258,10 +273,10 @@ class mutable_shared_buffer {
258
273
/* *
259
274
* @brief Return access to underlying @c std::vector.
260
275
*
261
- * This can be used to instantiate a dynamic_buffer as defined in the Networking TS.
262
- * Changing the @c std::vector from outside this class works because no state
263
- * data is stored within this object that needs to be consistent with the @c std::vector
264
- * contents.
276
+ * This can be used to instantiate a @c dynamic_buffer as defined in the Networking TS
277
+ * or Asio API. Changing the @c std::vector from outside this class works because no
278
+ * state data is stored within this object that needs to be consistent with the
279
+ * @c std::vector contents.
265
280
*
266
281
* @return Reference to @c std::vector<std::byte>.
267
282
*/
@@ -315,7 +330,7 @@ class mutable_shared_buffer {
315
330
/* *
316
331
* @brief Append a @c std::byte buffer to the end of the internal buffer.
317
332
*
318
- * @param buf @c std::byte array containing buffer of data.
333
+ * @param buf Non-null pointer to @c std::byte buffer of data.
319
334
*
320
335
* @param sz Size of buffer.
321
336
*
@@ -324,25 +339,53 @@ class mutable_shared_buffer {
324
339
mutable_shared_buffer& append (const std::byte* buf, size_type sz) {
325
340
size_type old_sz = size ();
326
341
resize (old_sz + sz); // set up buffer space
327
- std::memcpy ( data () + old_sz , buf, sz );
342
+ std::copy (buf , buf+sz, data ()+old_sz );
328
343
return *this ;
329
344
}
330
345
346
+ /* *
347
+ * @brief Append a @c std::span to the end of the internal buffer.
348
+ *
349
+ * @param sp @c std::span of @c std::byte data.
350
+ *
351
+ * @return Reference to @c this (to allow method chaining).
352
+ */
353
+ mutable_shared_buffer& append (std::span<const std::byte> sp) {
354
+ return append (sp.data (), sp.size ());
355
+ }
356
+
331
357
/* *
332
358
* @brief Append by copying bytes from an arbitrary pointer.
333
359
*
334
360
* The pointer passed into this method is cast into a @c std::byte pointer and bytes
335
361
* are then copied. In particular, this method can be used for @c char pointers,
336
362
* @c void pointers, @ unsigned @c char pointers, etc.
337
363
*
338
- * @param buf Pointer to a buffer of data. The pointer must be convertible
339
- * to a @c void pointer and then to a @c std::byte pointer.
364
+ * @param buf Non-null pointer to a buffer of data.
340
365
*
341
366
* @param sz Size of buffer, in bytes.
342
367
*/
343
368
template <typename T>
344
369
mutable_shared_buffer& append (const T* buf, size_type sz) {
345
- return append (reinterpret_cast <const std::byte *>(buf), sz);
370
+ return append (std::bit_cast<const std::byte *>(buf), sz);
371
+ }
372
+
373
+ /* *
374
+ * @brief Append a @c std::span that is a non @c std::byte buffer.
375
+ *
376
+ * The @c std::span passed into this method is performs a cast on the
377
+ * data. In particular, this method can be used for @c char pointers,
378
+ * @c void pointers, @ unsigned @c char pointers, etc.
379
+ *
380
+ * The type of the span must be convertible to or layout compatible with
381
+ * @c std::byte.
382
+ *
383
+ * @param sp @c std::span of arbitrary bytes.
384
+ *
385
+ */
386
+ template <typename T>
387
+ mutable_shared_buffer& append (std::span<const T> sp) {
388
+ return append (std::bit_cast<const std::byte *>(sp.data ()), sp.size ());
346
389
}
347
390
348
391
/* *
@@ -463,18 +506,27 @@ class const_shared_buffer {
463
506
const_shared_buffer& operator =(const const_shared_buffer&) = delete ;
464
507
const_shared_buffer& operator =(const_shared_buffer&&) = delete ;
465
508
509
+ /* *
510
+ * @brief Construct by copying from a @c std::span of @c std::byte.
511
+ *
512
+ * @param sp @c std::byte span pointing to buffer of data. The data is
513
+ * copied into the internal buffer of the @c const_shared_buffer.
514
+ *
515
+ */
516
+ explicit const_shared_buffer (std::span<const std::byte> sp) :
517
+ m_data(std::make_shared<byte_vec>(sp.data(), sp.data()+sp.size())) { }
466
518
/* *
467
519
* @brief Construct by copying from a @c std::byte array.
468
520
*
469
521
* @pre Size cannot be greater than the source buffer.
470
522
*
471
- * @param buf @c std::byte array containing buffer of data. The data is
523
+ * @param buf Non-null pointer to @c std::byte buffer of data. The data is
472
524
* copied into the internal buffer of the @c const_shared_buffer.
473
525
*
474
526
* @param sz Size of buffer.
475
527
*/
476
528
const_shared_buffer (const std::byte* buf, size_type sz) :
477
- m_data (std::make_shared<byte_vec >(buf, buf+ sz)) { }
529
+ const_shared_buffer (std::span< const std::byte >(buf, sz)) { }
478
530
479
531
/* *
480
532
* @brief Construct by copying bytes from an arbitrary pointer.
@@ -483,16 +535,18 @@ class const_shared_buffer {
483
535
* are then copied. In particular, this method can be used for @c char pointers,
484
536
* @c void pointers, @c unsigned @c char pointers, etc.
485
537
*
538
+ * The type of the span must be convertible to or layout compatible with
539
+ * @c std::byte.
540
+ *
486
541
* @pre Size cannot be greater than the source buffer.
487
542
*
488
- * @param buf Pointer to a buffer of data. The pointer must be convertible
489
- * to a @c void pointer and then to a @c std::byte pointer.
543
+ * @param buf Non-null pointer to a buffer of data.
490
544
*
491
545
* @param sz Size of buffer, in bytes.
492
546
*/
493
547
template <typename T>
494
548
const_shared_buffer (const T* buf, size_type sz) :
495
- const_shared_buffer (reinterpret_cast <const std::byte *>(buf), sz) { }
549
+ const_shared_buffer (std::bit_cast <const std::byte *>(buf), sz) { }
496
550
497
551
/* *
498
552
* @brief Construct by copying from a @c mutable_shared_buffer object.
0 commit comments