Skip to content

Commit 3e7b7a2

Browse files
Merge pull request #6 from connectivecpp/develop
Updating dependent libraries, docs, utility usage
2 parents 2d50653 + b711eb7 commit 3e7b7a2

File tree

9 files changed

+52
-40
lines changed

9 files changed

+52
-40
lines changed

.github/workflows/gen_docs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: checkout
1919
uses: actions/checkout@v4
2020
- name: run-doxygen
21-
uses: mattnotmitt/doxygen-action@v1.9.8
21+
uses: mattnotmitt/doxygen-action@v1.12.0
2222
with:
2323
working-directory: doc
2424
- name: deploy-pages

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2024 by Cliff Green
1+
# Copyright (c) 2024-2025 by Cliff Green
22
#
33
# https://github.com/connectivecpp/shared-buffer
44
#

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
The `shared_buffer` classes are reference counted `std::byte` buffer classes useful for asynchronous networking. In particular, the Asio asynchronous networking library requires a buffer to be kept alive and valid until the outstanding IO operation (e.g. a network write) is completed. A straightforward and idiomatic way to achieve this is by using reference counted buffers.
1818

19-
There are two classes - `const_shared_buffer` for outgoing buffers (which should not be modified), and `mutable_shared_buffer` for incoming buffers (mutable and expandable as data arrives). There are efficient (move) operations for creating a `const_shared_buffer` from a `mutable_shared_buffer`, which allows the use case of creating a message and serializing its contents, then sending it out over the network.
19+
There are two classes - `const_shared_buffer` for outgoing buffers (which should not be modified), and `mutable_shared_buffer` for incoming buffers (mutable and expandable as data arrives). In addition there are efficient (move) operations for creating a `const_shared_buffer` from a `mutable_shared_buffer`. This allows the following use common networking use case: create a `mutable_shared_buffer`, serialize objects into it, then (efficiently) construct a `const_shared_buffer` and send it out over the network.
2020

2121
While internally all data is kept in `std::byte` buffers, convenience methods are provided for converting between traditional buffer types (such as `char *` or `unsigned char*` or similar).
2222

