-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.h
88 lines (70 loc) · 1.69 KB
/
generator.h
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
#pragma once
#include <experimental/coroutine>
#include <stdexcept>
template<typename T>
class Generator
{
public:
class promise_type;
using CoroHandle = std::experimental::coroutine_handle<promise_type>;
Generator() = default;
Generator(Generator &&other);
Generator(const Generator &) = delete;
~Generator();
T nextValue();
bool hasNextValue() const;
private:
Generator(const CoroHandle &handle);
CoroHandle m_handle;
};
template<typename T>
class Generator<T>::promise_type
{
public:
Generator<T> get_return_object() { return Generator<T>::CoroHandle::from_promise(*this); }
auto initial_suspend() { return std::experimental::suspend_always(); }
auto final_suspend() { return std::experimental::suspend_always(); }
auto yield_value(T val) { m_currentValue = val; return std::experimental::suspend_always(); }
//void return_value(T val) { m_currentValue = val;}
void unhandled_exception() { m_exception = std::current_exception(); }
T getValue();
private:
T m_currentValue;
std::exception_ptr m_exception;
};
template<typename T>
Generator<T>::Generator(const Generator<T>::CoroHandle &handle)
: m_handle(handle)
{
}
template<typename T>
Generator<T>::Generator(Generator &&other)
{
std::swap(this->m_handle, other.m_handle);
}
template<typename T>
Generator<T>::~Generator()
{
if (m_handle) {
m_handle.destroy();
}
}
template<typename T>
bool Generator<T>::hasNextValue() const
{
return m_handle && !m_handle.done();
}
template<typename T>
T Generator<T>::nextValue()
{
m_handle.resume();
return m_handle.promise().getValue();
}
template<typename T>
T Generator<T>::promise_type::getValue()
{
if (m_exception) {
std::rethrow_exception(m_exception);
}
return m_currentValue;
}