-
Notifications
You must be signed in to change notification settings - Fork 38
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
Examples #81
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1e611ea
Allow tsan in container
andreiavrammsd 00b64b8
Details for move
andreiavrammsd 389cd87
Remove basic example
andreiavrammsd 0905fbe
Refine streaming
andreiavrammsd ffab178
Remove close
andreiavrammsd e9cd553
Describe move
andreiavrammsd 59892ac
Describe semaphore
andreiavrammsd e597af8
Use static_channel in move
andreiavrammsd 82f9f7c
Remove multithreading_static_channel
andreiavrammsd 53d28b4
Remove multithreading
andreiavrammsd f172016
Remove virtual
andreiavrammsd 01f6d36
Remove trailing comma
andreiavrammsd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,127 @@ | ||
#include <msd/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; | ||
virtual ~data() = default; | ||
andreiavrammsd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private: | ||
int value_{}; | ||
}; | ||
|
||
std::size_t data::copies_{}; | ||
std::size_t data::moves_{}; | ||
|
||
int main() | ||
{ | ||
msd::channel<data> chan{10}; | ||
|
||
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; | ||
} | ||
} | ||
|
||
const std::vector<int> expected{ | ||
1, | ||
2, | ||
3, | ||
}; | ||
andreiavrammsd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.