|
1 | | -#include <msd/channel.hpp> |
| 1 | +#include <msd/static_channel.hpp> |
2 | 2 |
|
| 3 | +#include <cstddef> |
3 | 4 | #include <iostream> |
| 5 | +#include <utility> |
| 6 | +#include <vector> |
4 | 7 |
|
5 | 8 | class data final { |
6 | | - int value_{}; |
7 | | - |
8 | 9 | public: |
| 10 | + static std::size_t copies_; |
| 11 | + static std::size_t moves_; |
| 12 | + |
9 | 13 | data() = default; |
10 | 14 | explicit data(int value) : value_{value} {} |
11 | 15 |
|
12 | 16 | int get_value() const { return value_; } |
13 | 17 |
|
14 | | - data(const data& other) noexcept : value_{other.value_} { std::cout << "copy " << value_ << '\n'; } |
| 18 | + data(const data& other) noexcept : value_{other.value_} |
| 19 | + { |
| 20 | + std::cout << "copy " << value_ << '\n'; |
| 21 | + ++copies_; |
| 22 | + } |
| 23 | + |
15 | 24 | data& operator=(const data& other) |
16 | 25 | { |
17 | 26 | if (this != &other) { |
18 | 27 | value_ = other.value_; |
| 28 | + std::cout << "copy " << value_ << '\n'; |
| 29 | + ++copies_; |
19 | 30 | } |
20 | | - std::cout << "copy " << value_ << '\n'; |
21 | 31 |
|
22 | 32 | return *this; |
23 | 33 | } |
24 | 34 |
|
25 | | - data(data&& other) noexcept : value_{other.value_} { std::cout << "move " << value_ << '\n'; } |
| 35 | + data(data&& other) noexcept : value_{other.value_} |
| 36 | + { |
| 37 | + std::cout << "move " << value_ << '\n'; |
| 38 | + ++moves_; |
| 39 | + } |
| 40 | + |
26 | 41 | data& operator=(data&& other) noexcept |
27 | 42 | { |
28 | 43 | if (this != &other) { |
29 | 44 | value_ = other.value_; |
30 | 45 | std::cout << "move " << value_ << '\n'; |
| 46 | + ++moves_; |
31 | 47 | } |
32 | 48 |
|
33 | 49 | return *this; |
34 | 50 | } |
35 | 51 |
|
36 | 52 | ~data() = default; |
| 53 | + |
| 54 | + private: |
| 55 | + int value_{}; |
37 | 56 | }; |
38 | 57 |
|
| 58 | +std::size_t data::copies_{}; |
| 59 | +std::size_t data::moves_{}; |
| 60 | + |
| 61 | +// Copy and move semantics with a user-defined type. |
| 62 | + |
39 | 63 | int main() |
40 | 64 | { |
41 | | - msd::channel<data> chan{10}; |
| 65 | + msd::static_channel<data, 10> chan{}; |
42 | 66 |
|
43 | | - auto in1 = data{1}; |
| 67 | + // l-value: will be copied |
| 68 | + const auto in1 = data{1}; |
44 | 69 | chan << in1; |
45 | 70 |
|
| 71 | + // r-value: will be moved |
46 | 72 | chan << data{2}; |
47 | 73 |
|
| 74 | + // l-value -> std::move -> r-value: will be moved |
48 | 75 | auto in3 = data{3}; |
49 | 76 | chan << std::move(in3); |
50 | 77 |
|
51 | | - for (const auto& out : chan) { |
| 78 | + std::vector<int> actual; |
| 79 | + |
| 80 | + // Each value will be moved when read |
| 81 | + for (const data& out : chan) { |
52 | 82 | std::cout << out.get_value() << '\n'; |
53 | 83 |
|
| 84 | + actual.push_back(out.get_value()); |
| 85 | + |
54 | 86 | if (chan.empty()) { |
55 | 87 | break; |
56 | 88 | } |
57 | 89 | } |
| 90 | + |
| 91 | + // Read values |
| 92 | + const std::vector<int> expected{1, 2, 3}; |
| 93 | + |
| 94 | + if (actual != expected) { |
| 95 | + std::cerr << "Error: got: "; |
| 96 | + for (const int value : actual) { |
| 97 | + std::cerr << value << ", "; |
| 98 | + } |
| 99 | + |
| 100 | + std::cerr << ", expected: "; |
| 101 | + for (const int value : expected) { |
| 102 | + std::cerr << value << ", "; |
| 103 | + } |
| 104 | + std::cerr << '\n'; |
| 105 | + |
| 106 | + std::terminate(); |
| 107 | + } |
| 108 | + |
| 109 | + // 1 copy when in1 was written |
| 110 | + constexpr std::size_t expected_copies = 1; |
| 111 | + |
| 112 | + if (data::copies_ != expected_copies) { |
| 113 | + std::cerr << "Error: copies: " << data::copies_ << ", expected: " << expected_copies << '\n'; |
| 114 | + std::terminate(); |
| 115 | + } |
| 116 | + |
| 117 | + // 1 move when the second value was written |
| 118 | + // 1 move when in3 was written |
| 119 | + // 3 moves when the 3 values were read |
| 120 | + constexpr std::size_t expected_moves = 5; |
| 121 | + |
| 122 | + if (data::moves_ != expected_moves) { |
| 123 | + std::cerr << "Error: moves: " << data::moves_ << ", expected: " << expected_moves << '\n'; |
| 124 | + std::terminate(); |
| 125 | + } |
58 | 126 | } |
0 commit comments