|
| 1 | +# C++ 코드 조각 |
| 2 | + |
| 3 | + |
| 4 | +## coroutine |
| 5 | + |
| 6 | +### 설명 |
| 7 | + |
| 8 | +* [https://cppreference.com/w/cpp/coroutine.html](https://cppreference.com/w/cpp/coroutine.html) |
| 9 | + |
| 10 | + |
| 11 | +### 예제 |
| 12 | + |
| 13 | +```C++ |
| 14 | + |
| 15 | +#include <coroutine> |
| 16 | +#include <iostream> |
| 17 | +#include <optional> |
| 18 | + |
| 19 | +template<std::movable T> |
| 20 | +class Generator |
| 21 | +{ |
| 22 | +public: |
| 23 | + struct promise_type |
| 24 | + { |
| 25 | + Generator<T> get_return_object() |
| 26 | + { |
| 27 | + return Generator{Handle::from_promise(*this)}; |
| 28 | + } |
| 29 | + static std::suspend_always initial_suspend() noexcept |
| 30 | + { |
| 31 | + return {}; |
| 32 | + } |
| 33 | + static std::suspend_always final_suspend() noexcept |
| 34 | + { |
| 35 | + return {}; |
| 36 | + } |
| 37 | + std::suspend_always yield_value(T value) noexcept |
| 38 | + { |
| 39 | + current_value = std::move(value); |
| 40 | + return {}; |
| 41 | + } |
| 42 | + // Disallow co_await in generator coroutines. |
| 43 | + void await_transform() = delete; |
| 44 | + [[noreturn]] |
| 45 | + static void unhandled_exception() { throw; } |
| 46 | + |
| 47 | + std::optional<T> current_value; |
| 48 | + }; |
| 49 | + |
| 50 | + using Handle = std::coroutine_handle<promise_type>; |
| 51 | + |
| 52 | + explicit Generator(const Handle coroutine) : |
| 53 | + m_coroutine{coroutine} |
| 54 | + {} |
| 55 | + |
| 56 | + Generator() = default; |
| 57 | + ~Generator() |
| 58 | + { |
| 59 | + if (m_coroutine) |
| 60 | + m_coroutine.destroy(); |
| 61 | + } |
| 62 | + |
| 63 | + Generator(const Generator&) = delete; |
| 64 | + Generator& operator=(const Generator&) = delete; |
| 65 | + |
| 66 | + Generator(Generator&& other) noexcept : |
| 67 | + m_coroutine{other.m_coroutine} |
| 68 | + { |
| 69 | + other.m_coroutine = {}; |
| 70 | + } |
| 71 | + Generator& operator=(Generator&& other) noexcept |
| 72 | + { |
| 73 | + if (this != &other) |
| 74 | + { |
| 75 | + if (m_coroutine) |
| 76 | + m_coroutine.destroy(); |
| 77 | + m_coroutine = other.m_coroutine; |
| 78 | + other.m_coroutine = {}; |
| 79 | + } |
| 80 | + return *this; |
| 81 | + } |
| 82 | + |
| 83 | + // Range-based for loop support. |
| 84 | + class Iter |
| 85 | + { |
| 86 | + public: |
| 87 | + void operator++() |
| 88 | + { |
| 89 | + m_coroutine.resume(); |
| 90 | + } |
| 91 | + const T& operator*() const |
| 92 | + { |
| 93 | + return *m_coroutine.promise().current_value; |
| 94 | + } |
| 95 | + bool operator==(std::default_sentinel_t) const |
| 96 | + { |
| 97 | + return !m_coroutine || m_coroutine.done(); |
| 98 | + } |
| 99 | + |
| 100 | + explicit Iter(const Handle coroutine) : |
| 101 | + m_coroutine{coroutine} |
| 102 | + {} |
| 103 | + |
| 104 | + private: |
| 105 | + Handle m_coroutine; |
| 106 | + }; |
| 107 | + |
| 108 | + Iter begin() |
| 109 | + { |
| 110 | + if (m_coroutine) |
| 111 | + m_coroutine.resume(); |
| 112 | + return Iter{m_coroutine}; |
| 113 | + } |
| 114 | + |
| 115 | + std::default_sentinel_t end() { return {}; } |
| 116 | + |
| 117 | +private: |
| 118 | + Handle m_coroutine; |
| 119 | +}; |
| 120 | + |
| 121 | +template<std::integral T> |
| 122 | +Generator<T> range(T first, const T last) |
| 123 | +{ |
| 124 | + while (first < last) |
| 125 | + co_yield first++; |
| 126 | +} |
| 127 | + |
| 128 | +int main() |
| 129 | +{ |
| 130 | + for (const char i : range(65, 91)) |
| 131 | + std::cout << i << ' '; |
| 132 | + std::cout << '\n'; |
| 133 | +} |
| 134 | + |
| 135 | +``` |
0 commit comments