|
| 1 | +// |
| 2 | +// Copyright (c) 2026 Vinnie Falco (vinnie.falco@gmail.com) |
| 3 | +// |
| 4 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | +// Official repository: https://github.com/cppalliance/capy |
| 8 | +// |
| 9 | + |
| 10 | +#include "awaitable_sender.hpp" |
| 11 | + |
| 12 | +#include <boost/capy.hpp> |
| 13 | + |
| 14 | +#include <beman/execution/execution.hpp> |
| 15 | + |
| 16 | +#include <chrono> |
| 17 | +#include <iostream> |
| 18 | +#include <latch> |
| 19 | +#include <system_error> |
| 20 | +#include <thread> |
| 21 | + |
| 22 | +namespace capy = boost::capy; |
| 23 | +namespace ex = beman::execution; |
| 24 | + |
| 25 | +// A receiver whose environment carries a Capy executor. |
| 26 | +// Completion signals a latch so main() can wait. |
| 27 | +struct demo_receiver |
| 28 | +{ |
| 29 | + using receiver_concept = ex::receiver_t; |
| 30 | + |
| 31 | + capy::io_sender_env env_; |
| 32 | + std::latch* done_; |
| 33 | + |
| 34 | + auto get_env() const noexcept -> capy::io_sender_env |
| 35 | + { |
| 36 | + return env_; |
| 37 | + } |
| 38 | + |
| 39 | + void set_value() && noexcept |
| 40 | + { |
| 41 | + std::cout |
| 42 | + << " set_value on thread " |
| 43 | + << std::this_thread::get_id() << "\n"; |
| 44 | + done_->count_down(); |
| 45 | + } |
| 46 | + |
| 47 | + void set_error(std::error_code ec) && noexcept |
| 48 | + { |
| 49 | + std::cerr << " error: " << ec.message() << "\n"; |
| 50 | + done_->count_down(); |
| 51 | + } |
| 52 | + |
| 53 | + void set_error(std::exception_ptr ep) && noexcept |
| 54 | + { |
| 55 | + try { std::rethrow_exception(ep); } |
| 56 | + catch (std::exception const& e) { |
| 57 | + std::cerr << " error: " << e.what() << "\n"; |
| 58 | + } |
| 59 | + done_->count_down(); |
| 60 | + } |
| 61 | + |
| 62 | + void set_stopped() && noexcept |
| 63 | + { |
| 64 | + std::cout << " stopped\n"; |
| 65 | + done_->count_down(); |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +int main() |
| 70 | +{ |
| 71 | + using namespace std::chrono_literals; |
| 72 | + |
| 73 | + std::cout |
| 74 | + << "main thread: " |
| 75 | + << std::this_thread::get_id() << "\n"; |
| 76 | + |
| 77 | + // Capy execution context (provides timer service, etc.) |
| 78 | + capy::thread_pool pool; |
| 79 | + |
| 80 | + std::latch done(1); |
| 81 | + |
| 82 | + // Build a sender from a Capy IoAwaitable |
| 83 | + auto sndr = capy::as_sender(capy::delay(500ms)); |
| 84 | + |
| 85 | + // Connect with a receiver whose environment carries |
| 86 | + // the Capy thread_pool executor |
| 87 | + auto op = ex::connect( |
| 88 | + std::move(sndr), |
| 89 | + demo_receiver{ |
| 90 | + {pool.get_executor(), std::stop_token{}}, |
| 91 | + &done}); |
| 92 | + |
| 93 | + std::cout << " starting delay...\n"; |
| 94 | + ex::start(op); |
| 95 | + |
| 96 | + done.wait(); |
| 97 | + std::cout << " delay completed\n"; |
| 98 | +} |
0 commit comments