Created
October 28, 2012 13:05
-
-
Save ArGxento/3968536 to your computer and use it in GitHub Desktop.
note about pseudo-coroutine in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <boost/optional.hpp> | |
boost::optional<int> coro (int const a) | |
{ | |
#define yield(arg) ___lino = __LINE__;return boost::optional<int>(arg);case __LINE__: | |
#define yield_return ___lino = 0;return boost::optional<int>();case __LINE__: | |
static int ___lino = 0; | |
//decl of vars | |
//-------------------- | |
static int i; | |
//-------------------- | |
switch(___lino){ | |
case 0: | |
//----init of vars | |
i = 0; | |
//------------------ | |
for(i=0; i < 42; i++){ | |
if(i & 1){ | |
yield(i); | |
} else { | |
yield(i*2); | |
} | |
} | |
yield_return; | |
} | |
return boost::optional<int>(); | |
#undef yield | |
#undef yield_return | |
} | |
int main() | |
{ | |
while(boost::optional<int> i = coro(0)){ | |
std::cout << *i << std::endl; | |
} | |
while(boost::optional<int> i = coro(0)){ | |
std::cout << *i * 2 << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment