summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--docs/ccommon_api.md32
-rw-r--r--docs/cmap_api.md4
-rw-r--r--docs/cpque_api.md2
-rw-r--r--docs/csmap_api.md4
-rw-r--r--docs/csview_api.md4
-rw-r--r--examples/advanced.c2
-rw-r--r--examples/birthday.c2
-rw-r--r--examples/bits.c6
-rw-r--r--examples/cpque.c6
-rw-r--r--examples/csmap_erase.c6
-rw-r--r--examples/csmap_find.c4
-rw-r--r--examples/csmap_insert.c12
-rw-r--r--examples/demos.c12
-rw-r--r--examples/ex_gauss1.c6
-rw-r--r--examples/ex_gauss2.c4
-rw-r--r--examples/inits.c10
-rw-r--r--examples/list.c2
-rw-r--r--examples/list_erase.c2
-rw-r--r--examples/list_splice.c7
-rw-r--r--examples/mapmap.c2
-rw-r--r--examples/mmap.c2
-rw-r--r--examples/multimap.c2
-rw-r--r--examples/phonebook.c4
-rw-r--r--examples/prime.c2
-rw-r--r--examples/priority.c2
-rw-r--r--examples/queue.c2
-rw-r--r--examples/replace.c2
-rw-r--r--examples/stack.c2
-rw-r--r--examples/stc_astar.c6
-rw-r--r--examples/svmap.c2
-rw-r--r--examples/words.c4
-rw-r--r--include/stc/carray.h2
-rw-r--r--include/stc/cbits.h2
-rw-r--r--include/stc/ccommon.h19
-rw-r--r--include/stc/clist.h2
-rw-r--r--include/stc/cmap.h2
-rw-r--r--include/stc/cpque.h2
-rw-r--r--include/stc/cqueue.h2
-rw-r--r--include/stc/csmap.h2
-rw-r--r--include/stc/cstack.h2
-rw-r--r--include/stc/cstr.h2
41 files changed, 95 insertions, 101 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md
index 7e329cfe..08b92396 100644
--- a/docs/ccommon_api.md
+++ b/docs/ccommon_api.md
@@ -2,25 +2,23 @@
The following handy macros are safe to use, i.e. have no side-effects.
-### c_defer, c_with, c_withvar, c_withbuf, c_exitwith/c_exitdefer
-General ***defer*** mechanics. These macros allows to specify a resource release explicitly or implicitly where the
-resource acquisition is made. This ensures that resources are released after usage, and avoids memory leaks.
-**c_with / c_withvar** are inspired by Python's *with* statement, and **c_defer** from Go language.
+### c_fordefer, c_forbuffer
+General ***defer*** mechanics for resource acquisition. These macros allows to specify the release of the
+resource where the resource acquisition is made. This ensures that resources are released after usage.
-**NB**: ***Only*** use `c_exitwith`/`c_exitdefer` to break out of `c_with*`/`c_defer`-blocks.
-***Never*** use `return`, `goto` or `break` inside such a block.
+**NB**: These macros are one-time **for**-loops. ***Do only*** use `continue` in order to break out of a
+`c_fordefer` / `c_forbuffer`-block. ***Do not*** use `return`, `goto` or `break`, as it will prevent the
+`release`-statement to be executed at the end.
| Usage | Description |
|:---------------------------------|:--------------------------------------------------|
-| `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, v1,...v3)` | `c_with (type v1 = type_init(), type_del(&v1))` |
-| `c_withbuf (buf, type, n)` | Declare, allocate and free memory buffer |
+| `c_fordefer (acquire, release)` | Do `acquire`. Defer `release` to end of block |
+| `c_fordefer (release)` | Defer execution of `release` to end of block |
+| `c_forbuffer (buf, type, n)` | Declare, allocate and free memory buffer |
-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()*
-which destructs the object. These are available for all STC containers.
+The `acquire`argument must be of the form: `type var = getResource`. Note that the `release` argument can be
+a list of statements enclosed in parathesises. **c_forbuffer** uses stack memory if buffer is <= to 256 bytes,
+othewise it uses more expensive heap memory.
```c
// Example: Load each line of a text file into a vector of strings
@@ -35,8 +33,8 @@ cvec_str readFile(const char* name)
// receiver should check errno variable
cvec_str vec = cvec_str_init();
- c_with (FILE* fp = fopen(name, "r"), fclose(fp)) {
- c_with (cstr line = cstr_null, cstr_del(&line))
+ c_fordefer (FILE* fp = fopen(name, "r"), fclose(fp)) {
+ c_fordefer (cstr line = cstr_null, cstr_del(&line))
while (cstr_getline(&line, fp))
cvec_str_emplace_back(&vec, line.str);
}
@@ -45,7 +43,7 @@ cvec_str readFile(const char* name)
int main()
{
- c_with (cvec_str x = readFile(__FILE__), cvec_str_del(&x))
+ c_fordefer (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/cmap_api.md b/docs/cmap_api.md
index a9481903..6bd998ba 100644
--- a/docs/cmap_api.md
+++ b/docs/cmap_api.md
@@ -208,7 +208,7 @@ using_cmap(vi, Vec3i, int, c_memcmp_equals, // bitwise equals
int main()
{
// Define map with defered destruct
- c_with (cmap_vi vecs = cmap_vi_init(), cmap_vi_del(&vecs))
+ c_fordefer (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 @@ using_cmap(iv, int, Vec3i);
int main()
{
- c_with (cmap_iv vecs = cmap_iv_init(), cmap_iv_del(&vecs))
+ c_fordefer (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});
diff --git a/docs/cpque_api.md b/docs/cpque_api.md
index eaa476c1..503e322e 100644
--- a/docs/cpque_api.md
+++ b/docs/cpque_api.md
@@ -69,7 +69,7 @@ int main()
stc64_uniform_t dist = stc64_uniform_init(0, N * 10);
// Declare heap, with defered del()
- c_with (cpque_i heap = cpque_i_init(), cpque_i_del(&heap))
+ c_fordefer (cpque_i heap = cpque_i_init(), cpque_i_del(&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 52c2829b..257e439c 100644
--- a/docs/csmap_api.md
+++ b/docs/csmap_api.md
@@ -192,7 +192,7 @@ using_csmap(vi, Vec3i, int, Vec3i_compare);
int main()
{
- c_with (csmap_vi vecs = csmap_vi_init(), csmap_vi_del(&vecs))
+ c_fordefer (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);
@@ -223,7 +223,7 @@ using_csmap(iv, int, Vec3i);
int main()
{
- c_with (csmap_iv vecs = csmap_iv_init(), csmap_iv_del(&vecs))
+ c_fordefer (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/csview_api.md b/docs/csview_api.md
index 16d59eae..26857938 100644
--- a/docs/csview_api.md
+++ b/docs/csview_api.md
@@ -128,7 +128,7 @@ int main()
csview text = c_lit("The length of this literal is evaluated at compile time and stored in csview text.");
printf("%s\nLength: %zu\n\n", text.str, text.size);
- c_withvar (cmap_si, map) // defines map and defers destruction.
+ c_fordefer (cmap_si map = csmap_si_init(), csmap_si_del(&map))
{
cmap_si_emplace(&map, c_lit("hello"), 100);
cmap_si_emplace(&map, c_lit("world"), 200);
@@ -187,7 +187,7 @@ int main()
print_split(c_lit("This has no matching separator"), c_lit("xx"));
puts("");
- c_with (cvec_str v = string_split(c_lit("Split,this,,string,now,"), c_lit(",")), cvec_str_del(&v))
+ c_fordefer (cvec_str v = string_split(c_lit("Split,this,,string,now,"), c_lit(",")), cvec_str_del(&v))
c_foreach (i, cvec_str, v)
printf("\"%s\"\n", i.ref->str);
}
diff --git a/examples/advanced.c b/examples/advanced.c
index 6d5486ae..1d8a8cf2 100644
--- a/examples/advanced.c
+++ b/examples/advanced.c
@@ -41,7 +41,7 @@ using_cmap_keydef(vk, Viking, int, vikingraw_equals, vikingraw_hash,
int main()
{
- c_with (cmap_vk vikings = cmap_vk_init(), cmap_vk_del(&vikings)) {
+ c_fordefer (cmap_vk vikings = cmap_vk_init(), cmap_vk_del(&vikings)) {
c_emplace(cmap_vk, vikings, {
{ {"Einar", "Norway"}, 20},
{ {"Olaf", "Denmark"}, 24},
diff --git a/examples/birthday.c b/examples/birthday.c
index 18e9cd77..80802572 100644
--- a/examples/birthday.c
+++ b/examples/birthday.c
@@ -35,7 +35,7 @@ void test_distribution(void)
stc64_t rng = stc64_init(seed);
const size_t N = 1ull << BITS ;
- c_with (cmap_x map = cmap_x_init(), cmap_x_del(&map)) {
+ c_fordefer (cmap_x map = cmap_x_init(), cmap_x_del(&map)) {
c_forrange (N) {
uint64_t k = stc64_rand(&rng);
cmap_x_emplace(&map, k & 0xf, 0).ref->second += 1;
diff --git a/examples/bits.c b/examples/bits.c
index 65be30ac..e478321c 100644
--- a/examples/bits.c
+++ b/examples/bits.c
@@ -3,7 +3,7 @@
int main()
{
- c_with (cbits set = cbits_with_size(23, true), cbits_del(&set)) {
+ c_fordefer (cbits set = cbits_with_size(23, true), cbits_del(&set)) {
printf("count %zu, %zu\n", cbits_count(set), set.size);
cbits s1 = cbits_from_str("1110100110111");
char buf[256];
@@ -12,7 +12,7 @@ int main()
cbits_reset(&set, 9);
cbits_resize(&set, 43, false);
- c_withbuf (str, char, set.size + 1)
+ c_forbuffer (str, char, set.size + 1)
printf(" str: %s\n", cbits_to_str(set, str, 0, -1));
printf("%4zu: ", set.size);
@@ -35,7 +35,7 @@ int main()
printf("%d", cbits_test(set, i));
puts("");
- c_with (cbits s2 = cbits_clone(set), cbits_del(&s2)) {
+ c_fordefer (cbits s2 = cbits_clone(set), cbits_del(&s2)) {
cbits_flip_all(&s2);
cbits_set(&s2, 16);
cbits_set(&s2, 17);
diff --git a/examples/cpque.c b/examples/cpque.c
index d43ddef8..937b7ac9 100644
--- a/examples/cpque.c
+++ b/examples/cpque.c
@@ -31,9 +31,9 @@ int main()
{
const int data[] = {1,8,5,6,3,4,0,9,7,2};
- c_withvar (cpque_imax, q) // init() and defered del()
- c_withvar (cpque_imin, q2)
- c_withvar (cpque_imix, q3)
+ c_fordefer (cpque_imax q = cpque_imax_init(), cpque_imax_del(&q)) // init() and defered del()
+ c_fordefer (cpque_imin q2 = cpque_imin_init(), cpque_imin_del(&q2))
+ c_fordefer (cpque_imix q3 = cpque_imix_init(), cpque_imix_del(&q3))
{
c_forrange (n, c_arraylen(data))
cpque_imax_push(&q, n);
diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c
index 1276bd7b..58a90d4b 100644
--- a/examples/csmap_erase.c
+++ b/examples/csmap_erase.c
@@ -15,7 +15,7 @@ void printmap(csmap_my map)
int main()
{
- c_with (csmap_my m1 = csmap_my_init(), csmap_my_del(&m1))
+ c_fordefer (csmap_my m1 = csmap_my_init(), csmap_my_del(&m1))
{
// Fill in some data to test with, one at a time
csmap_my_emplace(&m1, 1, "A");
@@ -32,7 +32,7 @@ int main()
printmap(m1);
}
- c_withvar (csmap_my, m2)
+ c_fordefer (csmap_my m2 = csmap_my_init(), csmap_my_del(&m2))
{
// Fill in some data to test with, one at a time, using emplace
c_emplace(csmap_my, m2, {
@@ -54,7 +54,7 @@ int main()
printmap(m2);
}
- c_withvar (csmap_my, m3)
+ c_fordefer (csmap_my m3 = csmap_my_init(), csmap_my_del(&m3))
{
// Fill in some data to test with, one at a time, using emplace
csmap_my_emplace(&m3, 1, "red");
diff --git a/examples/csmap_find.c b/examples/csmap_find.c
index d32cd1b5..4fcedb43 100644
--- a/examples/csmap_find.c
+++ b/examples/csmap_find.c
@@ -38,8 +38,8 @@ void findit(csmap_istr c, csmap_istr_key_t val) {
int main()
{
- c_withvar (csmap_istr, m1)
- c_withvar (cvec_istr, v) {
+ c_fordefer (csmap_istr m1 = csmap_istr_init(), csmap_istr_del(&m1))
+ c_fordefer (cvec_istr v = cvec_istr_init(), cvec_istr_del(&v)) {
c_emplace(csmap_istr, m1, { { 40, "Zr" }, { 45, "Rh" } });
puts("The starting map m1 is (key, value):");
print_collection_csmap_istr(m1);
diff --git a/examples/csmap_insert.c b/examples/csmap_insert.c
index e207a2a6..0f32e586 100644
--- a/examples/csmap_insert.c
+++ b/examples/csmap_insert.c
@@ -25,7 +25,7 @@ void print_istr(csmap_istr map) {
int main()
{
// insert single values
- c_with (csmap_ii m1 = csmap_ii_init(), csmap_ii_del(&m1)) {
+ c_fordefer (csmap_ii m1 = csmap_ii_init(), csmap_ii_del(&m1)) {
csmap_ii_insert(&m1, 1, 10);
csmap_ii_insert(&m1, 2, 20);
@@ -52,9 +52,9 @@ int main()
}
// The templatized version inserting a jumbled range
- // c_withvar(type, v) is shorthand for: c_with (type v = type_init(), type_del(&v))
- c_withvar (csmap_ii, m2)
- c_withvar (cvec_ii, v) {
+ // c_fordefer_var(type, v) is shorthand for: c_fordefer (type v = type_init(), type_del(&v))
+ c_fordefer (csmap_ii m2 = csmap_ii_init(), csmap_ii_del(&m2))
+ c_fordefer (cvec_ii v = cvec_ii_init(), cvec_ii_del(&v)) {
cvec_ii_push_back(&v, (cvec_ii_value_t){43, 294});
cvec_ii_push_back(&v, (cvec_ii_value_t){41, 262});
cvec_ii_push_back(&v, (cvec_ii_value_t){45, 330});
@@ -73,7 +73,7 @@ int main()
}
// The templatized versions move-constructing elements
- c_withvar (csmap_istr, m3) {
+ c_fordefer (csmap_istr m3 = csmap_istr_init(), csmap_istr_del(&m3)) {
csmap_istr_value_t ip1 = {475, cstr_lit("blue")}, ip2 = {510, cstr_lit("green")};
// single element
@@ -88,7 +88,7 @@ int main()
puts("");
}
- c_withvar (csmap_ii, m4) {
+ c_fordefer (csmap_ii m4 = csmap_ii_init(), csmap_ii_del(&m4)) {
// Insert the elements from an initializer_list
c_emplace(csmap_ii, m4, { { 4, 44 }, { 2, 22 }, { 3, 33 }, { 1, 11 }, { 5, 55 } });
puts("After initializer_list insertion, m4 contains:");
diff --git a/examples/demos.c b/examples/demos.c
index 91daf1b1..a9fad4ab 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -9,7 +9,7 @@
void stringdemo1()
{
printf("\nSTRINGDEMO1\n");
- c_with (cstr cs = cstr_from("one-nine-three-seven-five"), cstr_del(&cs))
+ c_fordefer (cstr cs = cstr_from("one-nine-three-seven-five"), cstr_del(&cs))
{
printf("%s.\n", cs.str);
@@ -40,7 +40,7 @@ using_cvec(ix, int64_t); // ix is just an example tag name.
void vectordemo1()
{
printf("\nVECTORDEMO1\n");
- c_with (cvec_ix bignums = cvec_ix_with_capacity(100), cvec_ix_del(&bignums))
+ c_fordefer (cvec_ix bignums = cvec_ix_with_capacity(100), cvec_ix_del(&bignums))
{
cvec_ix_reserve(&bignums, 100);
for (size_t i = 10; i <= 100; i += 10)
@@ -64,7 +64,7 @@ using_cvec_str();
void vectordemo2()
{
printf("\nVECTORDEMO2\n");
- c_with (cvec_str names = cvec_str_init(), cvec_str_del(&names)) {
+ c_fordefer (cvec_str names = cvec_str_init(), cvec_str_del(&names)) {
cvec_str_emplace_back(&names, "Mary");
cvec_str_emplace_back(&names, "Joe");
cvec_str_emplace_back(&names, "Chris");
@@ -82,8 +82,8 @@ using_clist(ix, int);
void listdemo1()
{
printf("\nLISTDEMO1\n");
- clist_ix nums = clist_ix_init(), nums2 = clist_ix_init();
- c_defer (clist_ix_del(&nums), clist_ix_del(&nums2))
+ c_fordefer (clist_ix nums = clist_ix_init(), clist_ix_del(&nums))
+ c_fordefer (clist_ix nums2 = clist_ix_init(), clist_ix_del(&nums2))
{
for (int i = 0; i < 10; ++i)
clist_ix_push_back(&nums, i);
@@ -142,7 +142,7 @@ using_cmap_strkey(si, int); // Shorthand macro for the general using_cmap expans
void mapdemo2()
{
printf("\nMAPDEMO2\n");
- c_with (cmap_si nums = cmap_si_init(), cmap_si_del(&nums))
+ c_fordefer (cmap_si nums = cmap_si_init(), cmap_si_del(&nums))
{
cmap_si_emplace_or_assign(&nums, "Hello", 64);
cmap_si_emplace_or_assign(&nums, "Groovy", 121);
diff --git a/examples/ex_gauss1.c b/examples/ex_gauss1.c
index 9b9285e6..d44c2b4e 100644
--- a/examples/ex_gauss1.c
+++ b/examples/ex_gauss1.c
@@ -30,8 +30,8 @@ int main()
stc64_normalf_t dist = stc64_normalf_init(Mean, StdDev);
// Create and init histogram vec and map with defered destructors:
- c_withvar (cvec_e, vhist)
- c_withvar (cmap_i, mhist)
+ c_fordefer (cvec_e vhist = cvec_e_init(), cvec_e_del(&vhist))
+ c_fordefer (cmap_i mhist = cmap_i_init(), cmap_i_del(&mhist))
{
c_forrange (N) {
int index = (int) round( stc64_normalf(&rng, &dist) );
@@ -45,7 +45,7 @@ int main()
cvec_e_sort(&vhist);
// Print the gaussian bar chart
- c_with (cstr bar = cstr_null, cstr_del(&bar))
+ c_fordefer (cstr bar = cstr_null, cstr_del(&bar))
c_foreach (i, cvec_e, vhist) {
size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N);
if (n > 0) {
diff --git a/examples/ex_gauss2.c b/examples/ex_gauss2.c
index 9ff42ebe..23d8dd7a 100644
--- a/examples/ex_gauss2.c
+++ b/examples/ex_gauss2.c
@@ -21,7 +21,8 @@ int main()
stc64_normalf_t dist = stc64_normalf_init(Mean, StdDev);
// Create and init histogram map with defered destruct
- c_withvar (csmap_i, mhist)
+ c_fordefer (csmap_i mhist = csmap_i_init(), csmap_i_del(&mhist))
+ c_fordefer (cstr bar = cstr_init(), cstr_del(&bar))
{
c_forrange (N) {
int index = (int) round( stc64_normalf(&rng, &dist) );
@@ -29,7 +30,6 @@ int main()
}
// Print the gaussian bar chart
- c_withvar (cstr, bar)
c_foreach (i, csmap_i, mhist) {
size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N);
if (n > 0) {
diff --git a/examples/inits.c b/examples/inits.c
index c1d64801..91f5c3c1 100644
--- a/examples/inits.c
+++ b/examples/inits.c
@@ -23,7 +23,7 @@ int main(void)
{
// CVEC FLOAT / PRIORITY QUEUE
- c_withvar (cvec_f, floats) {
+ c_fordefer (cvec_f floats = cvec_f_init(), cvec_f_del(&floats)) {
c_emplace(cvec_f, floats, {4.0f, 2.0f, 5.0f, 3.0f, 1.0f});
c_foreach (i, cvec_f, floats) printf("%.1f ", *i.ref);
@@ -45,7 +45,7 @@ int main(void)
// CMAP ID
int year = 2020;
- c_withvar (cmap_id, idnames) {
+ c_fordefer (cmap_id idnames = cmap_id_init(), cmap_id_del(&idnames)) {
cmap_id_emplace(&idnames, 100, "Hello");
cmap_id_insert(&idnames, 110, cstr_from("World"));
cmap_id_insert(&idnames, 120, cstr_from_fmt("Howdy, -%d-", year));
@@ -57,7 +57,7 @@ int main(void)
// CMAP CNT
- c_withvar (cmap_cnt, countries) {
+ c_fordefer (cmap_cnt countries = cmap_cnt_init(), cmap_cnt_del(&countries)) {
c_emplace(cmap_cnt, countries, {
{"Norway", 100},
{"Denmark", 50},
@@ -80,7 +80,7 @@ int main(void)
// CVEC PAIR
- c_withvar (cvec_ip, pairs1) {
+ c_fordefer (cvec_ip pairs1 = cvec_ip_init(), cvec_ip_del(&pairs1)) {
c_emplace (cvec_ip, pairs1, {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
cvec_ip_sort(&pairs1);
@@ -91,7 +91,7 @@ int main(void)
// CLIST PAIR
- c_withvar (clist_ip, pairs2) {
+ c_fordefer (clist_ip pairs2 = clist_ip_init(), clist_ip_del(&pairs2)) {
c_emplace(clist_ip, pairs2, {{5, 6}, {3, 4}, {1, 2}, {7, 8}});
clist_ip_sort(&pairs2);
diff --git a/examples/list.c b/examples/list.c
index 85ebcf02..e4b027a9 100644
--- a/examples/list.c
+++ b/examples/list.c
@@ -8,7 +8,7 @@ int main() {
int k;
const int n = 2000000;
- c_withvar (clist_fx, list) {
+ c_fordefer (clist_fx list = clist_fx_init(), clist_fx_del(&list)) {
stc64_t rng = stc64_init(1234);
stc64_uniformf_t dist = stc64_uniformf_init(100.0f, n);
int m = 0;
diff --git a/examples/list_erase.c b/examples/list_erase.c
index 3526b5eb..a9d2b9d3 100644
--- a/examples/list_erase.c
+++ b/examples/list_erase.c
@@ -6,7 +6,7 @@ using_clist(i, int);
int main ()
{
- c_with (clist_i L = clist_i_init(), clist_i_del(&L))
+ c_fordefer (clist_i L = clist_i_init(), clist_i_del(&L))
{
c_emplace(clist_i, L, {10, 20, 30, 40, 50});
// 10 20 30 40 50
diff --git a/examples/list_splice.c b/examples/list_splice.c
index dd5d8cc4..f80ac860 100644
--- a/examples/list_splice.c
+++ b/examples/list_splice.c
@@ -14,10 +14,11 @@ void print_ilist(const char* s, clist_i list)
int main ()
{
- c_var (clist_i, list1, {1, 2, 3, 4, 5});
- c_var (clist_i, list2, {10, 20, 30, 40, 50});
- c_defer (clist_i_del(&list1), clist_i_del(&list2))
+ c_fordefer (clist_i list1 = clist_i_init(), clist_i_del(&list1))
+ c_fordefer (clist_i list2 = clist_i_init(), clist_i_del(&list2))
{
+ c_emplace(clist_i, list1, {1, 2, 3, 4, 5});
+ c_emplace(clist_i, list2, {10, 20, 30, 40, 50});
clist_i_iter_t it = clist_i_fwd(clist_i_begin(&list1), 2);
it = clist_i_splice(&list1, it, &list2);
diff --git a/examples/mapmap.c b/examples/mapmap.c
index b4bb6beb..21b2cac9 100644
--- a/examples/mapmap.c
+++ b/examples/mapmap.c
@@ -9,7 +9,7 @@ using_cmap_str();
using_cmap_strkey(cfg, cmap_str, cmap_str_del, c_no_clone);
int main(void) {
- c_withvar (cmap_cfg, cfg) {
+ c_fordefer (cmap_cfg cfg = cmap_cfg_init(), cmap_cfg_del(&cfg)) {
cmap_str init = cmap_str_init();
cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("user"), init).ref->second, "name", "Joe");
cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("user"), init).ref->second, "groups", "proj1,proj3");
diff --git a/examples/mmap.c b/examples/mmap.c
index c925db39..58511c37 100644
--- a/examples/mmap.c
+++ b/examples/mmap.c
@@ -26,7 +26,7 @@ void insert(csmap_m* mmap, int key, const char* str)
int main()
{
- c_with (csmap_m mmap = csmap_m_init(), csmap_m_del(&mmap))
+ c_fordefer (csmap_m mmap = csmap_m_init(), csmap_m_del(&mmap))
{
// list-initialize
struct {int i; const char* s;} vals[] = {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}};
diff --git a/examples/multimap.c b/examples/multimap.c
index 8d56119f..4b192e29 100644
--- a/examples/multimap.c
+++ b/examples/multimap.c
@@ -55,7 +55,7 @@ using_csmap_strkey(OL, clist_OL, clist_OL_del, c_no_clone);
int main()
{
// Define the multimap with destructor defered to when block is completed.
- c_with (csmap_OL multimap = csmap_OL_init(), csmap_OL_del(&multimap))
+ c_fordefer (csmap_OL multimap = csmap_OL_init(), csmap_OL_del(&multimap))
{
clist_OL empty = clist_OL_init();
diff --git a/examples/phonebook.c b/examples/phonebook.c
index 40a4b0fd..e755923f 100644
--- a/examples/phonebook.c
+++ b/examples/phonebook.c
@@ -39,14 +39,14 @@ int main(int argc, char **argv)
{
c_static_assert(3 == 3, "hello");
- c_withvar (cset_str, names) {
+ c_fordefer (cset_str names = cset_str_init(), cset_str_del(&names)) {
c_emplace (cset_str, names, {"Hello", "Cool", "True"});
c_foreach (i, cset_str, names) printf("%s ", i.ref->str);
puts("");
}
bool erased;
- c_withvar (cmap_str, phone_book) {
+ c_fordefer (cmap_str phone_book = cmap_str_init(), cmap_str_del(&phone_book)) {
c_emplace (cmap_str, phone_book, {
{"Lilia Friedman", "(892) 670-4739"},
{"Tariq Beltran", "(489) 600-7575"},
diff --git a/examples/prime.c b/examples/prime.c
index 48fa5bbe..67f92c7b 100644
--- a/examples/prime.c
+++ b/examples/prime.c
@@ -27,7 +27,7 @@ int main(void)
printf("computing prime numbers up to %zu\n", n);
clock_t t1 = clock();
- c_with (cbits primes = sieveOfEratosthenes(n + 1), cbits_del(&primes)) {
+ c_fordefer (cbits primes = sieveOfEratosthenes(n + 1), cbits_del(&primes)) {
size_t np = cbits_count(primes);
clock_t t2 = clock();
diff --git a/examples/priority.c b/examples/priority.c
index d0234dbc..e0865054 100644
--- a/examples/priority.c
+++ b/examples/priority.c
@@ -13,7 +13,7 @@ int main() {
size_t N = 10000000;
stc64_t rng = stc64_init(time(NULL));
stc64_uniform_t dist = stc64_uniform_init(0, N * 10);
- c_withvar (cpque_i, heap)
+ c_fordefer (cpque_i heap = cpque_i_init(), cpque_i_del(&heap))
{
// Push ten million random numbers to priority queue
c_forrange (N)
diff --git a/examples/queue.c b/examples/queue.c
index e3a74f33..d2687507 100644
--- a/examples/queue.c
+++ b/examples/queue.c
@@ -17,7 +17,7 @@ int main() {
stc64_t rng = stc64_init(1234);
dist = stc64_uniform_init(0, n);
- c_withvar (cqueue_i, queue)
+ c_fordefer (cqueue_i queue = cqueue_i_init(), cqueue_i_del(&queue))
{
// Push ten million random numbers onto the queue.
c_forrange (n)
diff --git a/examples/replace.c b/examples/replace.c
index b537ef88..69928917 100644
--- a/examples/replace.c
+++ b/examples/replace.c
@@ -13,7 +13,7 @@ int main ()
// Ustring positions: 0123456789*123456789*12345
cstr s = cstr_from(base); // "this is a test string."
cstr m = cstr_clone(s);
- c_defer (cstr_del(&s), cstr_del(&m)) {
+ c_fordefer ((cstr_del(&s), cstr_del(&m))) {
cstr_append(&m, m.str);
cstr_append(&m, m.str);
printf("%s\n", m.str);
diff --git a/examples/stack.c b/examples/stack.c
index 0815b580..83c8cc08 100644
--- a/examples/stack.c
+++ b/examples/stack.c
@@ -11,7 +11,7 @@ using_cstack(c, cvec_c);
int main() {
cstack_i stack = cstack_i_init();
cstack_c chars = cstack_c_init();
- c_defer (cstack_i_del(&stack), cstack_c_del(&chars))
+ c_fordefer ((cstack_i_del(&stack), cstack_c_del(&chars)))
{
c_forrange (i, int, 101)
cstack_i_push(&stack, i*i);
diff --git a/examples/stc_astar.c b/examples/stc_astar.c
index 11e8ca2b..ecca939f 100644
--- a/examples/stc_astar.c
+++ b/examples/stc_astar.c
@@ -83,9 +83,9 @@ astar(cstr maze, int width)
cpque_pnt frontier = cpque_pnt_init();
csmap_step came_from = csmap_step_init();
csmap_cost cost_so_far = csmap_cost_init();
- c_defer (cpque_pnt_del(&frontier),
- csmap_step_del(&came_from),
- csmap_cost_del(&cost_so_far))
+ c_fordefer ((cpque_pnt_del(&frontier),
+ csmap_step_del(&came_from),
+ csmap_cost_del(&cost_so_far)))
{
cpque_pnt_push(&frontier, start);
csmap_cost_insert(&cost_so_far, start, 0);
diff --git a/examples/svmap.c b/examples/svmap.c
index c45ff1ae..3087706e 100644
--- a/examples/svmap.c
+++ b/examples/svmap.c
@@ -9,7 +9,7 @@ int main() {
csview fox = c_lit("The quick brown fox jumps over the lazy dog.");
printf("\"%s\", length=%zu\n", fox.str, fox.size);
- c_withvar (cmap_si, frequencies)
+ c_fordefer (cmap_si frequencies = cmap_si_init(), cmap_si_del(&frequencies))
{
// Non-emplace: cstr element API
cmap_si_insert(&frequencies, cstr_lit("thousand"), 1000);
diff --git a/examples/words.c b/examples/words.c
index 6859f104..afc1f989 100644
--- a/examples/words.c
+++ b/examples/words.c
@@ -15,7 +15,7 @@ int main1()
"this", "sentence", "is", "not", "a", "sentence",
"this", "sentence", "is", "a", "hoax"
});
- c_defer (clist_str_del(&lwords))
+ c_fordefer (clist_str_del(&lwords))
{
clist_str_push_back(&lwords, cstr_from_fmt("%.15f", sqrt(2)));
c_foreach (w, clist_str, lwords)
@@ -26,7 +26,7 @@ int main1()
"this", "sentence", "is", "not", "a", "sentence",
"this", "sentence", "is", "a", "hoax"
});
- c_defer (cvec_str_del(&words))
+ c_fordefer (cvec_str_del(&words))
{
cmap_si word_map = cmap_si_init();
c_foreach (w, cvec_str, words)
diff --git a/include/stc/carray.h b/include/stc/carray.h
index e3859b8a..fa794238 100644
--- a/include/stc/carray.h
+++ b/include/stc/carray.h
@@ -33,7 +33,7 @@ using_carray2(i, int);
int main() {
int w = 7, h = 5;
- c_with (carray2i image = carray2i_init(w, h), carray2i_del(&image))
+ c_fordefer (carray2i image = carray2i_init(w, h), carray2i_del(&image))
{
int *dat = carray2i_data(&image);
for (int i = 0; i < carray2i_size(image); ++i)
diff --git a/include/stc/cbits.h b/include/stc/cbits.h
index fc2d525b..803fe2d5 100644
--- a/include/stc/cbits.h
+++ b/include/stc/cbits.h
@@ -30,7 +30,7 @@ Similar to boost::dynamic_bitset / std::bitset
#include "cbits.h"
int main() {
- c_with (cbits bset = cbits_with_size(23, true), cbits_del(&bset))
+ c_fordefer (cbits bset = cbits_with_size(23, true), cbits_del(&bset))
{
cbits_reset(&bset, 9);
cbits_resize(&bset, 43, false);
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index bf9d6aed..62b8a172 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -53,12 +53,13 @@
/* Macro overloading feature support: https://rextester.com/ONP80107 */
#define _c_CAT( A, B ) A ## B
+#define _c_SELECT(NAME, NUM) _c_CAT( NAME ## _, NUM)
#define _c_EXPAND(...) __VA_ARGS__
#define _c_ARG_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, N,...) N
#define _c_RSEQ_N 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
#define _c_APPLY_ARG_N(ARGS) _c_EXPAND(_c_ARG_N ARGS)
#define _c_ARG_COUNT(...) _c_EXPAND(_c_APPLY_ARG_N((__VA_ARGS__, _c_RSEQ_N)))
-#define _c_SELECT(NAME, NUM) _c_CAT( NAME ## _, NUM)
+#define _c_LABEL(id) _c_SELECT(_c_label_##id, __LINE__)
#define c_MACRO_OVERLOAD(NAME, ...) _c_SELECT(NAME, _c_ARG_COUNT(__VA_ARGS__))(__VA_ARGS__)
#define c_static_assert(cond, ...) typedef char _static_assert_[(cond) ? 1 : -1]
@@ -122,21 +123,15 @@
for (type i=start, _c_inc=step, _c_end=(stop) - (0 < _c_inc) \
; (i <= _c_end) == (0 < _c_inc); i += _c_inc)
-#define c_with(start, end) for (start, *_c_ii = NULL; !_c_ii; ++_c_ii, end)
-#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_fordefer(...) c_MACRO_OVERLOAD(c_fordefer, __VA_ARGS__)
+#define c_fordefer_1(release) for (int _c_ii = 0; !_c_ii; ++_c_ii, release)
+#define c_fordefer_2(acquire, release) for (acquire, *_c_ii = NULL; !_c_ii; ++_c_ii, release)
-#define c_withbuf(b, type, n) c_withbuf_N(b, type, n, 256)
-#define c_withbuf_N(b, type, n, BYTES) \
+#define c_forbuffer(b, type, n) c_forbuffer_N(b, type, n, 256)
+#define c_forbuffer_N(b, type, n, BYTES) \
for (type _c_b[((BYTES) - 1) / sizeof(type) + 1], \
*b = (n)*sizeof *b > (BYTES) ? c_new_n(type, n) : _c_b \
; b; b != _c_b ? c_free(b) : (void)0, b = NULL)
-#define c_exitwith continue
-#define c_exitdefer continue
#define c_var(CX, c, ...) \
CX c = CX##_init(); c_emplace(CX, c, __VA_ARGS__)
diff --git a/include/stc/clist.h b/include/stc/clist.h
index 55ab5bf0..538c25b4 100644
--- a/include/stc/clist.h
+++ b/include/stc/clist.h
@@ -35,7 +35,7 @@
using_clist(ix, int64_t);
int main() {
- c_with (clist_ix list = clist_ix_init(), clist_ix_del(&list))
+ c_fordefer (clist_ix list = clist_ix_init(), clist_ix_del(&list))
{
stc64_t rng = stc64_init(12345);
int n;
diff --git a/include/stc/cmap.h b/include/stc/cmap.h
index efa1ddb9..9377aadd 100644
--- a/include/stc/cmap.h
+++ b/include/stc/cmap.h
@@ -31,7 +31,7 @@
using_cmap(mx, int, char); // Map of int -> char
int main(void) {
- c_with (cmap_mx m = cmap_mx_init(), cmap_mx_del(&m))
+ c_fordefer (cmap_mx m = cmap_mx_init(), cmap_mx_del(&m))
{
cmap_mx_emplace(&m, 5, 'a');
cmap_mx_emplace(&m, 8, 'b');
diff --git a/include/stc/cpque.h b/include/stc/cpque.h
index 20acf6f6..afdd41ac 100644
--- a/include/stc/cpque.h
+++ b/include/stc/cpque.h
@@ -32,7 +32,7 @@
stc64_t rng = stc64_init(1234);
stc64_uniformf_t dist = stc64_uniformf_init(10.0f, 100.0f);
- c_with (cpque_f queue = cpque_f_init(), cpque_f_del(&queue))
+ c_fordefer (cpque_f queue = cpque_f_init(), cpque_f_del(&queue))
{
// Push ten million random numbers onto the queue.
for (int i=0; i<10000000; ++i)
diff --git a/include/stc/cqueue.h b/include/stc/cqueue.h
index 37d18fba..99afc296 100644
--- a/include/stc/cqueue.h
+++ b/include/stc/cqueue.h
@@ -35,7 +35,7 @@
stc64_t rng = stc64_init(1234);
stc64_uniform_t dist = stc64_uniform_init(rng, 0, n);
- c_with (cqueue_i queue = cqueue_i_init(), cqueue_i_del(&queue))
+ c_fordefer (cqueue_i queue = cqueue_i_init(), cqueue_i_del(&queue))
{
// Push ten million random numbers onto the queue.
for (int i=0; i<n; ++i)
diff --git a/include/stc/csmap.h b/include/stc/csmap.h
index 91cf3d11..2909e0a1 100644
--- a/include/stc/csmap.h
+++ b/include/stc/csmap.h
@@ -30,7 +30,7 @@
using_csmap(mx, int, char); // Sorted map<int, char>
int main(void) {
- c_with (csmap_mx m = csmap_mx_init(), csmap_mx_del(&m))
+ c_fordefer (csmap_mx m = csmap_mx_init(), csmap_mx_del(&m))
{
csmap_mx_insert(&m, 5, 'a');
csmap_mx_insert(&m, 8, 'b');
diff --git a/include/stc/cstack.h b/include/stc/cstack.h
index 2184c89e..575e9012 100644
--- a/include/stc/cstack.h
+++ b/include/stc/cstack.h
@@ -32,7 +32,7 @@
using_cstack(i, cvec_i);
int main() {
- c_with (cstack_i stack = cstack_i_init(), cstack_i_del(&stack))
+ c_fordefer (cstack_i stack = cstack_i_init(), cstack_i_del(&stack))
{
for (int i=0; i<100; ++i)
cstack_i_push(&stack, i*i);
diff --git a/include/stc/cstr.h b/include/stc/cstr.h
index a188b1cf..54ec993b 100644
--- a/include/stc/cstr.h
+++ b/include/stc/cstr.h
@@ -307,7 +307,7 @@ STC_DEF void
cstr_replace_n(cstr* self, size_t pos, size_t len, const char* str, size_t n) {
size_t sz = cstr_size(*self);
if (len > sz - pos) len = sz - pos;
- c_withbuf (xstr, char, n) {
+ c_forbuffer (xstr, char, n) {
memcpy(xstr, str, n);
_cstr_internal_move(self, pos + len, pos + n);
memcpy(&self->str[pos], xstr, n);