Skip to content

Commit 5c62b42

Browse files
Enhanced span handling
1 parent ccc61f9 commit 5c62b42

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

include/buffer/shared_buffer.hpp

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
#include <utility> // std::move, std::swap
8484
#include <algorithm> // std::copy
8585

86-
// TODO - add conecepts and / or requires
86+
// TODO - add concepts and / or requires
8787
//
8888
// modify the templated constructor that takes a buffer of any valid
8989
// byte type, add constraints which makes the casting safer
@@ -172,8 +172,8 @@ class mutable_shared_buffer {
172172
* @param sz Size of buffer.
173173
*
174174
*/
175-
mutable_shared_buffer(const std::byte* buf, size_type sz) :
176-
mutable_shared_buffer(std::span<const std::byte>(buf, sz)) { }
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})) { }
177177

178178
/**
179179
* @brief Move construct from a @c std::vector of @c std::bytes.
@@ -216,7 +216,7 @@ class mutable_shared_buffer {
216216
*/
217217
template <typename T, std::size_t Ext>
218218
mutable_shared_buffer(std::span<const T, Ext> sp) :
219-
mutable_shared_buffer(std::bit_cast<const std::byte *>(sp.data()), sp.size()) { }
219+
mutable_shared_buffer(std::as_bytes(sp)) { }
220220

221221
/**
222222
* @brief Construct by copying bytes from an arbitrary pointer.
@@ -233,7 +233,7 @@ class mutable_shared_buffer {
233233
*/
234234
template <typename T>
235235
mutable_shared_buffer(const T* buf, size_type sz) :
236-
mutable_shared_buffer(std::bit_cast<const std::byte *>(buf), sz) { }
236+
mutable_shared_buffer(std::as_bytes(std::span<const T>{buf, sz})) { }
237237

238238
/**
239239
* @brief Construct from input iterators.
@@ -271,6 +271,13 @@ class mutable_shared_buffer {
271271
*/
272272
const std::byte* data() const noexcept { return m_data->data(); }
273273

274+
/**
275+
* @brief Return size (number of bytes) of buffer.
276+
*
277+
* @return Size of buffer, which may be zero.
278+
*/
279+
size_type size() const noexcept { return m_data->size(); }
280+
274281
/**
275282
* @brief Return access to underlying @c std::vector.
276283
*
@@ -283,13 +290,6 @@ class mutable_shared_buffer {
283290
*/
284291
byte_vec& get_byte_vec() noexcept { return *m_data; }
285292

286-
/**
287-
* @brief Return size (number of bytes) of buffer.
288-
*
289-
* @return Size of buffer, which may be zero.
290-
*/
291-
size_type size() const noexcept { return m_data->size(); }
292-
293293
/**
294294
* @brief Query to see if size is zero.
295295
*
@@ -337,7 +337,7 @@ class mutable_shared_buffer {
337337
*
338338
* @return Reference to @c this (to allow method chaining).
339339
*/
340-
mutable_shared_buffer& append(const std::byte* buf, size_type sz) {
340+
mutable_shared_buffer& append(const std::byte* buf, std::size_t sz) {
341341
size_type old_sz = size();
342342
resize(old_sz + sz); // set up buffer space
343343
std::copy(buf, buf+sz, data()+old_sz);
@@ -368,8 +368,8 @@ class mutable_shared_buffer {
368368
* @param sz Size of buffer, in bytes.
369369
*/
370370
template <typename T>
371-
mutable_shared_buffer& append(const T* buf, size_type sz) {
372-
return append(std::bit_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}));
373373
}
374374

375375
/**
@@ -387,7 +387,7 @@ class mutable_shared_buffer {
387387
*/
388388
template <typename T, std::size_t Ext>
389389
mutable_shared_buffer& append(std::span<const T, Ext> sp) {
390-
return append(std::bit_cast<const std::byte *>(sp.data()), sp.size());
390+
return append(std::as_bytes(sp));
391391
}
392392

393393
/**
@@ -528,9 +528,22 @@ class const_shared_buffer {
528528
*
529529
* @param sz Size of buffer.
530530
*/
531-
const_shared_buffer(const std::byte* buf, size_type sz) :
532-
const_shared_buffer(std::span<const std::byte>{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})) { }
533533

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)) { }
534547
/**
535548
* @brief Construct by copying bytes from an arbitrary pointer.
536549
*
@@ -548,8 +561,8 @@ class const_shared_buffer {
548561
* @param sz Size of buffer, in bytes.
549562
*/
550563
template <typename T>
551-
const_shared_buffer(const T* buf, size_type sz) :
552-
const_shared_buffer(std::bit_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})) { }
553566

554567
/**
555568
* @brief Construct by copying from a @c mutable_shared_buffer object.

0 commit comments

Comments
 (0)