Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <boost/coroutine/all.hpp>
  2. #include <functional>
  3. #include <iostream>
  4.  
  5. using namespace boost::coroutines;
  6.  
  7.  
  8. class Stmt
  9. {
  10. public:
  11.     virtual ~Stmt()
  12.     {
  13.     }
  14.  
  15.     virtual void execute(coroutine<int>::push_type& sink) = 0;
  16. };
  17.  
  18.  
  19. class WhileStmt : public Stmt
  20. {
  21. public:
  22.     void execute(coroutine<int>::push_type& sink) override
  23.     {
  24.         for (int n = 1; n <= 5; ++n)
  25.             sink(n);
  26.  
  27.         sink(-1);
  28.     }
  29. };
  30.  
  31.  
  32. class WhileStmt2 : public Stmt
  33. {
  34. public:
  35.     void execute(coroutine<int>::push_type& sink) override
  36.     {
  37.         WhileStmt subStmt;
  38.         coroutine<int>::pull_type subSource{
  39.             std::bind(&WhileStmt::execute, subStmt, std::placeholders::_1)};
  40.  
  41.         while (subSource.get() != -1)
  42.         {
  43.             sink(subSource.get());
  44.  
  45.             for (int n = 1; n <= 2; ++n)
  46.                 sink(10);
  47.  
  48.             subSource();
  49.         }
  50.  
  51.         sink(-1);
  52.     }
  53. };
  54.  
  55.  
  56. int main()
  57. {
  58.     Stmt* stmt = new WhileStmt2();
  59.     coroutine<int>::pull_type source{std::bind(&Stmt::execute, stmt, std::placeholders::_1)};
  60.  
  61.     while (source.get() != -1)
  62.     {
  63.         std::cout << source.get() << std::endl;
  64.         source();
  65.     }
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement