Код: Выделить всё
class MyClass;
class CoroutineReturnType {
std::coroutine_handle handle;
public:
CoroutineReturnType( std::coroutine_handle handle) : handle(handle) {}
struct promise_type {
CoroutineReturnType get_return_object() {
auto result = CoroutineReturnType( from_promise(*this));
// Magic happens here...
return result;
}
void initial_suspend() {
// ...or here
// such that currentlyExecutingMethod below
// holds a pointer to the currently
// executing coroutine return type.
}
}
};
class MyClass {
public:
CoroutineReturnType * currentlyExecutingMethod;
CoroutineReturnType myMethod() {
// or maybe, less satisfactory, the magic could happen here.
// Do stuff
co_await std::suspend_always{};
// Do more stuff
co_return;
};
// ... more similar methods.
// Something external calls this to kick the currently executing method along...
void externalCallback()
{
currentlyExecutingMethod->handle->resume();
}
};
Подробнее здесь: https://stackoverflow.com/questions/797 ... ct-methods