Generators in C++ revisited.

Previous version of generators has a design problem – it required some special stop value. That is the same kind of problem as with iterators in C++ – they require special end() value. But in some cases it is not even possible to choose such a value.

So is this new version:


// 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

  //++ coroutine, generator, continuation for C++

  struct _generator
  {
    int _line;
    _generator():_line(-1) {}
  };

  #define $generator(NAME) struct NAME : public _generator

  #define $emit(T) bool operator()(T& _rv) { \
                      if(_line < 0) { _line=0;}\
                      switch(_line) { case 0:;

  #define $stop  } _line = 0; return false; }

  #define $yield(V)     \
          do {\
              _line=__LINE__;\
              _rv = (V); return true; case __LINE__:;\
          } while (0)

  //-- coroutine, generator, continuation for C++

Implementation of typical iterator practically is the same an in previous version except of $stop is being used without any parameter now:

#include "generator.h"

$generator(descent)
{
   int i;
   $emit(int) // will emit int values. Start of body of the generator.
      for (i = 10; i > 0; i–)
         $yield(i); // a.k.a. yield in Python, 
                      // returns next number in [1..10], reversed.
   $stop; // stop, end of sequence. End of body of the generator.
};

But its usage is a bit different and is close to JavaScript for(var in sequence) statement:

int main(int argc, char* argv[])
{
  descent gen;
  for(int n; gen(n);) // "get next" generator invocation
    printf("next number is %d\n", n);
  return 0;
}

And here is version of the generator that supports restart (recursive call) - needed for walking through tree alike (recursive) data structures. It uses allocations on the heap that I think is overkill for such constructions.

4 Replies to “Generators in C++ revisited.”

  1. Hello. I’m software engineer and these days seek to use ‘continuation’ in c++ for single-threaded net-message process that needs to be waited for response from another machine and I tried that in ugly MACRO.

    But yesterday I found your article and applied your generator code. It works greatly for me.
    Thank you and I like that code very much 🙂

    I uploaded my test continuation-applied message processing frameworks(with your generator code) in my blog. (sorry because it was published in KOREAN)

    Thanks again.

Comments are closed.