Skip to content

Instantly share code, notes, and snippets.

@sean-parent
Created December 18, 2023 05:09
Show Gist options
  • Save sean-parent/c483fa71ae4e3bc20a462b78b7f0195e to your computer and use it in GitHub Desktop.
Save sean-parent/c483fa71ae4e3bc20a462b78b7f0195e to your computer and use it in GitHub Desktop.
teplate <class T>
auto split(future<T>&&) -> pair<future<T>, future<T>>;
// Q: Is the only way to attach a continuation to spawn/await?
class application {
//...
future<document> _document;
vector<future<void>> _pending; // async scope
// implementation of save with split - not const
void save() {
auto [f1, f2] split(_document);
_document = move(f1); // requires save is mutable - copy would not
_pending.push_back(spawn([_f = move(f2)] {
_f.await().save();
});
}
// implementation of save with copy
void save() const {
_pending.push_back(spawn([_f = copy(_document)] {
_f.await().save();
});
}
// implementation of save with continuations - stlab syntax
void save() const {
_pending.push_back(copy(_document) | [](const document& d){
d.save();
});
}
void blur() {
_document = spawn([_f = move(_document)]{
_f.await().blur();
});
}
};
application _application;
int main() {
while (true) { // run loop
char command;
cin >> command;
switch (command) {
case 's': _application.save(); break;
case 'b': _application.blur(); break;
default: break; // do nothing
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment