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
- // TODO - add conecepts and / or requires
86
+ // TODO - add concepts 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,34 @@ 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
+ template <std::size_t Ext>
158
+ explicit mutable_shared_buffer (std::span<const std::byte, Ext> sp) :
159
+ m_data{std::make_shared<byte_vec>(sp.data (), sp.data ()+sp.size ())} { }
160
+
161
+ /* *
162
+ * @brief Construct by copying from a @c std::byte array.
163
+ *
164
+ * A @c std::span is first created, then the constructor taking
165
+ * a @c std::span is called.
166
+ *
167
+ * @pre Size cannot be greater than the source buffer.
168
+ *
169
+ * @param buf Non-null pointer to a @c std::byte buffer of data. The
170
+ * data is copied into the internal buffer of the @c mutable_shared_buffer.
171
+ *
172
+ * @param sz Size of buffer.
173
+ *
174
+ */
175
+ mutable_shared_buffer (const std::byte* buf, std::size_t sz) :
176
+ mutable_shared_buffer (std::as_bytes(std::span<const std::byte>{buf, sz})) { }
177
+
150
178
/* *
151
179
* @brief Move construct from a @c std::vector of @c std::bytes.
152
180
*
@@ -161,45 +189,34 @@ class mutable_shared_buffer {
161
189
m_data{std::make_shared<byte_vec>(size_type (0 ))} {
162
190
*m_data = std::move (bv);
163
191
}
192
+
164
193
/* *
165
194
* @brief Construct a @c mutable_shared_buffer with an initial size, contents
166
- * set to zero.
195
+ * of each byte set to zero.
167
196
*
168
197
* Allocate zero initialized space which can be overwritten with data as needed.
169
198
* The @c data method is called to get access to the underlying @c std::byte
170
199
* buffer.
171
200
*
172
201
* @param sz Size for internal @c std::byte buffer.
173
202
*/
174
- explicit mutable_shared_buffer (size_type sz) noexcept :
203
+ explicit mutable_shared_buffer (size_type sz) :
175
204
m_data{std::make_shared<byte_vec>(sz)} { }
176
205
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
206
187
207
/* *
188
- * @brief Construct by copying from a @c std::byte array.
189
- *
190
- * A @c std::span is first created, then the constructor taking
191
- * a @c std::span is called.
208
+ * @brief Construct by copying bytes from a @c std::span.
192
209
*
193
- * @pre Size cannot be greater than the source buffer.
194
- *
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.
210
+ * The type of the span must be convertible to or be layout compatible with
211
+ * @c std::byte.
197
212
*
198
- * @param sz Size of buffer.
213
+ * @param sp @c std::span pointing to buffer of data. The @c std::span
214
+ * pointer is cast into a @c std::byte pointer and bytes are then copied.
199
215
*
200
216
*/
201
- mutable_shared_buffer (const std::byte* buf, size_type sz) noexcept :
202
- mutable_shared_buffer (std::span<const std::byte>(buf, sz)) { }
217
+ template <typename T, std::size_t Ext>
218
+ mutable_shared_buffer (std::span<const T, Ext> sp) :
219
+ mutable_shared_buffer (std::as_bytes(sp)) { }
203
220
204
221
/* *
205
222
* @brief Construct by copying bytes from an arbitrary pointer.
@@ -210,14 +227,13 @@ class mutable_shared_buffer {
210
227
*
211
228
* @pre Size cannot be greater than the source buffer.
212
229
*
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.
230
+ * @param buf Non-null pointer to a buffer of data.
215
231
*
216
232
* @param sz Size of buffer, in bytes.
217
233
*/
218
234
template <typename T>
219
235
mutable_shared_buffer (const T* buf, size_type sz) :
220
- mutable_shared_buffer (reinterpret_cast < const std::byte *>( buf) , sz) { }
236
+ mutable_shared_buffer (std::as_bytes(std::span< const T>{ buf, sz}) ) { }
221
237
222
238
/* *
223
239
* @brief Construct from input iterators.
@@ -256,23 +272,23 @@ class mutable_shared_buffer {
256
272
const std::byte* data () const noexcept { return m_data->data (); }
257
273
258
274
/* *
259
- * @brief Return access to underlying @c std::vector.
260
- *
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.
275
+ * @brief Return size (number of bytes) of buffer.
265
276
*
266
- * @return Reference to @c std::vector<std::byte> .
277
+ * @return Size of buffer, which may be zero .
267
278
*/
268
- byte_vec& get_byte_vec () noexcept { return * m_data; }
279
+ size_type size () const noexcept { return m_data-> size () ; }
269
280
270
281
/* *
271
- * @brief Return size (number of bytes) of buffer .
282
+ * @brief Return access to underlying @c std::vector .
272
283
*
273
- * @return Size of buffer, which may be zero.
284
+ * This can be used to instantiate a @c dynamic_buffer as defined in the Networking TS
285
+ * or Asio API. Changing the @c std::vector from outside this class works because no
286
+ * state data is stored within this object that needs to be consistent with the
287
+ * @c std::vector contents.
288
+ *
289
+ * @return Reference to @c std::vector<std::byte>.
274
290
*/
275
- size_type size () const noexcept { return m_data-> size () ; }
291
+ byte_vec& get_byte_vec () noexcept { return * m_data; }
276
292
277
293
/* *
278
294
* @brief Query to see if size is zero.
@@ -315,34 +331,63 @@ class mutable_shared_buffer {
315
331
/* *
316
332
* @brief Append a @c std::byte buffer to the end of the internal buffer.
317
333
*
318
- * @param buf @c std::byte array containing buffer of data.
334
+ * @param buf Non-null pointer to @c std::byte buffer of data.
319
335
*
320
336
* @param sz Size of buffer.
321
337
*
322
338
* @return Reference to @c this (to allow method chaining).
323
339
*/
324
- mutable_shared_buffer& append (const std::byte* buf, size_type sz) {
340
+ mutable_shared_buffer& append (const std::byte* buf, std:: size_t sz) {
325
341
size_type old_sz = size ();
326
342
resize (old_sz + sz); // set up buffer space
327
- std::memcpy ( data () + old_sz , buf, sz );
343
+ std::copy (buf , buf+sz, data ()+old_sz );
328
344
return *this ;
329
345
}
330
346
347
+ /* *
348
+ * @brief Append a @c std::span to the end of the internal buffer.
349
+ *
350
+ * @param sp @c std::span of @c std::byte data.
351
+ *
352
+ * @return Reference to @c this (to allow method chaining).
353
+ */
354
+ template <std::size_t Ext>
355
+ mutable_shared_buffer& append (std::span<const std::byte, Ext> sp) {
356
+ return append (sp.data (), sp.size ());
357
+ }
358
+
331
359
/* *
332
360
* @brief Append by copying bytes from an arbitrary pointer.
333
361
*
334
362
* The pointer passed into this method is cast into a @c std::byte pointer and bytes
335
363
* are then copied. In particular, this method can be used for @c char pointers,
336
364
* @c void pointers, @ unsigned @c char pointers, etc.
337
365
*
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.
366
+ * @param buf Non-null pointer to a buffer of data.
340
367
*
341
368
* @param sz Size of buffer, in bytes.
342
369
*/
343
370
template <typename T>
344
- mutable_shared_buffer& append (const T* buf, size_type sz) {
345
- return append (reinterpret_cast <const std::byte *>(buf), sz);
371
+ mutable_shared_buffer& append (const T* buf, std::size_t sz) {
372
+ return append (std::as_bytes (std::span<const T>{buf, sz}));
373
+ }
374
+
375
+ /* *
376
+ * @brief Append a @c std::span that is a non @c std::byte buffer.
377
+ *
378
+ * The @c std::span passed into this method is performs a cast on the
379
+ * data. In particular, this method can be used for @c char pointers,
380
+ * @c void pointers, @ unsigned @c char pointers, etc.
381
+ *
382
+ * The type of the span must be convertible to or be layout compatible with
383
+ * @c std::byte.
384
+ *
385
+ * @param sp @c std::span of arbitrary bytes.
386
+ *
387
+ */
388
+ template <typename T, std::size_t Ext>
389
+ mutable_shared_buffer& append (std::span<const T, Ext> sp) {
390
+ return append (std::as_bytes (sp));
346
391
}
347
392
348
393
/* *
@@ -463,36 +508,61 @@ class const_shared_buffer {
463
508
const_shared_buffer& operator =(const const_shared_buffer&) = delete ;
464
509
const_shared_buffer& operator =(const_shared_buffer&&) = delete ;
465
510
511
+ /* *
512
+ * @brief Construct by copying from a @c std::span of @c std::byte.
513
+ *
514
+ * @param sp @c std::byte span pointing to buffer of data. The data is
515
+ * copied into the internal buffer of the @c const_shared_buffer.
516
+ *
517
+ */
518
+ template <std::size_t Ext>
519
+ explicit const_shared_buffer (std::span<const std::byte, Ext> sp) :
520
+ m_data(std::make_shared<byte_vec>(sp.data(), sp.data()+sp.size())) { }
466
521
/* *
467
522
* @brief Construct by copying from a @c std::byte array.
468
523
*
469
524
* @pre Size cannot be greater than the source buffer.
470
525
*
471
- * @param buf @c std::byte array containing buffer of data. The data is
526
+ * @param buf Non-null pointer to @c std::byte buffer of data. The data is
472
527
* copied into the internal buffer of the @c const_shared_buffer.
473
528
*
474
529
* @param sz Size of buffer.
475
530
*/
476
- const_shared_buffer (const std::byte* buf, size_type sz) :
477
- m_data (std::make_shared<byte_vec>( buf, buf+sz )) { }
531
+ const_shared_buffer (const std::byte* buf, std:: size_t sz) :
532
+ const_shared_buffer (std::as_bytes(std::span< const std::byte>{ buf, sz} )) { }
478
533
534
+ /* *
535
+ * @brief Construct by copying from a @c std::span.
536
+ *
537
+ * The type of the span must be convertible to or be layout compatible with
538
+ * @c std::byte.
539
+ *
540
+ * @param sp @c std::span pointing to buffer of data. The @c std::span
541
+ * pointer is cast into a @c std::byte pointer and bytes are then copied.
542
+ *
543
+ */
544
+ template <typename T, std::size_t Ext>
545
+ const_shared_buffer (std::span<const T, Ext> sp) :
546
+ const_shared_buffer (std::as_bytes(sp)) { }
479
547
/* *
480
548
* @brief Construct by copying bytes from an arbitrary pointer.
481
549
*
482
550
* The pointer passed into this constructor is cast into a @c std::byte pointer and bytes
483
551
* are then copied. In particular, this method can be used for @c char pointers,
484
552
* @c void pointers, @c unsigned @c char pointers, etc.
485
553
*
554
+ * The type of the span must be convertible to or be layout compatible with
555
+ * @c std::byte.
556
+ *
486
557
* @pre Size cannot be greater than the source buffer.
487
558
*
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.
559
+ * @param buf Non-null pointer to a buffer of data.
490
560
*
491
561
* @param sz Size of buffer, in bytes.
492
562
*/
493
563
template <typename T>
494
- const_shared_buffer (const T* buf, size_type sz) :
495
- const_shared_buffer (reinterpret_cast < const std::byte *>( buf) , sz) { }
564
+ const_shared_buffer (const T* buf, std:: size_t sz) :
565
+ const_shared_buffer (std::as_bytes(std::span< const T>{ buf, sz}) ) { }
496
566
497
567
/* *
498
568
* @brief Construct by copying from a @c mutable_shared_buffer object.
0 commit comments