Skip to content

Commit f046646

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

File tree

4 files changed

+525
-0
lines changed

4 files changed

+525
-0
lines changed

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: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)