libostd/examples/coroutine2.cc

46 lines
991 B
C++

#include <ostd/io.hh>
#include <ostd/coroutine.hh>
using namespace ostd;
int main() {
auto f = [](auto yield) {
for (int i: range(5, 26, 5)) {
yield(i);
}
};
writeln("generator test");
for (int i: generator<int>{f}) {
writefln("generated: %s", i);
}
generator<int> x{f};
/* coroutine_context exists as a base type for any coroutine */
coroutine_context *ctx = &x;
writefln(
"generator is coroutine<int()>: %s",
bool(dynamic_cast<coroutine<int()> *>(ctx))
);
writefln(
"generator is generator<int>: %s",
bool(dynamic_cast<generator<int> *>(ctx))
);
generator<int> &gr = dynamic_cast<generator<int> &>(*ctx);
writefln("value of cast back generator: %s", gr.value());
}
/*
generator test
generated: 5
generated: 10
generated: 15
generated: 20
generated: 25
generator is coroutine<int()>: false
generator is generator<int>: true
value of cast back generator: 5
*/