Skip to content

Commit 60bf781

Browse files
committed
Add awaitable-sender
1 parent 3f47cfc commit 60bf781

4 files changed

Lines changed: 450 additions & 0 deletions

File tree

example/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_subdirectory(timeout-cancellation)
2323
add_subdirectory(type-erased-echo)
2424
add_subdirectory(when-any-cancellation)
2525
add_subdirectory(sender-bridge)
26+
add_subdirectory(awaitable-sender)
2627

2728
if(TARGET Boost::asio)
2829
add_subdirectory(asio)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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(FetchContent)
11+
12+
FetchContent_Declare(
13+
beman-execution
14+
GIT_REPOSITORY https://github.com/bemanproject/execution.git
15+
GIT_TAG main
16+
SYSTEM
17+
)
18+
FetchContent_MakeAvailable(beman-execution)
19+
20+
file(GLOB_RECURSE PFILES CONFIGURE_DEPENDS *.cpp *.hpp
21+
CMakeLists.txt
22+
Jamfile)
23+
24+
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} PREFIX "" FILES ${PFILES})
25+
26+
add_executable(capy_example_awaitable_sender ${PFILES})
27+
28+
set_property(TARGET capy_example_awaitable_sender
29+
PROPERTY FOLDER "examples")
30+
31+
target_compile_features(capy_example_awaitable_sender
32+
PRIVATE cxx_std_23)
33+
34+
target_link_libraries(capy_example_awaitable_sender
35+
Boost::capy
36+
beman::execution_headers)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 <thread>
20+
21+
namespace capy = boost::capy;
22+
namespace ex = beman::execution;
23+
24+
// A receiver whose environment carries a Capy executor.
25+
// Completion signals a latch so main() can wait.
26+
struct demo_receiver
27+
{
28+
using receiver_concept = ex::receiver_t;
29+
30+
capy::io_sender_env env_;
31+
std::latch* done_;
32+
33+
auto get_env() const noexcept -> capy::io_sender_env
34+
{
35+
return env_;
36+
}
37+
38+
void set_value() && noexcept
39+
{
40+
std::cout
41+
<< " set_value on thread "
42+
<< std::this_thread::get_id() << "\n";
43+
done_->count_down();
44+
}
45+
46+
void set_error(std::exception_ptr ep) && noexcept
47+
{
48+
try { std::rethrow_exception(ep); }
49+
catch (std::exception const& e) {
50+
std::cerr << " error: " << e.what() << "\n";
51+
}
52+
done_->count_down();
53+
}
54+
55+
void set_stopped() && noexcept
56+
{
57+
std::cout << " stopped\n";
58+
done_->count_down();
59+
}
60+
};
61+
62+
int main()
63+
{
64+
using namespace std::chrono_literals;
65+
66+
std::cout
67+
<< "main thread: "
68+
<< std::this_thread::get_id() << "\n";
69+
70+
// Capy execution context (provides timer service, etc.)
71+
capy::thread_pool pool;
72+
73+
std::latch done(1);
74+
75+
// Build a sender from a Capy IoAwaitable
76+
auto sndr = capy::as_sender(capy::delay(500ms));
77+
78+
// Connect with a receiver whose environment carries
79+
// the Capy thread_pool executor
80+
auto op = ex::connect(
81+
std::move(sndr),
82+
demo_receiver{
83+
{pool.get_executor(), std::stop_token{}},
84+
&done});
85+
86+
std::cout << " starting delay...\n";
87+
ex::start(op);
88+
89+
done.wait();
90+
std::cout << " delay completed\n";
91+
}

0 commit comments

Comments
 (0)