Skip to content

Commit d51bb5f

Browse files
Enhancing test with a little bit of span and generic pointer testing
1 parent cac94b9 commit d51bb5f

File tree

2 files changed

+37
-32
lines changed

2 files changed

+37
-32
lines changed

include/buffer/shared_buffer.hpp

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,27 @@ class mutable_shared_buffer {
154154
* copied into the internal buffer of the @c mutable_shared_buffer.
155155
*
156156
*/
157-
explicit mutable_shared_buffer(std::span<const std::byte> sp) :
157+
template <std::size_t Ext>
158+
explicit mutable_shared_buffer(std::span<const std::byte, Ext> sp) :
158159
m_data{std::make_shared<byte_vec>(sp.data(), sp.data()+sp.size())} { }
159160

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, size_type sz) :
176+
mutable_shared_buffer(std::span<const std::byte>(buf, sz)) { }
177+
160178
/**
161179
* @brief Move construct from a @c std::vector of @c std::bytes.
162180
*
@@ -174,7 +192,7 @@ class mutable_shared_buffer {
174192

175193
/**
176194
* @brief Construct a @c mutable_shared_buffer with an initial size, contents
177-
* set to zero.
195+
* of each byte set to zero.
178196
*
179197
* Allocate zero initialized space which can be overwritten with data as needed.
180198
* The @c data method is called to get access to the underlying @c std::byte
@@ -186,35 +204,18 @@ class mutable_shared_buffer {
186204
m_data{std::make_shared<byte_vec>(sz)} { }
187205

188206

189-
/**
190-
* @brief Construct by copying from a @c std::byte array.
191-
*
192-
* A @c std::span is first created, then the constructor taking
193-
* a @c std::span is called.
194-
*
195-
* @pre Size cannot be greater than the source buffer.
196-
*
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.
199-
*
200-
* @param sz Size of buffer.
201-
*
202-
*/
203-
mutable_shared_buffer(const std::byte* buf, size_type sz) :
204-
mutable_shared_buffer(std::span<const std::byte>(buf, sz)) { }
205-
206207
/**
207208
* @brief Construct by copying bytes from a @c std::span.
208209
*
209-
* The type of the span must be convertible to or layout compatible with
210+
* The type of the span must be convertible to or be layout compatible with
210211
* @c std::byte.
211212
*
212213
* @param sp @c std::span pointing to buffer of data. The @c std::span
213214
* pointer is cast into a @c std::byte pointer and bytes are then copied.
214215
*
215216
*/
216-
template <typename T>
217-
mutable_shared_buffer(std::span<const T> sp) :
217+
template <typename T, std::size_t Ext>
218+
mutable_shared_buffer(std::span<const T, Ext> sp) :
218219
mutable_shared_buffer(std::bit_cast<const std::byte *>(sp.data()), sp.size()) { }
219220

220221
/**
@@ -350,7 +351,8 @@ class mutable_shared_buffer {
350351
*
351352
* @return Reference to @c this (to allow method chaining).
352353
*/
353-
mutable_shared_buffer& append(std::span<const std::byte> sp) {
354+
template <std::size_t Ext>
355+
mutable_shared_buffer& append(std::span<const std::byte, Ext> sp) {
354356
return append(sp.data(), sp.size());
355357
}
356358

@@ -377,14 +379,14 @@ class mutable_shared_buffer {
377379
* data. In particular, this method can be used for @c char pointers,
378380
* @c void pointers, @ unsigned @c char pointers, etc.
379381
*
380-
* The type of the span must be convertible to or layout compatible with
382+
* The type of the span must be convertible to or be layout compatible with
381383
* @c std::byte.
382384
*
383385
* @param sp @c std::span of arbitrary bytes.
384386
*
385387
*/
386-
template <typename T>
387-
mutable_shared_buffer& append(std::span<const T> sp) {
388+
template <typename T, std::size_t Ext>
389+
mutable_shared_buffer& append(std::span<const T, Ext> sp) {
388390
return append(std::bit_cast<const std::byte *>(sp.data()), sp.size());
389391
}
390392

@@ -513,7 +515,8 @@ class const_shared_buffer {
513515
* copied into the internal buffer of the @c const_shared_buffer.
514516
*
515517
*/
516-
explicit const_shared_buffer(std::span<const std::byte> sp) :
518+
template <std::size_t Ext>
519+
explicit const_shared_buffer(std::span<const std::byte, Ext> sp) :
517520
m_data(std::make_shared<byte_vec>(sp.data(), sp.data()+sp.size())) { }
518521
/**
519522
* @brief Construct by copying from a @c std::byte array.
@@ -526,7 +529,7 @@ class const_shared_buffer {
526529
* @param sz Size of buffer.
527530
*/
528531
const_shared_buffer(const std::byte* buf, size_type sz) :
529-
const_shared_buffer(std::span<const std::byte>(buf, sz)) { }
532+
const_shared_buffer(std::span<const std::byte>{buf, buf+sz}) { }
530533

531534
/**
532535
* @brief Construct by copying bytes from an arbitrary pointer.
@@ -535,7 +538,7 @@ class const_shared_buffer {
535538
* are then copied. In particular, this method can be used for @c char pointers,
536539
* @c void pointers, @c unsigned @c char pointers, etc.
537540
*
538-
* The type of the span must be convertible to or layout compatible with
541+
* The type of the span must be convertible to or be layout compatible with
539542
* @c std::byte.
540543
*
541544
* @pre Size cannot be greater than the source buffer.

test/shared_buffer_test.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ void generic_pointer_append_test() {
4646
auto sb { generic_pointer_construction_test<chops::mutable_shared_buffer, PT>() };
4747
auto sav_sz { sb.size() };
4848
const PT arr[] { 5, 6, 7 };
49-
sb.append (arr, 3);
49+
const PT* ptr_arr { arr };
50+
sb.append (ptr_arr, 3);
5051
REQUIRE (sb.size() == (sav_sz + 3));
51-
// sb.append (std::span<PT>(arr, 3));
52-
// REQUIRE (sb.size() == (sav_sz + 6));
52+
std::span<const PT> sp { arr };
53+
sb.append (sp);
54+
REQUIRE (sb.size() == (sav_sz + 6));
5355
}
5456

5557
template <typename SB>

0 commit comments

Comments
 (0)