Coroutines in C

Simon Tatham has an interesting variation on Duff's device, used to mimic coroutines in C. The guts are hidden inside macros, so the resulting code actually reads quite cleanly. Something to consider next time you find yourself writing a large state machine, wishing you didn't have to turn all your control statements inside out. Here's an example of the result:


int decompressor(void) {
static int c, len;
crBegin;
while (1) {
c = getchar();
if (c == EOF)
break;
if (c == 0xFF) {
len = getchar();
c = getchar();
while (len--)
crReturn(c);
} else {
crReturn(c);
}
}
crReturn(EOF);
crFinish;
}

He points out that the requirement to use static variables can be got around by the standard technique of passing a struct holding all the state. But if you follow this train of thought a bit further, you realize that the technique is even better in C++, where the state is accessible via the "this" pointer:

class Decompressor {
private:
     int c, len;
public:
     int read() {
         crBegin();
         while (1) {
             c = getchar();
             if (c == EOF)
                 break;
             if (c == 0xFF) {
                 len = getchar();
                 c = getchar();
                 while (len--)
                     crReturn(c);
             }
             else {
                 crReturn(c);
             }
         }
         crReturn(EOF);
         crFinish;
     }
}

The C++ version also gives the ability to initialize the state in the constructor, instead of requiring a special initialization call by the client.

Posted on May 26, 2003 03:33 PM
More languages articles

Comments

People all over the world know the abercrombie and fitch,but not everyone really knows how fashion the abercrombie is,hollister is the Legend maker. Everybody wears the hollister clothing would be the abercrombie mensand the abercrombie womens, if you want know you can search the Ruehl No.925 or abercrombie outlet in the www.google.com .

Posted by: fitch at November 14, 2009 01:42 AM

Louis Vuitton, commonly referred to as Louis Vuitton Bags and Louis Vuitton Shoes, or sometimes shortened to Louis Vuitton Speedy 30 has become one of the most Louis Vuitton Tivoli PM Agendas luxurybrands Louis Vuitton Tivoli PM.

Posted by: 1 at November 17, 2009 02:17 AM

I love

99.html">ugg mayfaire boots

! It fits perfectly

and arrived in condition I expected. Thank you!

Posted by: aewrtwet at November 19, 2009 08:29 PM

Laptop Battery Laptop Battery Laptop Batteries
Laptop Batteries discount laptop battery
discount laptop battery
notebook battery notebook battery
computer battery computer battery
replacement laptop battery replacement laptop battery
notebook batteries notebook batteries

Posted by: Laptop Battery at November 24, 2009 09:47 PM

Ugg Classic Cardy

Boots

Ugg Classic Tall

Boots

Ugg Classic Short

Boots

Posted by: topuggshoes at November 26, 2009 03:28 AM

Ugg Classic Cardy

Boots

Ugg Classic Tall

Boots

Ugg Classic Short

Boots

Posted by: topuggshoes at November 26, 2009 03:29 AM

Just wanted to say great job with the blog, today is my first visit here and I’ve enjoyed reading your posts so far
ugg bailey button
Wow, my ugg classic mini will not be coming off now! I’ve had them on for 12hrs strait and I do not want to take them off. Thanks for everything, well worth the wait.

Posted by: ugg bailey button boots at November 30, 2009 06:08 AM

Spring is near,every girl wants to be the bride in the special season.They are eager to put on beautiful Wedding Dresses or the Bridal gowns.During the day the Wedding gowns is the good choose,and the night,if you want radiant,you need the Evening gowns.
About the bridesmaid,they have to wear Bridesmaid Dresses in order to avoid grab limelight with the bride.And the Flower Girl has the Flower Girl Dresses,too.In the wedding,the Cocktail Dresses and the Evening Dresses is necessary,too!And remember,the Wedding Dress of the Bridal Dress must be the most glaring!

Posted by: weddingdressclub at December 1, 2009 01:20 AM
Post a comment









Remember info?




Prove you're human. Type "human":