Skip to content

Examples #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@
"source=cache,target=/home/ubuntu/.cache,type=volume"
],
"workspaceFolder": "/workspace",
"runArgs": [
"--cap-add=SYS_PTRACE",
"--security-opt",
"seccomp=unconfined"
],
"remoteUser": "ubuntu"
}
3 changes: 2 additions & 1 deletion examples/.clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ InheritParentConfig: true

Checks: >
-cppcoreguidelines-avoid-magic-numbers,
-readability-magic-numbers
-readability-magic-numbers,
-fuchsia-default-arguments-calls
11 changes: 0 additions & 11 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,12 @@ add_custom_target(examples)
add_custom_target(run_examples)

# Examples
add_example(example_basic basic.cpp)
run_example(example_basic)

add_example(example_close close.cpp)
run_example(example_close)

add_example(example_move move.cpp)
run_example(example_move)

add_example(example_multithreading multithreading.cpp)

add_example(example_streaming streaming.cpp)
run_example(example_streaming)

add_example(example_multithreading_static_channel multithreading_static_channel.cpp)
run_example(example_multithreading_static_channel)

add_example(example_concurrent_map_filter concurrent_map_filter.cpp)
run_example(example_concurrent_map_filter)

Expand Down
21 changes: 0 additions & 21 deletions examples/basic.cpp

This file was deleted.

58 changes: 0 additions & 58 deletions examples/close.cpp

This file was deleted.

86 changes: 77 additions & 9 deletions examples/move.cpp
Original file line number Diff line number Diff line change
@@ -1,58 +1,126 @@
#include <msd/channel.hpp>
#include <msd/static_channel.hpp>

#include <cstddef>
#include <iostream>
#include <utility>
#include <vector>

class data final {
int value_{};

public:
static std::size_t copies_;
static std::size_t moves_;

data() = default;
explicit data(int value) : value_{value} {}

int get_value() const { return value_; }

data(const data& other) noexcept : value_{other.value_} { std::cout << "copy " << value_ << '\n'; }
data(const data& other) noexcept : value_{other.value_}
{
std::cout << "copy " << value_ << '\n';
++copies_;
}

data& operator=(const data& other)
{
if (this != &other) {
value_ = other.value_;
std::cout << "copy " << value_ << '\n';
++copies_;
}
std::cout << "copy " << value_ << '\n';

return *this;
}

data(data&& other) noexcept : value_{other.value_} { std::cout << "move " << value_ << '\n'; }
data(data&& other) noexcept : value_{other.value_}
{
std::cout << "move " << value_ << '\n';
++moves_;
}

data& operator=(data&& other) noexcept
{
if (this != &other) {
value_ = other.value_;
std::cout << "move " << value_ << '\n';
++moves_;
}

return *this;
}

~data() = default;

private:
int value_{};
};

std::size_t data::copies_{};
std::size_t data::moves_{};

// Copy and move semantics with a user-defined type.

int main()
{
msd::channel<data> chan{10};
msd::static_channel<data, 10> chan{};

auto in1 = data{1};
// l-value: will be copied
const auto in1 = data{1};
chan << in1;

// r-value: will be moved
chan << data{2};

// l-value -> std::move -> r-value: will be moved
auto in3 = data{3};
chan << std::move(in3);

for (const auto& out : chan) {
std::vector<int> actual;

// Each value will be moved when read
for (const data& out : chan) {
std::cout << out.get_value() << '\n';

actual.push_back(out.get_value());

if (chan.empty()) {
break;
}
}

// Read values
const std::vector<int> expected{1, 2, 3};

if (actual != expected) {
std::cerr << "Error: got: ";
for (const int value : actual) {
std::cerr << value << ", ";
}

std::cerr << ", expected: ";
for (const int value : expected) {
std::cerr << value << ", ";
}
std::cerr << '\n';

std::terminate();
}

// 1 copy when in1 was written
constexpr std::size_t expected_copies = 1;

if (data::copies_ != expected_copies) {
std::cerr << "Error: copies: " << data::copies_ << ", expected: " << expected_copies << '\n';
std::terminate();
}

// 1 move when the second value was written
// 1 move when in3 was written
// 3 moves when the 3 values were read
constexpr std::size_t expected_moves = 5;

if (data::moves_ != expected_moves) {
std::cerr << "Error: moves: " << data::moves_ << ", expected: " << expected_moves << '\n';
std::terminate();
}
}
46 changes: 0 additions & 46 deletions examples/multithreading.cpp

This file was deleted.

60 changes: 0 additions & 60 deletions examples/multithreading_static_channel.cpp

This file was deleted.

2 changes: 2 additions & 0 deletions examples/semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ int simulate_heavy_computation(const int value)
return result;
};

// https://en.wikipedia.org/wiki/Semaphore_(programming)

int main()
{
semaphore sem{2};
Expand Down
Loading