16 #if defined(LIBCOPP_MACRO_ENABLE_STD_COROUTINE) && LIBCOPP_MACRO_ENABLE_STD_COROUTINE
19 class sample_message_t {
23 sample_message_t() : ret_code(0) {}
24 sample_message_t(
int c) : ret_code(c) {}
25 sample_message_t(
const sample_message_t &) =
default;
26 sample_message_t &operator=(
const sample_message_t &) =
default;
27 sample_message_t(sample_message_t &&) =
default;
28 sample_message_t &operator=(sample_message_t &&) =
default;
29 ~sample_message_t() {}
32 # define SAMPLE_TIMEOUT_ERROR_CODE (-500)
34 LIBCOPP_COPP_NAMESPACE_BEGIN
36 struct std_coroutine_default_error_transform<sample_message_t> {
37 using type = sample_message_t;
38 type operator()(promise_status in)
const {
39 if (in == promise_status::kTimeout) {
40 return sample_message_t{SAMPLE_TIMEOUT_ERROR_CODE};
42 return sample_message_t{
static_cast<int>(in)};
45 LIBCOPP_COPP_NAMESPACE_END
47 using int_generator = copp::generator_future<int>;
48 std::list<int_generator::context_pointer_type> g_int_executor;
49 using custom_generator = copp::generator_future<sample_message_t>;
50 std::list<custom_generator::context_pointer_type> g_sample_executor;
52 static void int_generator_callback(int_generator::context_pointer_type ctx) {
53 g_int_executor.emplace_back(std::move(ctx));
56 static void custom_generator_callback(custom_generator::context_pointer_type ctx) {
57 g_sample_executor.emplace_back(std::move(ctx));
60 static copp::callable_future<int> coroutine_simulator_rpc_integer_l2() {
61 auto result = co_await int_generator{int_generator_callback};
65 static copp::callable_future<void> coroutine_simulator_rpc_integer() {
66 auto result = co_await coroutine_simulator_rpc_integer_l2();
67 std::cout <<
"int generator is killed with code: " << result << std::endl;
71 static copp::callable_future<int> coroutine_simulator_rpc_custom_l2() {
72 auto result = co_await custom_generator{custom_generator_callback};
73 co_return result.ret_code;
76 static copp::callable_future<void> coroutine_simulator_rpc_custom() {
77 auto result = co_await coroutine_simulator_rpc_custom_l2();
78 std::cout <<
"custom generator is killed with code: " << result << std::endl;
84 auto f1 = coroutine_simulator_rpc_integer();
85 f1.kill(copp::promise_status::kCancle,
true);
86 std::cout <<
"int generator is killed" << std::endl;
89 auto f2 = coroutine_simulator_rpc_custom();
90 f2.kill(copp::promise_status::kTimeout,
true);
91 std::cout <<
"custom generator is killed" << std::endl;
96 puts(
"this sample require cotask enabled and compiler support c++20 coroutine");