From 3aa8a04f4fe38d828006b4713beb390ef526cea7 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Wed, 21 Jun 2023 00:31:59 +0200 Subject: Added cco_switch(x) {}, cco_case(val) {}, and cco_default {} to use inside coroutines. --- include/stc/algo/coroutine.h | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'include') diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index b92507b8..727ce2a8 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -66,6 +66,11 @@ enum { #define cco_initial(co) ((co)->cco_state == 0) #define cco_done(co) ((co)->cco_state == cco_state_done) +/* Always use { } after cco_switch(x), cco_case(val), and cco_default. */ +#define cco_switch(x) for (intmax_t _sw = (intmax_t)(x), _once=1; _once; _once=0) +#define cco_case(val) if (_sw == (intmax_t)(val)) +#define cco_default else + #define cco_routine(co) \ for (int *_state = &(co)->cco_state; *_state != cco_state_done; *_state = cco_state_done) \ _resume: switch (*_state) case 0: // thanks, @liigo! -- cgit v1.2.3