From 0861391b9b3b8ad37e24b0dba7da192f31e48a6b Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Mon, 20 Sep 2021 10:36:25 +0200 Subject: Renamed macros: breaking changes for Version 2.0! c_forvar => c_autovar c_forscope => c_autoscope c_forauto => c_auto Removed: c_fordefer(x); use c_forscope(0, x) instead. Added: c_exitauto; // continue --- docs/ccommon_api.md | 49 +++++++++++++++++++++---------------------------- docs/clist_api.md | 2 +- docs/cmap_api.md | 6 +++--- docs/cpque_api.md | 2 +- docs/csmap_api.md | 6 +++--- docs/csptr_api.md | 6 +++--- docs/csview_api.md | 2 +- 7 files changed, 33 insertions(+), 40 deletions(-) (limited to 'docs') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index dc274869..75ebae2f 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -2,32 +2,32 @@ The following handy macros are safe to use, i.e. have no side-effects. -### c_forvar, c_forauto, c_forscope, c_fordefer +### c_autoscope, c_autovar, c_auto, c_exitauto 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. -**NB**: These macros are one-time executed **for-loops**. Use ***only*** `continue` in order to break out -of these four `c_for`-blocks. ***Do not*** use `return` or `goto` (or `break`) inside them, as they will -prevent the `end`-statement to be executed when leaving scope. This is not particular to the `c_for*()` +**NB**: These macros are one-time executed **for-loops**. Use ***only*** `c_exitauto` in order to break out +of these `c_auto*`-blocks! ***Do not*** use `return` or `goto` (or `break`) inside them, as they will +prevent the `end`-statement to be executed when leaving scope. This is not particular to the `c_auto*()` macros, as one must always make sure to unwind temporary allocated resources before a `return` in C. -| Usage | Description | -|:---------------------------------------|:--------------------------------------------------| -| `c_forvar (Type var=init, end...)` | Declare `var`. Defer `end` to end of block | -| `c_forauto (Type, var...)` | `c_forvar (Type var=Type_init(), Type_del(&var))` | -| `c_forscope (start, end)` | Execute `start`. Defer `end` to end of block | -| `c_fordefer (end...)` | Defer execution of `end` to end of block | +| 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 | -For multiple variables, use either multiple **c_forvar** in sequence, or declare variable outside -scope and use **c_forscope** or **c_fordefer**. Also, **c_forauto** support up to 3 vars. +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. ```c -c_forvar (cstr s = cstr_lit("Hello"), cstr_del(&s)) +c_autovar (cstr s = cstr_lit("Hello"), cstr_del(&s)) { cstr_append(&s, " world"); printf("%s\n", s.str); } -c_forauto (cstr, s1, s2) +c_auto (cstr, s1, s2) { cstr_append(&s1, "Hello"); cstr_append(&s1, " world"); @@ -38,21 +38,14 @@ c_forauto (cstr, s1, s2) printf("%s %s\n", s1.str, s2.str); } -#define i_tag i32 -#define i_val int32_t -#include -... -cvec_i32 vec; -c_forscope (vec = cvec_i32_init(), cvec_i32_del(&vec)) +MyData data; +c_autoscope (mydata_init(&data), mydata_destroy(&data)) { - cvec_i32_push_back(&vec, 123); - cvec_i32_push_back(&vec, 321); - - c_foreach (i, cvec_i32, vec) printf(" %d", *i.ref); + printf("%s\n", mydata.name.str); } cstr s1 = cstr_lit("Hello"), s2 = cstr_lit("world"); -c_fordefer (cstr_del(&s1), cstr_del(&s2)) +c_autoscope (0, cstr_del(&s1), cstr_del(&s2)) { printf("%s %s\n", s1.str, s2.str); } @@ -70,8 +63,8 @@ cvec_str readFile(const char* name) { cvec_str vec = cvec_str_init(); // returned - c_forvar (FILE* fp = fopen(name, "r"), fclose(fp)) - c_forvar (cstr line = cstr_null, cstr_del(&line)) + c_autovar (FILE* fp = fopen(name, "r"), fclose(fp)) + c_autovar (cstr line = cstr_null, cstr_del(&line)) while (cstr_getline(&line, fp)) cvec_str_emplace_back(&vec, line.str); return vec; @@ -79,7 +72,7 @@ cvec_str readFile(const char* name) int main() { - c_forvar (cvec_str x = readFile(__FILE__), cvec_str_del(&x)) + c_autovar (cvec_str x = readFile(__FILE__), cvec_str_del(&x)) c_foreach (i, cvec_str, x) printf("%s\n", i.ref->str); } diff --git a/docs/clist_api.md b/docs/clist_api.md index 1eb92c12..b4cbced7 100644 --- a/docs/clist_api.md +++ b/docs/clist_api.md @@ -175,7 +175,7 @@ Splice `[30, 40]` from *L2* into *L1* before `3`: #include int main() { - c_forauto (clist_i, L1, L2) + c_auto (clist_i, L1, L2) { c_emplace(clist_i, L1, {1, 2, 3, 4, 5}); c_emplace(clist_i, L2, {10, 20, 30, 40, 50}); diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 4fee41b7..0dd19d2c 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -206,7 +206,7 @@ typedef struct { int x, y, z; } Vec3i; int main() { // Define map with defered destruct - c_forvar (cmap_vi vecs = cmap_vi_init(), cmap_vi_del(&vecs)) + c_autovar (cmap_vi vecs = cmap_vi_init(), cmap_vi_del(&vecs)) { cmap_vi_insert(&vecs, (Vec3i){100, 0, 0}, 1); cmap_vi_insert(&vecs, (Vec3i){ 0, 100, 0}, 2); @@ -239,7 +239,7 @@ typedef struct { int x, y, z; } Vec3i; int main() { - c_forvar (cmap_iv vecs = cmap_iv_init(), cmap_iv_del(&vecs)) + c_autovar (cmap_iv vecs = cmap_iv_init(), cmap_iv_del(&vecs)) { cmap_iv_insert(&vecs, 1, (Vec3i){100, 0, 0}); cmap_iv_insert(&vecs, 2, (Vec3i){ 0, 100, 0}); @@ -367,7 +367,7 @@ static RViking Viking_toR(const Viking* v) int main() { - c_forauto (cmap_vk, vikings) // RAII + c_auto (cmap_vk, vikings) // RAII { // Insert works as before, takes a constructed Viking object cmap_vk_insert(&vikings, (Viking){cstr_from("Einar"), cstr_from("Norway")}, 25); diff --git a/docs/cpque_api.md b/docs/cpque_api.md index 3b3338e8..990cc4b7 100644 --- a/docs/cpque_api.md +++ b/docs/cpque_api.md @@ -68,7 +68,7 @@ int main() stc64_uniform_t dist = stc64_uniform_init(0, N * 10); // Declare heap, with defered del() - c_forauto (cpque_i, heap) + c_auto (cpque_i, heap) { // Push ten million random numbers to priority queue, plus some negative ones. c_forrange (N) diff --git a/docs/csmap_api.md b/docs/csmap_api.md index a9481bf1..1e41593f 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -143,7 +143,7 @@ int main() {100, "Red"}, {110, "Blue"}, }); - c_fordefer (csmap_id_del(&idnames)) + c_autoscope (0, csmap_id_del(&idnames)) { // put replaces existing mapped value: csmap_id_emplace_or_assign(&idnames, 110, "White"); @@ -185,7 +185,7 @@ static int Vec3i_compare(const Vec3i* a, const Vec3i* b) { int main() { - c_forvar (csmap_vi vecs = csmap_vi_init(), csmap_vi_del(&vecs)) + c_autovar (csmap_vi vecs = csmap_vi_init(), csmap_vi_del(&vecs)) { csmap_vi_insert(&vecs, (Vec3i){100, 0, 0}, 1); csmap_vi_insert(&vecs, (Vec3i){ 0, 100, 0}, 2); @@ -218,7 +218,7 @@ typedef struct { int x, y, z; } Vec3i; int main() { - c_forvar (csmap_iv vecs = csmap_iv_init(), csmap_iv_del(&vecs)) + c_autovar (csmap_iv vecs = csmap_iv_init(), csmap_iv_del(&vecs)) { csmap_iv_insert(&vecs, 1, (Vec3i){100, 0, 0}); csmap_iv_insert(&vecs, 2, (Vec3i){ 0, 100, 0}); diff --git a/docs/csptr_api.md b/docs/csptr_api.md index ce70d3c8..67a67d5c 100644 --- a/docs/csptr_api.md +++ b/docs/csptr_api.md @@ -83,8 +83,8 @@ void int_del(int* x) { int main() { - c_forauto (cvec_int, vec) // declare and init vec, call del at scope exit - c_forauto (csset_int, set) // declare and init set, call del at scope exit + c_auto (cvec_int, vec) // declare and init vec, call del at scope exit + c_auto (csset_int, set) // declare and init set, call del at scope exit { cvec_int_push_back(&vec, csptr_int_make(2021)); cvec_int_push_back(&vec, csptr_int_make(2012)); @@ -110,7 +110,7 @@ int main() printf("\nset:"); c_foreach (i, csset_int, set) printf(" %d", *i.ref->get); - c_forvar (csptr_int p = csptr_int_clone(vec.data[0]), csptr_int_del(&p)) { + c_autovar (csptr_int p = csptr_int_clone(vec.data[0]), csptr_int_del(&p)) { printf("\n%d is now owned by %zu objects\n", *p.get, *p.use_count); } diff --git a/docs/csview_api.md b/docs/csview_api.md index 905a6489..6125ef07 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -166,7 +166,7 @@ int main() print_split(c_sv("This has no matching separator"), c_sv("xx")); puts(""); - c_forvar (cvec_str v = string_split(c_sv("Split,this,,string,now,"), c_sv(",")), cvec_str_del(&v)) + c_autovar (cvec_str v = string_split(c_sv("Split,this,,string,now,"), c_sv(",")), cvec_str_del(&v)) c_foreach (i, cvec_str, v) printf("\"%s\"\n", i.ref->str); } -- cgit v1.2.3