From 20645dbb5ca19015d100f33277c7fcf60bd4bf93 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 4 Oct 2021 19:28:17 +0200 Subject: Added c_autodefer() macro (again). Depressed warning with c_no_compare(). --- docs/ccommon_api.md | 11 ++++++----- examples/splitstr.c | 2 +- include/stc/ccommon.h | 3 ++- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 902b33f5..83f5dc6f 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -2,7 +2,7 @@ The following handy macros are safe to use, i.e. have no side-effects. -### c_autoscope, c_autovar, c_auto, c_exitauto +### c_auto, c_autovar, c_autoscope, c_autodefer General ***defer*** mechanics for resource acquisition. These macros allows to specify the release of the resource where the resource acquisition takes place. Makes it easier to verify that resources are released. @@ -13,10 +13,11 @@ macros, as one must always make sure to unwind temporary allocated resources bef | Usage | Description | |:---------------------------------------|:---------------------------------------------------| -| `c_autoscope (init, end...)` | Execute `init`. Defer `end...` to end of block | -| `c_autovar (Type var=init, end...)` | Declare `var`. Defer `end...` to end of block | | `c_auto (Type, var...)` | `c_autovar (Type var=Type_init(), Type_del(&var))` | -| `c_exitauto;` | Break out of a `c_auto*`-block/scope | +| `c_autovar (Type var=init, end...)` | Declare `var`. Defer `end...` to end of block | +| `c_autoscope (init, end...)` | Execute `init`. Defer `end...` to end of block | +| `c_autodefer (end...)` | Defer `end...` to end of block | +| `c_exitauto;` | Break safely out of a `c_auto*`-block/scope | For multiple variables, use either multiple **c_autovar** in sequence, or declare variable outside scope and use **c_autoscope**. Also, **c_auto** support up to 3 variables. @@ -45,7 +46,7 @@ c_autoscope (mydata_init(&data), mydata_destroy(&data)) } cstr s1 = cstr_lit("Hello"), s2 = cstr_lit("world"); -c_autoscope (0, cstr_del(&s1), cstr_del(&s2)) +c_autodefer (cstr_del(&s1), cstr_del(&s2)) { printf("%s %s\n", s1.str, s2.str); } diff --git a/examples/splitstr.c b/examples/splitstr.c index e4f6ab8a..82914485 100644 --- a/examples/splitstr.c +++ b/examples/splitstr.c @@ -36,7 +36,7 @@ int main() cstr string = cstr_lit("Split,this,,string,now,"); cvec_str vec = string_split(cstr_sv(string), c_sv(",")); - c_autoscope (0, cvec_str_del(&vec), cstr_del(&string)) + c_autodefer (cvec_str_del(&vec), cstr_del(&string)) c_foreach (i, cvec_str, vec) printf("\t\"%s\"\n", i.ref->str); } \ No newline at end of file diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index c9ca7ad0..e3ccfffd 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -94,7 +94,7 @@ #define c_default_compare(x, y) c_less_compare(c_default_less, x, y) #define c_default_less(x, y) (*(x) < *(y)) -#define c_no_compare(x, y) (assert(!"c_no_compare() called"), 0) +#define c_no_compare(x, y) (assert(!"c_no_compare() called"), (x)==(y)) #define c_less_compare(less, x, y) (less(y, x) - less(x, y)) #define c_default_equals(x, y) (*(x) == *(y)) @@ -138,6 +138,7 @@ STC_API uint64_t c_default_hash(const void *key, size_t len); #define c_autoscope(init, ...) for (int _c_ii = (init, 0); !_c_ii; ++_c_ii, __VA_ARGS__) #define c_autovar(declvar, ...) for (declvar, *_c_ii = NULL; !_c_ii; ++_c_ii, __VA_ARGS__) +#define c_autodefer(...) for (int _c_ii = 0; !_c_ii; ++_c_ii, __VA_ARGS__) #define c_exitauto continue #define c_auto(...) c_MACRO_OVERLOAD(c_auto, __VA_ARGS__) -- cgit v1.2.3