* @example ref_count.cpp **/
| 6 | * @example ref_count.cpp |
| 7 | **/ |
| 8 | int main() // NOLINT(bugprone-exception-escape) |
| 9 | { |
| 10 | { |
| 11 | //! [ref_count] |
| 12 | auto observable = rpp::source::create<int>([](const auto& observer) { |
| 13 | std::cout << "SUBSCRIBE" << std::endl; |
| 14 | for (int i = 0; i < 3; ++i) |
| 15 | { |
| 16 | observer.on_next(i); |
| 17 | } |
| 18 | observer.on_completed(); |
| 19 | }) |
| 20 | | rpp::operators::multicast(); |
| 21 | |
| 22 | std::cout << "subscribe first" << std::endl; |
| 23 | observable.subscribe([](int v) { std::cout << "#1 " << v << std::endl; }); |
| 24 | // No Output |
| 25 | |
| 26 | std::cout << "subscribe with ref_count" << std::endl; |
| 27 | observable.ref_count().subscribe([](int v) { std::cout << "#2 " << v << std::endl; }); |
| 28 | // Output: |
| 29 | // subscribe first |
| 30 | // subscribe with ref_count |
| 31 | // SUBSCRIBE |
| 32 | // #1 0 |
| 33 | // #2 0 |
| 34 | // #1 1 |
| 35 | // #2 1 |
| 36 | // #1 2 |
| 37 | // #2 2 |
| 38 | |
| 39 | //! [ref_count] |
| 40 | } |
| 41 | { |
| 42 | //! [ref_count_operator] |
| 43 | auto observable = rpp::source::just(1, 2, 3) | rpp::operators::multicast(); |
| 44 | observable.subscribe([](int v) { std::cout << "#1 " << v << std::endl; }); |
| 45 | // No Output |
| 46 | |
| 47 | observable | rpp::ops::ref_count() | rpp::ops::subscribe([](int v) { std::cout << "#2 " << v << std::endl; }); |
| 48 | // Output: |
| 49 | // #1 1 |
| 50 | // #2 1 |
| 51 | // #1 2 |
| 52 | // #2 2 |
| 53 | // #1 3 |
| 54 | // #2 3 |
| 55 | |
| 56 | //! [ref_count_operator] |
| 57 | } |
| 58 | return 0; |
| 59 | } |