libcopp  2.2.0
sample_readme_8.cpp
Go to the documentation of this file.
1 /*
2  * sample_readme_9.cpp
3  *
4  * Created on: 2020-05-20
5  * Author: owent
6  *
7  * Released under the MIT license
8  */
9 
10 #include <iostream>
11 
12 // include manager header file
15 
16 #if defined(LIBCOPP_MACRO_ENABLE_STD_COROUTINE) && LIBCOPP_MACRO_ENABLE_STD_COROUTINE
17 
18 // ============================ types for task and generator ============================
19 class sample_message_t {
20  public:
21  int ret_code;
22 
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() {}
30 };
31 
32 # define SAMPLE_TIMEOUT_ERROR_CODE (-500)
33 
34 LIBCOPP_COPP_NAMESPACE_BEGIN
35 template <>
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};
41  }
42  return sample_message_t{static_cast<int>(in)};
43  }
44 };
45 LIBCOPP_COPP_NAMESPACE_END
46 
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;
51 
52 static void int_generator_callback(int_generator::context_pointer_type ctx) {
53  g_int_executor.emplace_back(std::move(ctx));
54 }
55 
56 static void custom_generator_callback(custom_generator::context_pointer_type ctx) {
57  g_sample_executor.emplace_back(std::move(ctx));
58 }
59 
60 static copp::callable_future<int> coroutine_simulator_rpc_integer_l2() {
61  auto result = co_await int_generator{int_generator_callback};
62  co_return result;
63 }
64 
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;
68  co_return;
69 }
70 
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;
74 }
75 
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;
79  co_return;
80 }
81 
82 int main() {
83  // sample for await generator and timeout
84  auto f1 = coroutine_simulator_rpc_integer();
85  f1.kill(copp::promise_status::kCancle, true);
86  std::cout << "int generator is killed" << std::endl;
87 
88  // sample for await task and timeout
89  auto f2 = coroutine_simulator_rpc_custom();
90  f2.kill(copp::promise_status::kTimeout, true);
91  std::cout << "custom generator is killed" << std::endl;
92  return 0;
93 }
94 #else
95 int main() {
96  puts("this sample require cotask enabled and compiler support c++20 coroutine");
97  return 0;
98 }
99 #endif
int main()