Component index
Section index

Component COROUTINE


Search

Type

Object

Summary

Control cooperating co-routines to perform iteration

Description

Co-routines are functions actives together. When one function reach a useful state (find a result), it just freeses and let the other continue with the result. When the second is done it gives control to the first, which resumes at the esact point The COROUTINE class makes this trick very easy to use. See the samples.

bool is_done ()void run ()
bool is_idle ()
bool is_running ()
bool next ()
void restart ()
void stop ()

Examples

sample / COROUTINE object / principles
sample / COROUTINE object performance
sample / object COROUTINE / nested
sample / object COROUTINE / recursion
sample / testing COROUTINE

Prototypes

COROUTINE ()

No parameter passed to the constructor. The destructor takes care of stopping the co-routine if it is running.

Methods

bool is_done ()

Return true if the co-routine has run and ended

bool is_idle ()

Return true if the co-routine has not started yet

This is true right after object creation or after a restart().

bool is_running ()

Return true if the co-routine is alive

The co-routine is stuck in a yield. It has run somehow and it has not end yet.

bool next ()

Let the co-routine run up to the next yield point or to the end

Return true if the co-routine reached a yield point, false if the co-routine ended.

void restart ()

Ask the co-routine to end quickly and be ready to run again

The next call to next() will start the co-routine.

void stop ()

Ask the co-routine to end quickly without yielding anymore.

Further calls to next() won't have any effect and next() will return false.

Functags

void run () Mandatory

You place the co-routine code there

The co-routinel starts when the next() method is invoked. The routine must use the yield() fhelper to signal it has reached a proper state. It usually record suitable information in glocal variable so the parent may process the results.

Fhelpers

int yield ()

Give control back to the parent

The function returns when the parent calls next() again (or restart() or stop()). The function return -1 if it asked to end quickly without yielding anymore.