Browse Source Download (without any required ccan dependencies)

Module:

generator

Summary:

generators for C

Author:

David Gibson <[email protected]>

Dependencies:

Description:

Generators are a limited form of coroutines, which provide a useful way of expressing certain problems, while being much simpler to understand than general coroutines.

Instead of returning a single value, a generator can "yield" a value at various points during its execution. Whenever it yields, the "calling" function resumes and obtains the newly yielded value to work with. When the caller asks for the next value from the generator, the generator resumes execution from the last yield and continues onto the next.

Example:

#include <stdio.h>
#include <ccan/generator/generator.h>

generator_def_static(simple_gen, int)
{
        generator_yield(1);
        generator_yield(3);
        generator_yield(17);
}

int main(int argc, char *argv[])
{
        generator_t(int) gen = simple_gen();
        int *ret;

        while ((ret = generator_next(gen)) != NULL) {
                printf("Generator returned %d\n", *ret);
        }

        return 0;
}

License:

LGPL (v2.1 or any later version)