From 345bff3cc9a36135a00bb963b3fada09d054980d Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 21 May 2021 14:55:04 +0200 Subject: Allow up to 3 vars in the c_withvar (type, v1, v2, v3) statement. --- docs/ccommon_api.md | 4 ++-- include/stc/ccommon.h | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 6ccec29e..3a2ee521 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -13,10 +13,10 @@ constructors are defined. This ensures that resources are released after usage, |:---------------------------------|:--------------------------------------------------| | `c_with (acquire, release)` | Do `acquire`. Defer `release` to end of block | | `c_defer (release...)` | Defer `release` to end of defer block | -| `c_withvar (type, var)` | `c_with (type var = type_init(), type_del(&var))` | +| `c_withvar (type, v1,...v3)` | `c_with (type v1 = type_init(), type_del(&v1))` | | `c_withbuf (buf, type, n)` | Declare, allocate and free memory buffer | -The `acquire`argument must be of the form: `type var = get_resource`. +The `acquire`argument must be of the form: `type var = getResource`. **c_with** and **c_defer** are general macros, whereas **c_withvar** requires that there are methods *type_init()* to create and return an object of type, and *type_del()* that destructs the object. These are available for all STC containers. diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index d29ce699..2bc45a21 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -122,9 +122,13 @@ for (type i=start, _c_inc=step, _c_end=(stop) - (0 < _c_inc) \ ; (i <= _c_end) == (0 < _c_inc); i += _c_inc) -#define c_defer(...) for (int _c_ii = 0; !_c_ii; ++_c_ii, __VA_ARGS__) #define c_with(start, end) for (start, *_c_ii = NULL; !_c_ii; ++_c_ii, end) -#define c_withvar(ctype, var) c_with (ctype var = ctype##_init(), ctype##_del(&var)) +#define c_withvar(...) c_MACRO_OVERLOAD(c_withvar, __VA_ARGS__) +#define c_withvar_2(CX, var) c_with (CX var = CX##_init(), CX##_del(&var)) +#define c_withvar_3(CX, v1, v2) c_with (_c_EXPAND(CX v1 = CX##_init(), v2 = v1), (CX##_del(&v2), CX##_del(&v1))) +#define c_withvar_4(CX, v1, v2, v3) c_with (_c_EXPAND(CX v1 = CX##_init(), v2 = v1, v3 = v1), \ + (CX##_del(&v3), CX##_del(&v2), CX##_del(&v1))) +#define c_defer(...) for (int _c_ii = 0; !_c_ii; ++_c_ii, __VA_ARGS__) #define c_withbuf(b, type, n) c_withbuf_N(b, type, n, 256) #define c_withbuf_N(b, type, n, BYTES) \ -- cgit v1.2.3