-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathGenerator.cpp
More file actions
54 lines (48 loc) · 1.66 KB
/
Generator.cpp
File metadata and controls
54 lines (48 loc) · 1.66 KB
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
/*************************************************************************
> File Name: Generator.cpp
> Author: Netcan
> Blog: https://netcan.github.io/
> Mail: netcan1996@gmail.com
> Created Time: 2021-12-06 20:48
************************************************************************/
#include <iostream>
#include <coroutine>
#include <tuple>
#include <utility>
struct Generator {
struct promise_type;
using handle = std::coroutine_handle<promise_type>;
struct promise_type {
Generator get_return_object() { return {handle::from_promise(*this)}; }
auto initial_suspend() noexcept { return std::suspend_never{}; }
auto final_suspend() noexcept { return std::suspend_always{}; }
void unhandled_exception() { std::terminate(); }
void return_void() {}
auto yield_value(int value) {
current_value_ = value;
return std::suspend_always{};
}
int current_value_;
};
void next() { return coro_handle_.resume(); }
bool done() { return coro_handle_.done(); }
int current_value() { return coro_handle_.promise().current_value_; }
Generator(Generator&& rhs) noexcept:
coro_handle_(std::exchange(rhs.coro_handle_, {})) {}
~Generator() { if(coro_handle_) coro_handle_.destroy(); }
private:
Generator(handle h): coro_handle_(h) {}
handle coro_handle_;
};
Generator fibo() {
int a = 1, b = 1;
while (a < 1000000) {
co_yield a;
std::tie(a, b) = std::make_tuple(b, a + b);
}
co_return; // 可省略
}
int main() {
for (auto f = fibo(); !f.done(); f.next())
std::cout << f.current_value() << std::endl;
}