libcopp 2.3.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sample_readme_7.cpp
Go to the documentation of this file.
1/*
2 * sample_readme_7.cpp
3 *
4 * Created on: 2020-05-22
5 * Author: owent
6 *
7 * Released under the MIT license
8 */
9
10#include <assert.h>
11#include <iostream>
12#include <string>
13
14// include manager header file
17
18#if defined(LIBCOPP_MACRO_ENABLE_STD_COROUTINE) && LIBCOPP_MACRO_ENABLE_STD_COROUTINE
19
20using my_generator = copp::generator_future<int>;
21std::list<my_generator::context_pointer_type> g_sample_executor;
22
23static void generator_callback(my_generator::context_pointer_type ctx) {
24 g_sample_executor.emplace_back(std::move(ctx));
25}
26
27static copp::callable_future<void> coroutine_simulator_rpc() {
28 my_generator generator_object{generator_callback};
29 auto value1 = co_await generator_object;
30 std::cout << "co_await named generator: " << value1 << std::endl;
31 auto value2 = co_await my_generator{generator_callback};
32 std::cout << "co_await temporary generator: " << value2 << std::endl;
33
34 generator_object.get_context()->reset_value();
35 auto value3 = co_await generator_object;
36 std::cout << "reset and co_await named generator again: " << value3 << std::endl;
37 co_return;
38}
39
40int main() {
41 int result = 191;
42 auto f = coroutine_simulator_rpc();
43
44 while (!g_sample_executor.empty()) {
45 auto ctx = g_sample_executor.front();
46 g_sample_executor.pop_front();
47 ctx->set_value(++result);
48 }
49 return 0;
50}
51#else
52int main() {
53 puts("this sample require cotask enabled and compiler support c++20 coroutine");
54 return 0;
55}
56#endif
int main()