| // generator/continuation for C++ | |
| // author: Andrew Fedoniouk @ terrainformatica.com | |
| // idea borrowed from: "coroutines in C" Simon Tatham, | |
| // http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html | |
| // BSD license | |
| template<typename T> | |
| struct _generator | |
| { | |
| T* _stack; | |
| int _line; | |
| _generator():_stack(0), _line(-1) {} | |
| void _push() { T* n = new T; *n = *static_cast<T*>(this); _stack = n; } | |
| bool _pop() { if(!_stack) return false; T* t = _stack; *static_cast<T*>(this) = *_stack; t->_stack = 0; delete t; return true; } | |
| ~_generator() { while(_pop()); } | |
| }; | |