11. Boost.Coroutine の使い方
using namespace boost::coroutines;
typedef generator<int> gen_type;
int f(gen_type::self& self) {
int n = 0;
while (true) ++n, self.yield(n * n);
self.exit();
}
int main() {
gen_type gen(f);
while (gen != generator_type()) {
*gen; // 1, 4, 9, 16, ...
++gen;
}
}
12. Boost.Coroutine の使い方
void bullet(point pt)
for (int i = 0; i
for (int i = 0; i
for (int i = 0; i
}
{
< 10; i++) ++pt.x;
< 20; i++) ++pt.y;
< 30; i++) --pt.x;
・こんな弾の動きを作りたい。けど、
void bullet(int& state, point& pt) {
if (0 <= state && state < 10) ++pt.x;
if (10 <= state && state < 30) ++pt.y;
if (30 <= state && state < 60) --pt.x;
++state;
}
・こんな非直感的なコードを書かないといけない
13. Boost.Coroutine の使い方
point bullet(coro_type::self& self, point pt) {
for (int i = 0; i < 10; i++) ++pt.x, self.yield(pt);
for (int i = 0; i < 20; i++) ++pt.y, self.yield(pt);
for (int i = 0; i < 30; i++) --pt.x, self.yield(pt);
self.exit();
}
class bullet_task {
coro_type coro_;
public:
bullet_task()
: coro_(boost::bind(bullet, _1, point(10, 20)){}
bool move() {
point pt = coro_(std::nothrow);
return coro_;
}
};