-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLazyDataCoroutine.h
More file actions
63 lines (47 loc) · 1.4 KB
/
LazyDataCoroutine.h
File metadata and controls
63 lines (47 loc) · 1.4 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
55
56
57
58
59
60
61
62
63
#pragma once
#include <experimental\coroutine>
template<typename T>
class LazyDataCoroutine
{
public:
struct promise_type
{
LazyDataCoroutine get_return_object() {
return LazyDataCoroutine(HandleType::from_promise(*this));
}
std::experimental::suspend_never initial_suspend() { return {}; }
std::experimental::suspend_always final_suspend() { return {}; }
void return_value(T value) {
// Do nothing here because the value is set through the setData member
}
void unhandled_exception() {}
};
using HandleType = std::experimental::coroutine_handle<promise_type>;
std::experimental::coroutine_handle<> m_waitingCoroutine;
bool m_set = false;
T m_value;
LazyDataCoroutine() = default;
LazyDataCoroutine(T value):
m_value(value),
m_set(true) {
}
LazyDataCoroutine(HandleType h) {
}
bool await_ready() {
cout << "LazyDataCoroutine::await_ready\n";
return m_set;
}
auto await_resume() {
cout << "LazyDataCoroutine::await_resume\n";
const auto returnValue = m_value;
return returnValue;
}
void await_suspend(coroutine_handle<> waitingCoroutine) {
cout << "LazyDataCoroutine::await_suspend\n";
waitingCoroutine.resume();
}
void setData(T value) {
m_value = value;
m_set = true;
}
};