У меня есть ситуация, когда у меня есть класс, в котором каждый метод - это коратика, но на уровне бизнес -логики ограничен, так что только одна коратика может быть «в полете», так сказать, за раз.
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();
}
};
У меня есть ситуация, когда у меня есть класс, в котором каждый метод - это коратика, но на уровне бизнес -логики ограничен, так что только одна коратика может быть «в полете», так сказать, за раз.[code]class MyClass;
class CoroutineReturnType { std::coroutine_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(); } }; [/code] Есть предложения?>