cmake/download_cpm.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
file(
66
DOWNLOAD
7-
https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.39.0/CPM.cmake
7+
https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.40.0/CPM.cmake
88
${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake
99
)
1010
include(${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake)

example/CMakeLists.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2024 by Cliff Green
1+
# Copyright (c) 2024-2025 by Cliff Green
22
#
33
# Distributed under the Boost Software License, Version 1.0.
44
# (See accompanying file LICENSE.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
@@ -11,8 +11,8 @@ project ( shared_buffer_example LANGUAGES CXX )
1111
# add dependencies
1212
include ( ../cmake/download_cpm.cmake )
1313

14-
CPMAddPackage ( "gh:connectivecpp/[email protected].0" )
15-
CPMAddPackage ( "gh:connectivecpp/[email protected].0" )
14+
CPMAddPackage ( "gh:connectivecpp/[email protected].5" )
15+
CPMAddPackage ( "gh:connectivecpp/[email protected].4" )
1616

1717
# add executable
1818
add_executable ( shared_buffer_example shared_buffer_example.cpp )

example/shared_buffer_example.cpp

+30-19
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/** @file
22
*
3-
* @brief Example code demonstrating use of @c chops::shared_buffer and
4-
* @c chops::repeat. See @c threaded_wait_shared_demo.cpp for multithreaded
5-
* example.
3+
* @brief Example code demonstrating use of @c chops::shared_buffer.
4+
* See @c threaded_wait_shared_demo.cpp for multithreaded example.
65
*
76
* @author Thurman Gillespy
87
*
98
* @copyright (c) 2019 by Thurman Gillespy
109
* 3/22/19
1110
*
11+
* Minor changes 4/3/2025 by Cliff Green (change repeat to iota, append now has endian flag).
12+
*
1213
* Distributed under the Boost Software License, Version 1.0.
1314
* (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
1415
*
@@ -25,10 +26,11 @@
2526
#include <cstddef> // std::byte
2627
#include <cstdint> // std::uint16_t
2728
#include <string>
29+
#include <cstring> // std::strlen
30+
#include <ranges> // std::views::iota
2831

2932
#include "serialize/extract_append.hpp"
3033
#include "buffer/shared_buffer.hpp"
31-
#include "utility/repeat.hpp"
3234

3335
// tasty utility lambda function
3436
constexpr auto printLn = [] () { std::cout << std::endl; };
@@ -55,17 +57,18 @@ int main() {
5557
constexpr char str1[] = "A cat in the hat.";
5658
const char* strptr = str1;
5759

58-
// add one char at a time, inside chops::repeat
59-
chops::repeat(sizeof(str1),
60-
[&] () { buf1.append(static_cast<std::byte> (*strptr++)); });
60+
// add one char at a time
61+
for (int i : std::views::iota(0u, std::strlen(strptr) + 1)) {
62+
buf1.append(static_cast<std::byte> (*strptr++));
63+
}
6164

62-
// what str1 and chops::repeat replaces
65+
// what str1 and the loop replaces
6366
// buf1.append(static_cast<std::byte>('A');
6467
// buf1.append((static_cast<std::byte>(' ');
6568
// buf1.append((static_cast<std::byte>('c');
6669
// etc.
6770

68-
std::cout << "buffer1 contains " << buf1.size() << " bytes" << std::endl;
71+
std::cout << "buffer1 contains (including trailing nul char) " << buf1.size() << " bytes" << std::endl;
6972
// print the output, one char at a time
7073
const char* byte = cast_to_char_ptr (buf1); // data starts here
7174
for (unsigned int i = 0; i < buf1.size(); ++i) {
@@ -90,29 +93,37 @@ int main() {
9093
std::cout << "buffer2 contains " << buf2.size() << " bytes and ";
9194
std::cout << (buf2.size()/sizeof(std::uint16_t)) << " short integers\n";
9295

93-
// input some numbers using chops::repeat
96+
// input some numbers
9497
const std::uint16_t* data = cast_to_uint16_ptr (buf2);// data starts here
95-
std::uint16_t* valptr = const_cast<std::uint16_t*>(data); // remove const*
9698

9799
// create number, convert to 'network' (big endian) byte order, place into buf2
98100
std::uint16_t count = 1;
99-
chops::repeat(NUM_INTS, [count, x = buf2.data()] () mutable {auto sz =
100-
chops::append_val <std::uint16_t> (x, count++ * 5); x += sz; });
101+
auto x = buf2.data();
102+
for (int i : std::views::iota(0, NUM_INTS)) {
103+
auto sz = chops::append_val <std::endian::big, std::uint16_t> (x, count++ * 5);
104+
x += sz;
105+
}
101106

102107
// print them out
103-
valptr = const_cast<std::uint16_t*> (data);
104-
// read 2 bytes, convert back to proper endian order, print
105-
auto f = [x = buf2.data()] () mutable { std::cout << chops::extract_val<std::uint16_t>(x) << " "; x+=2;};
106-
chops::repeat(NUM_INTS, f);
108+
// read 2 bytes, convert back to native endian order, print
109+
x = buf2.data();
110+
for (int i : std::views::iota(0, NUM_INTS)) {
111+
std::cout << chops::extract_val<std::endian::native, std::uint16_t>(x) << " ";
112+
x+=2;
113+
}
114+
107115
printLn();
108116

109117
// swap the buffers, print result
110118
buf2.swap(buf1);
111119
std::cout << "buffer2 contents after swap" << std::endl;
112120
std::cout << cast_to_char_ptr (buf2) << std::endl;
113121
std::cout << "buffer1 contents after swap" << std::endl;
114-
valptr = const_cast<std::uint16_t*> (data);
115-
chops::repeat(NUM_INTS, f);
122+
x = buf1.data();
123+
for (int i : std::views::iota(0, NUM_INTS)) {
124+
std::cout << chops::extract_val<std::endian::native, std::uint16_t>(x) << " ";
125+
x+=2;
126+
}
116127
printLn();
117128

118129
return EXIT_SUCCESS;

include/buffer/shared_buffer.hpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
* convenience methods for appending data. @c const_shared_buffer is a reference counted
2727
* non-modifiable buffer class. Once the object is constructed, it cannot be modified.
2828
*
29-
* A @c const_shared_buffer can be efficiently constructed (no buffer copies) from a
30-
* @c mutable shared_buffer. This allows the use case of serializing data into a
31-
* @c mutable_shared_buffer then constructing a @c const_shared_buffer for writing to
32-
* the network.
29+
* A @c const_shared_buffer can be efficiently constructed (no buffer copies, only
30+
* pointer assignments through move construction) from a @c mutable shared_buffer. This
31+
* allows the use case of serializing data into a @c mutable_shared_buffer then
32+
* constructing a @c const_shared_buffer for writing to the network.
3333
*
3434
* Besides the data buffer lifetime management, these utility classes eliminate data
35-
* copies and (obviously) do not have to be used only in networking use cases.
35+
* copies and (obviously) can be utilized in use cases other than networking.
3636
*
3737
* ### Additional Details
3838
*
@@ -64,7 +64,7 @@
6464
*
6565
* @authors Cliff Green, Chris Kohlhoff
6666
*
67-
* @copyright (c) 2017-2024 by Cliff Green
67+
* @copyright (c) 2017-2025 by Cliff Green
6868
*
6969
* Distributed under the Boost Software License, Version 1.0.
7070
* (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

test/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2024 by Cliff Green
1+
# Copyright (c) 2024-2025 by Cliff Green
22
#
33
# Distributed under the Boost Software License, Version 1.0.
44
# (See accompanying file LICENSE.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
@@ -16,7 +16,7 @@ target_compile_features ( shared_buffer_test PRIVATE cxx_std_20 )
1616
include ( ../cmake/download_cpm.cmake )
1717

1818
CPMAddPackage ( "gh:catchorg/[email protected]" )
19-
CPMAddPackage ( "gh:connectivecpp/[email protected].4" )
19+
CPMAddPackage ( "gh:connectivecpp/[email protected].5" )
2020

2121
# link dependencies
2222
target_link_libraries ( shared_buffer_test PRIVATE shared_buffer utility_rack Catch2::Catch2WithMain )

test/shared_buffer_test.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @author Cliff Green
77
*
8-
* @copyright (c) 2017-2024 by Cliff Green
8+
* @copyright (c) 2017-2025 by Cliff Green
99
*
1010
* Distributed under the Boost Software License, Version 1.0.
1111
* (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -23,10 +23,10 @@
2323
#include <array>
2424
#include <algorithm> // std::copy
2525
#include <bit> // std::bit_cast
26+
#include <ranges> // std::views::iota
2627

2728
#include "buffer/shared_buffer.hpp"
2829

29-
#include "utility/repeat.hpp"
3030
#include "utility/byte_array.hpp"
3131

3232
constexpr std::size_t test_data_size { 12u };
@@ -203,15 +203,16 @@ TEST_CASE ( "Mutable shared buffer resize and clear",
203203

204204
sb.resize(N);
205205
REQUIRE (sb.size() == N);
206-
chops::repeat(N, [&sb] (int i) { REQUIRE (std::to_integer<int>(*(sb.data() + i)) == 0 ); } );
207-
206+
for (int i : std::views::iota(0, N)) {
207+
REQUIRE (std::to_integer<int>(*(sb.data() + i)) == 0 );
208+
}
208209
SECTION ( "Compare two resized mutable shared buffer with same size" ) {
209210
chops::mutable_shared_buffer sb2(N);
210211
REQUIRE (sb == sb2);
211-
chops::repeat(N, [&sb, &sb2] (int i) {
212+
for (int i : std::views::iota(0, N)) {
212213
REQUIRE (std::to_integer<int>(*(sb.data() + i)) == 0 );
213214
REQUIRE (std::to_integer<int>(*(sb2.data() + i)) == 0 );
214-
} );
215+
}
215216
}
216217
SECTION ( "Clear, check size" ) {
217218
sb.clear();

0 commit comments

Comments
 (0)