-
Notifications
You must be signed in to change notification settings - Fork 88
/
rapidxml_generator.hpp
80 lines (61 loc) · 1.9 KB
/
rapidxml_generator.hpp
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
//
// Created by dave on 29/07/2024.
//
#ifndef RAPIDXML_RAPIDXML_GENERATOR_HPP
#define RAPIDXML_RAPIDXML_GENERATOR_HPP
#include <coroutine>
#include <iterator>
namespace rapidxml {
template<typename T>
class generator {
public:
using value_pointer = std::remove_reference<T>::type *;
struct handle_type;
struct promise_type {
value_pointer value;
std::suspend_always yield_value(T & v) {
value = &v;
return {};
}
std::suspend_never initial_suspend() {
return {};
}
std::suspend_always final_suspend() noexcept {
return {}; // Change this to std::suspend_always
}
void return_void() {}
void unhandled_exception() {
std::terminate();
}
generator get_return_object() {
return generator{handle_type{handle_type::from_promise(*this)}};
}
};
struct handle_type : std::coroutine_handle<promise_type> {
explicit handle_type(std::coroutine_handle<promise_type> && h) : std::coroutine_handle<promise_type>(std::move(h)) {}
T &operator*() {
return *(this->promise().value);
}
void operator++() {
this->resume();
}
bool operator!=(std::default_sentinel_t) const {
return !this->done();
}
};
explicit generator(handle_type h) : m_handle(h) {}
~generator() {
if (m_handle)
m_handle.destroy();
}
handle_type begin() {
return m_handle;
}
std::default_sentinel_t end() {
return std::default_sentinel;
}
private:
handle_type m_handle{};
};
}
#endif //RAPIDXML_RAPIDXML_GENERATOR_HPP