Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <boost/coroutine/all.hpp>
- #include <functional>
- #include <iostream>
- using namespace boost::coroutines;
- class Stmt
- {
- public:
- virtual ~Stmt()
- {
- }
- virtual void execute(coroutine<int>::push_type& sink) = 0;
- };
- class WhileStmt : public Stmt
- {
- public:
- void execute(coroutine<int>::push_type& sink) override
- {
- for (int n = 1; n <= 5; ++n)
- sink(n);
- sink(-1);
- }
- };
- class WhileStmt2 : public Stmt
- {
- public:
- void execute(coroutine<int>::push_type& sink) override
- {
- WhileStmt subStmt;
- coroutine<int>::pull_type subSource{
- std::bind(&WhileStmt::execute, subStmt, std::placeholders::_1)};
- while (subSource.get() != -1)
- {
- sink(subSource.get());
- for (int n = 1; n <= 2; ++n)
- sink(10);
- subSource();
- }
- sink(-1);
- }
- };
- int main()
- {
- Stmt* stmt = new WhileStmt2();
- coroutine<int>::pull_type source{std::bind(&Stmt::execute, stmt, std::placeholders::_1)};
- while (source.get() != -1)
- {
- std::cout << source.get() << std::endl;
- source();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement