Skip to content

Commit 61e184c

Browse files
Merge pull request #34 from andreiavrammsd/build-examples
Add iterator reference type.
2 parents 33e6f09 + be8fc6c commit 61e184c

File tree

7 files changed

+29
-13
lines changed

7 files changed

+29
-13
lines changed

.github/workflows/cmake.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
working-directory: ${{github.workspace}}/build
6161
shell: bash
6262
# Execute the build. You can specify a specific target with "--target <NAME>"
63-
run: cmake --build . --config ${{ matrix.config.build_type }} --target channel_test11 channel_test14 channel_test17
63+
run: cmake --build . --config ${{ matrix.config.build_type }} --target tests examples
6464

6565
- name: Test
6666
working-directory: ${{github.workspace}}/build

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.12)
22
project(cpp_channel)
3-
set(PROJECT_VERSION 0.8.0)
3+
set(PROJECT_VERSION 0.8.1)
44

55
set(CMAKE_CXX_STANDARD 11)
66
set(CMAKE_CXX_STANDARD_REQUIRED YES)

examples/CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ function(add_example NAME)
22
add_executable(${NAME} ${ARGN})
33

44
set_target_warnings(${NAME} PRIVATE)
5-
target_link_libraries(${NAME} -ltsan)
6-
target_compile_options(${NAME} PRIVATE -fsanitize=thread)
5+
6+
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
7+
target_link_libraries(${NAME} -ltsan)
8+
target_compile_options(${NAME} PRIVATE -fsanitize=thread)
9+
endif()
10+
11+
add_dependencies(examples ${NAME})
712
endfunction()
813

14+
add_custom_target(examples)
15+
16+
# Examples
917
add_example(example_basic basic.cpp)
1018
add_example(example_close close.cpp)
1119
add_example(example_move move.cpp)

examples/cmake-project/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ target_link_libraries(cmake_project)
1414

1515
include(FetchContent)
1616
if (NOT channel_POPULATED)
17-
FetchContent_Declare(channel URL https://github.com/andreiavrammsd/cpp-channel/archive/v0.8.0.zip)
17+
FetchContent_Declare(channel URL https://github.com/andreiavrammsd/cpp-channel/archive/v0.8.1.zip)
1818
FetchContent_Populate(channel)
1919
include_directories(${channel_SOURCE_DIR}/include)
2020
# OR

examples/streaming.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
#include <chrono>
22
#include <future>
33
#include <iostream>
4+
#include <string>
45
#include <thread>
56
#include <utility>
7+
#include <vector>
68

79
#include "msd/channel.hpp"
810

911
int main()
1012
{
1113
using messages = msd::channel<std::string>;
1214

13-
auto threads = std::thread::hardware_concurrency();
15+
const auto threads = std::thread::hardware_concurrency();
1416
messages channel{threads};
1517

1618
// Continuously get some data on multiple threads and send it all to a channel
17-
auto in = [](messages& ch, int thread, std::chrono::milliseconds pause) {
18-
thread_local static int i = 0;
19+
const auto in = [](messages& ch, std::size_t thread, std::chrono::milliseconds pause) {
20+
thread_local static std::size_t i = 0U;
1921

2022
while (true) {
2123
if (ch.closed()) {
@@ -31,21 +33,21 @@ int main()
3133

3234
std::vector<std::future<void>> in_futures;
3335
for (std::size_t i = 0U; i < threads; ++i) {
34-
in_futures.push_back(std::async(in, std::ref(channel), i, std::chrono::milliseconds{500}));
36+
in_futures.push_back(std::async(in, std::ref(channel), i, std::chrono::milliseconds{500U}));
3537
}
3638

3739
// Stream incoming data to a destination
38-
auto out = [](messages& ch, std::ostream& stream, const std::string& separator) {
40+
const auto out = [](messages& ch, std::ostream& stream, const std::string& separator) {
3941
std::move(ch.begin(), ch.end(), std::ostream_iterator<std::string>(stream, separator.c_str()));
4042
};
41-
auto out_future = std::async(out, std::ref(channel), std::ref(std::cout), "\n");
43+
const auto out_future = std::async(out, std::ref(channel), std::ref(std::cout), "\n");
4244

4345
// Close the channel after some time
44-
auto timeout = [](messages& ch, std::chrono::milliseconds after) {
46+
const auto timeout = [](messages& ch, std::chrono::milliseconds after) {
4547
std::this_thread::sleep_for(after);
4648
ch.close();
4749
};
48-
auto timeout_future = std::async(timeout, std::ref(channel), std::chrono::milliseconds{3000});
50+
const auto timeout_future = std::async(timeout, std::ref(channel), std::chrono::milliseconds{3000U});
4951

5052
out_future.wait();
5153
for (auto& future : in_futures) {

include/msd/blocking_iterator.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ template <typename Channel>
2020
class blocking_iterator {
2121
public:
2222
using value_type = typename Channel::value_type;
23+
using reference = typename Channel::value_type&;
2324

2425
explicit blocking_iterator(Channel& ch) : ch_{ch} {}
2526

@@ -62,6 +63,7 @@ class blocking_iterator {
6263
template <typename T>
6364
struct std::iterator_traits<msd::blocking_iterator<T>> {
6465
using value_type = typename msd::blocking_iterator<T>::value_type;
66+
using reference = typename msd::blocking_iterator<T>::reference;
6567
using iterator_category = std::output_iterator_tag;
6668
};
6769

tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ function(package_add_test TESTNAME)
3131
endif ()
3232

3333
add_test(NAME ${TESTNAME} COMMAND ${TESTNAME})
34+
35+
add_dependencies(tests ${TESTNAME})
3436
endfunction()
3537

38+
add_custom_target(tests)
39+
3640
# Tests
3741
package_add_test(channel_test11 channel_test.cpp blocking_iterator_test.cpp)
3842
package_add_test(channel_test14 channel_test.cpp blocking_iterator_test.cpp)

0 commit comments

Comments
 (0)