32
32
* constructing a @c const_shared_buffer for writing to the network.
33
33
*
34
34
* Besides the data buffer lifetime management, these utility classes eliminate data
35
- * copies and (obviously) can be utilized in use cases other than networking.
35
+ * buffer copies and (obviously) can be utilized in use cases other than networking
36
+ * (for example reading and writing disk files).
36
37
*
37
38
* ### Additional Details
38
39
*
53
54
* @c mutable_shared_buffer class as well as adding convenience methods to the
54
55
* @c const_shared_buffer class.
55
56
*
56
- * It is likely that this shared buffer design and code will change as the C++
57
- * Networking TS buffer features are expanded, changed, or better understood. Currently
58
- * there are no direct ties to Networking TS buffer features.
59
- *
60
57
* @note Everything is declared @c noexcept except for the methods that allocate
61
58
* memory and might throw a memory exception. This is tighter than the @c noexcept
62
59
* declarations on the underlying @c std::vector methods, since @c std::byte
79
76
#include < memory> // std::shared_ptr
80
77
#include < compare> // spaceship operator
81
78
#include < span>
79
+ #include < bit> // std::bit_cast
82
80
83
81
#include < utility> // std::move, std::swap
84
82
#include < algorithm> // std::copy
@@ -99,7 +97,7 @@ class const_shared_buffer;
99
97
* This class provides ownership, copying, and lifetime management for byte oriented
100
98
* buffers. In particular, it is designed to be used in conjunction with the
101
99
* @c const_shared_buffer class for efficient transfer and correct lifetime management
102
- * of buffers in asynchronous libraries (such as the C++ Networking TS ). In particular,
100
+ * of buffers in asynchronous libraries (such as Asio ). In particular,
103
101
* a reference counted buffer can be passed among multiple layers of software without
104
102
* any one layer "owning" the buffer.
105
103
*
@@ -223,17 +221,37 @@ class mutable_shared_buffer {
223
221
*
224
222
* The pointer passed into this constructor is cast into a @c std::byte pointer and bytes
225
223
* are then copied. In particular, this method can be used for @c char pointers,
226
- * @c void pointers, @c unsigned @c char pointers, etc.
224
+ * @c unsigned @c char pointers, @c std::uint8_t pointers, etc. Non character types that
225
+ * are trivially copyable are also allowed, although the usual care must be taken
226
+ * (padding bytes, alignment, etc).
227
227
*
228
228
* @pre Size cannot be greater than the source buffer.
229
229
*
230
- * @param buf Non-null pointer to a buffer of data.
230
+ * @param buf Non-null pointer to a contiguous array of data.
231
231
*
232
- * @param sz Size of buffer, in bytes.
232
+ * @param num Number of elements in the array.
233
+ *
234
+ * @note For @c void pointers, see specific constructor taking a @c void pointer.
233
235
*/
234
236
template <typename T>
235
- mutable_shared_buffer (const T* buf, size_type sz) :
236
- mutable_shared_buffer (std::as_bytes(std::span<const T>{buf, sz})) { }
237
+ mutable_shared_buffer (const T* buf, size_type num) :
238
+ mutable_shared_buffer (std::as_bytes(std::span<const T>{buf, num})) { }
239
+
240
+ /* *
241
+ * @brief Construct by copying bytes from a void pointer.
242
+ *
243
+ * The pointer passed into this constructor is cast into a @c std::byte pointer and bytes
244
+ * are then copied.
245
+ *
246
+ * @pre Size cannot be greater than the source buffer.
247
+ *
248
+ * @param buf Non-null @c void pointer to a buffer of data.
249
+ *
250
+ * @param sz Size of buffer, in bytes.
251
+ */
252
+ mutable_shared_buffer (const void * buf, size_type sz) :
253
+ mutable_shared_buffer (std::as_bytes(
254
+ std::span<const std::byte>{std::bit_cast<const std::byte*>(buf), sz})) { }
237
255
238
256
/* *
239
257
* @brief Construct from input iterators.
@@ -345,7 +363,7 @@ class mutable_shared_buffer {
345
363
}
346
364
347
365
/* *
348
- * @brief Append a @c std::span to the end of the internal buffer.
366
+ * @brief Append a @c std::span of @c std::bytes to the end of the internal buffer.
349
367
*
350
368
* @param sp @c std::span of @c std::byte data.
351
369
*
@@ -361,15 +379,33 @@ class mutable_shared_buffer {
361
379
*
362
380
* The pointer passed into this method is cast into a @c std::byte pointer and bytes
363
381
* are then copied. In particular, this method can be used for @c char pointers,
364
- * @c void pointers, @ unsigned @c char pointers, etc.
382
+ * @c void pointers, @c unsigned @c char pointers, @c std::uint8_t pointers, etc.
383
+ * Non character types that are layout compatible with @c std::byte are allowed.
365
384
*
366
- * @param buf Non-null pointer to a buffer of data.
385
+ * @param buf Non-null pointer to an array of data.
367
386
*
368
- * @param sz Size of buffer, in bytes .
387
+ * @param num Number of elements in the array .
369
388
*/
370
389
template <typename T>
371
- mutable_shared_buffer& append (const T* buf, std::size_t sz) {
372
- return append (std::as_bytes (std::span<const T>{buf, sz}));
390
+ mutable_shared_buffer& append (const T* buf, std::size_t num) {
391
+ return append (std::as_bytes (std::span<const T>{buf, num}));
392
+ }
393
+
394
+ /* *
395
+ * @brief Append by copying bytes from a void pointer.
396
+ *
397
+ * The pointer passed into this constructor is cast into a @c std::byte pointer and bytes
398
+ * are then appended.
399
+ *
400
+ * @pre Size cannot be greater than the source buffer.
401
+ *
402
+ * @param buf Non-null @c void pointer to a buffer of data.
403
+ *
404
+ * @param sz Size of buffer, in bytes.
405
+ */
406
+ mutable_shared_buffer& append (const void * buf, size_type sz) {
407
+ return append (std::as_bytes (
408
+ std::span<const std::byte>{std::bit_cast<const std::byte*>(buf), sz}));
373
409
}
374
410
375
411
/* *
@@ -549,20 +585,38 @@ class const_shared_buffer {
549
585
*
550
586
* The pointer passed into this constructor is cast into a @c std::byte pointer and bytes
551
587
* are then copied. In particular, this method can be used for @c char pointers,
552
- * @c void pointers, @c unsigned @c char pointers, etc.
588
+ * @c unsigned @c char pointers, @c std::uint8_t pointers, etc. Non character types that
589
+ * are trivially copyable are also allowed, although the usual care must be taken
590
+ * (padding bytes, alignment, etc).
553
591
*
554
592
* The type of the span must be convertible to or be layout compatible with
555
593
* @c std::byte.
556
594
*
557
595
* @pre Size cannot be greater than the source buffer.
558
596
*
559
- * @param buf Non-null pointer to a buffer of data.
597
+ * @param buf Non-null pointer to an array of data.
560
598
*
561
- * @param sz Size of buffer, in bytes .
599
+ * @param num Number of elements in the array .
562
600
*/
563
601
template <typename T>
564
- const_shared_buffer (const T* buf, std::size_t sz) :
565
- const_shared_buffer (std::as_bytes(std::span<const T>{buf, sz})) { }
602
+ const_shared_buffer (const T* buf, std::size_t num) :
603
+ const_shared_buffer (std::as_bytes(std::span<const T>{buf, num})) { }
604
+
605
+ /* *
606
+ * @brief Construct by copying bytes from a void pointer.
607
+ *
608
+ * The pointer passed into this constructor is cast into a @c std::byte pointer and bytes
609
+ * are then copied.
610
+ *
611
+ * @pre Size cannot be greater than the source buffer.
612
+ *
613
+ * @param buf Non-null @c void pointer to a buffer of data.
614
+ *
615
+ * @param sz Size of buffer, in bytes.
616
+ */
617
+ const_shared_buffer (const void * buf, size_type sz) :
618
+ const_shared_buffer (std::as_bytes(
619
+ std::span<const std::byte>{std::bit_cast<const std::byte*>(buf), sz})) { }
566
620
567
621
/* *
568
622
* @brief Construct by copying from a @c mutable_shared_buffer object.
@@ -581,8 +635,8 @@ class const_shared_buffer {
581
635
*
582
636
* This constructor will move from a @c mutable_shared_buffer into a @c const_shared_buffer.
583
637
* This allows efficient API boundaries, where application code can construct and fill in a
584
- * @c mutable_shared_buffer, then @c std::move it into a @c const_shared_buffer for use
585
- * with asynchronous functions.
638
+ * @c mutable_shared_buffer, then use this constructor which will @c std::move it into a
639
+ * @c const_shared_buffer for use with asynchronous functions.
586
640
*
587
641
* @param rhs @c mutable_shared_buffer to be moved from; after moving the
588
642
* @c mutable_shared_buffer will be empty.
0 commit comments