diff options
| -rw-r--r-- | README.md | 21 | ||||
| -rw-r--r-- | docs/csmap_api.md | 42 | ||||
| -rw-r--r-- | docs/csview_api.md | 46 |
3 files changed, 54 insertions, 55 deletions
@@ -215,17 +215,16 @@ and non-emplace methods: using_cvec_str(); // vector of string (cstr)
...
cvec_str vec = cvec_str_init();
-cstr s = cstr_from("a new string");
-
-cvec_str_push_back(&vec, cstr_from("Hello")); // construct and add string
-cvec_str_push_back(&vec, cstr_clone(s)); // clone and add an existing string
-
-cvec_str_emplace_back(&vec, "Yay, literal"); // internally constructs cstr from string-literal
-cvec_str_emplace_back(&vec, cstr_clone(s)); // <-- COMPILE ERROR: wrong input type
-cvec_str_emplace_back(&vec, s.str); // Ok: const char* input type (= rawvalue).
-
-cstr_del(&s);
-cvec_del(&vec);
+cstr s = cstr_new("a string literal"); // cstr_new() for literals; no strlen() usage
+c_defer (cvec_str_del(&vec), cstr_del(&s)) // defer the destructors to end of block:
+{
+ cvec_str_push_back(&vec, cstr_from("Hello")); // construct and add string from const char*
+ cvec_str_push_back(&vec, cstr_clone(s)); // clone and add an existing string
+
+ cvec_str_emplace_back(&vec, "Yay, literal"); // internally constructs cstr from string-literal
+ cvec_str_emplace_back(&vec, cstr_clone(s)); // <-- COMPILE ERROR: wrong input type
+ cvec_str_emplace_back(&vec, s.str); // Ok: const char* input type (= rawvalue).
+}
```
This is made possible because the **using**-declarations may be given an optional
conversion/"rawvalue"-type as template parameter, along with a back and forth conversion
diff --git a/docs/csmap_api.md b/docs/csmap_api.md index a5d0aeee..73f19c18 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -150,17 +150,18 @@ int main() {100, "Red"}, {110, "Blue"}, }); - /* put replaces existing mapped value: */ - csmap_id_emplace_or_assign(&idnames, 110, "White"); - /* put a constructed mapped value into map: */ - csmap_id_insert_or_assign(&idnames, 120, cstr_from_fmt("#%08x", col)); - /* emplace adds only when key does not exist: */ - csmap_id_emplace(&idnames, 100, "Green"); - - c_foreach (i, csmap_id, idnames) - printf("%d: %s\n", i.ref->first, i.ref->second.str); - - csmap_id_del(&idnames); + c_defer (csmap_id_del(&idnames)) + { + /* put replaces existing mapped value: */ + csmap_id_emplace_or_assign(&idnames, 110, "White"); + /* put a constructed mapped value into map: */ + csmap_id_insert_or_assign(&idnames, 120, cstr_from_fmt("#%08x", col)); + /* emplace adds only when key does not exist: */ + csmap_id_emplace(&idnames, 100, "Green"); + + c_foreach (i, csmap_id, idnames) + printf("%d: %s\n", i.ref->first, i.ref->second.str); + } } ``` Output: @@ -189,17 +190,16 @@ using_csmap(vi, Vec3i, int, Vec3i_compare); int main() { - csmap_vi vecs = csmap_vi_init(); - - csmap_vi_emplace(&vecs, (Vec3i){100, 0, 0}, 1); - csmap_vi_emplace(&vecs, (Vec3i){ 0, 100, 0}, 2); - csmap_vi_emplace(&vecs, (Vec3i){ 0, 0, 100}, 3); - csmap_vi_emplace(&vecs, (Vec3i){100, 100, 100}, 4); - - c_foreach (i, csmap_vi, vecs) - printf("{ %3d, %3d, %3d }: %d\n", i.ref->first.x, i.ref->first.y, i.ref->first.z, i.ref->second); + c_withvar (csmap_vi, vecs) // define, init and defer destruction of vecs + { + csmap_vi_emplace(&vecs, (Vec3i){100, 0, 0}, 1); + csmap_vi_emplace(&vecs, (Vec3i){ 0, 100, 0}, 2); + csmap_vi_emplace(&vecs, (Vec3i){ 0, 0, 100}, 3); + csmap_vi_emplace(&vecs, (Vec3i){100, 100, 100}, 4); - csmap_vi_del(&vecs); + c_foreach (i, csmap_vi, vecs) + printf("{ %3d, %3d, %3d }: %d\n", i.ref->first.x, i.ref->first.y, i.ref->first.z, i.ref->second); + } } ``` Output: diff --git a/docs/csview_api.md b/docs/csview_api.md index 3a965565..dfa9a5a6 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -125,32 +125,32 @@ int main() printf("%s\nLength: %zu\n\n", text.str, text.size); // cvec of cstr elements, using csview as "emplace" type - c_var (cvec_sv, vec, { - c_sv("Element 1"), + c_var (cvec_sv, vec, { // defines vec with 3 cstr elements. + c_sv("Element 1"), // will be converted to cstr. c_sv("Element 2"), c_sv("Element 3") }); - - // push constructed cstr directly - cvec_sv_push_back(&vec, cstr_new("Second last element")); - // emplace constructs cstr from a csview - cvec_sv_emplace_back(&vec, c_sv("Last element")); - - c_foreach (i, cvec_sv, vec) - printf("%s\n", i.ref->str); - - c_var (cmap_si, map, { - {c_sv("hello"), 100}, - {c_sv("world"), 200} - }); - cmap_si_emplace(&map, c_sv("gone mad"), 300); - - // cmap_si_get() knows the length of "world" without strlen(). - cmap_si_value_t* v = cmap_si_get(&map, c_sv("world")); - printf("\n%s: %d\n", v->first.str, v->second); - - cmap_si_del(&map); - cvec_sv_del(&vec); + c_defer (cvec_sv_del(&vec)) // defer destruction to end of block. + { + // push constructed cstr directly + cvec_sv_push_back(&vec, cstr_new("Second last element")); + // emplace constructs cstr from a csview + cvec_sv_emplace_back(&vec, c_sv("Last element")); + + c_foreach (i, cvec_sv, vec) + printf("%s\n", i.ref->str); + } + + c_withvar (cmap_si, map) // defines map and defers destruction. + { + cmap_si_emplace(&map, c_sv("hello"), 100); + cmap_si_emplace(&map, c_sv("world"), 200); + cmap_si_emplace(&map, c_sv("gone mad"), 300); + + // Efficient lookup: no string allocation or strlen() takes place: + cmap_si_value_t* v = cmap_si_get(&map, c_sv("world")); + printf("\n%s: %d\n", v->first.str, v->second); + } } ``` Output: |
