File: generators.adoc

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (194 lines) | stat: -rw-r--r-- 4,501 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
[#generator]
== cobalt/generator.hpp

A generator is an eager coroutine that can `co_await` and `co_yield` values to the caller.

[source,cpp]
----
cobalt::generator<int> example()
{
  printf("In coro 1\n");
  co_yield 2;
  printf("In coro 3\n");
  co_return 4;
}

cobalt::main co_main(int argc, char * argv[])
{
  printf("In main 0\n");
  auto f = example(); // call and let it run until the first co_yield
  printf("In main 1\n");
  printf("In main %d\n", co_await f);
  printf("In main %d\n", co_await f);
  return 0;
}
----

Which will generate the following output

 In main 0
 In coro 1
 In main 1
 In main 2
 In coro 3
 In main 4


ifdef::generate-diagram[]
[mermaid, target=generators1]
----
sequenceDiagram
    participant main;
    Note left of main: "In main 0"
    main->>+example: example()
    Note right of example: "In coro 1"
    example-->>main: co_yield 2
    Note left of main: "In main 2"
    main-->>+example: co_await f
    Note right of example: "In coro 3"
    example->>main: co_return 3
    Note left of main: "In main 4"
----
endif::[]

ifndef::generate-diagram[]
image::generators1.png[]
endif::[]

Values can be pushed into the generator, when `Push` (the second template parameter) is set to non-void:


[source,cpp]
----
cobalt::generator<int, int> example()
{
  printf("In coro 1\n");
  int i =  co_yield 2;
  printf("In coro %d\n", i);
  co_return 4;
}

cobalt::main co_main(int argc, char * argv[])
{
  printf("In main 0\n");
  auto f = example(); // call and let it run until the first co_yield
  printf("In main %d\n", co_await f(3)); // <1>
  co_return 0;
}
----
<1> The pushed value gets passed through `operator()` to the result of `co_yield`.

Which will generate the following output

 In main 0
 In coro 1
 In main 2
 In coro 3

[#initial]
=== Lazy

A generator can be turned lazy by awaiting initial.
This `co_await` expression will produce the `Push` value.
This means the generator will wait until it's awaited for the first time,
and then process the newly pushed value and resume at the next co_yield.

[source,cpp]
----
cobalt::generator<int, int> example()
{
  int v = co_await cobalt::this_coro::initial;
  printf("In coro %d\n", v);
  co_yield 2;
  printf("In coro %d\n", v);
  co_return 4;
}

cobalt::main co_main(int argc, char * argv[])
{
  printf("In main 0\n");
  auto f = example(); // call and let it run until the first co_yield
  printf("In main 1\n"); // < this is now before the co_await initial
  printf("In main %d\n", co_await f(1));
  printf("In main %d\n", co_await f(3));
  return 0;
}
----

Which will generate the following output

 In main 0
 In main 1
 In coro 1
 In main 2
 In coro 3
 In main 4

ifdef::generate-diagram[]
[mermaid, target=generators2]
----
sequenceDiagram
    participant main;
    Note left of main: "In main 0"
    main->>+example: example()
    Note right of example: "In coro 1"
    example-->>main: co_yield 2
    Note left of main: "In main 2"
    main-->>+example: co_await f
    Note right of example: "In coro 3"
    example->>main: co_return 3
    Note left of main: "In main 4"
----
endif::[]

ifndef::generate-diagram[]
image::generators2.png[]
endif::[]

[#generator-executor]
=== Executor

The executor is taken from the `thread_local` <<this_thread, get_executor>> function, unless a `asio::executor_arg` is used
in any position followed by the executor argument.

[source, cpp]
----
cobalt::generator<int> my_gen(asio::executor_arg_t, asio::io_context::executor_type exec_to_use);
----

[#generator-allocator]
=== Memory Resource

The memory resource is taken from the `thread_local` <<this_thread, get_default_resource>> function,
unless a `std::allocator_arg` is used in any position followed by a `polymorphic_allocator` argument.

[source, cpp]
----
cobalt::generator<int> my_gen(std::allocator_arg_t, pmr::polymorphic_allocator<void> alloc);
----

[#generator-outline]
=== Outline

[source,cpp,subs=+quotes]
----
include::../../include/boost/cobalt/generator.hpp[tag=outline]
----
<1> This allows code like `while (gen) co_await gen:`
<2> Supports <<interrupt_await>>
<3> A cancelled generator maybe be resumable

NOTE: The destruction of an eager generator will be deferred, a lazy will be destroyed immediately.

[#generator-promise]
=== Promise

The generator promise has the following properties.

- <<promise_memory_resource_base>>
- <<promise_cancellation_base>>
- <<promise_throw_if_cancelled_base>>
- <<enable_awaitables>>
- <<enable_await_allocator>>
- <<enable_await_executor>>