From 77e61266489a2e69693f714b601f3824d16ae1ba Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 21 May 2021 16:02:25 +0200 Subject: API change: c_breakwith => c_exitwith, c_breakdefer => c_exitdefer. Added csmap_insert.c example. --- docs/ccommon_api.md | 13 ++++--- examples/csmap_insert.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ include/stc/ccommon.h | 8 ++-- 3 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 examples/csmap_insert.c diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 3a2ee521..7e329cfe 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -2,12 +2,13 @@ The following handy macros are safe to use, i.e. have no side-effects. -### c_defer, c_with, c_withvar, c_withbuf, c_breakwith -General *defer* mechanics. These macros allows to specify destructors explicitly or implicitly where the -constructors are defined. This ensures that resources are released after usage, and avoids memory leaks. -**c_with / c_withvar** are inspired by Python's *with* statement. +### 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. -**NB**: ***Only*** use `c_breakwith` or `c_breakdefer` to break out of `c_with`/`c_defer`-blocks. ***Never*** use `return`, `break`, or `goto` inside such a block. +**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. | Usage | Description | |:---------------------------------|:--------------------------------------------------| @@ -19,7 +20,7 @@ constructors are defined. This ensures that resources are released after usage, 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. +which destructs the object. These are available for all STC containers. ```c // Example: Load each line of a text file into a vector of strings diff --git a/examples/csmap_insert.c b/examples/csmap_insert.c new file mode 100644 index 00000000..e207a2a6 --- /dev/null +++ b/examples/csmap_insert.c @@ -0,0 +1,98 @@ +#include +#include +#include +#include + +// This implements the std::map insert c++ example at: +// https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-19 + + +using_csmap(ii, int, int); // Map of int => int +using_csmap_strval(istr, int); // Map of int => cstr +using_cvec(ii, csmap_ii_rawvalue_t, c_no_compare); + +void print_ii(csmap_ii map) { + c_foreach (e, csmap_ii, map) + printf("(%d, %d) ", e.ref->first, e.ref->second); + puts(""); +} +void print_istr(csmap_istr map) { + c_foreach (e, csmap_istr, map) + printf("(%d, %s) ", e.ref->first, e.ref->second.str); + puts(""); +} + +int main() +{ + // insert single values + c_with (csmap_ii m1 = csmap_ii_init(), csmap_ii_del(&m1)) { + csmap_ii_insert(&m1, 1, 10); + csmap_ii_insert(&m1, 2, 20); + + puts("The original key and mapped values of m1 are:"); + print_ii(m1); + + // intentionally attempt a duplicate, single element + csmap_ii_result_t ret = csmap_ii_insert(&m1, 1, 111); + if (!ret.inserted) { + csmap_ii_value_t pr = *ret.ref; + puts("Insert failed, element with key value 1 already exists."); + printf(" The existing element is (%d, %d)\n", pr.first, pr.second); + } + else { + puts("The modified key and mapped values of m1 are:"); + print_ii(m1); + } + puts(""); + + csmap_ii_insert(&m1, 3, 30); + puts("The modified key and mapped values of m1 are:"); + print_ii(m1); + puts(""); + } + + // 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) { + 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}); + cvec_ii_push_back(&v, (cvec_ii_value_t){42, 277}); + cvec_ii_push_back(&v, (cvec_ii_value_t){44, 311}); + + puts("Inserting the following vector data into m2:"); + c_foreach (e, cvec_ii, v) printf("(%d, %d) ", e.ref->first, e.ref->second); + puts(""); + + csmap_ii_emplace_items(&m2, v.data, cvec_ii_size(v)); + + puts("The modified key and mapped values of m2 are:"); + c_foreach (e, cvec_ii, v) printf("(%d, %d) ", e.ref->first, e.ref->second); + puts("\n"); + } + + // The templatized versions move-constructing elements + c_withvar (csmap_istr, m3) { + csmap_istr_value_t ip1 = {475, cstr_lit("blue")}, ip2 = {510, cstr_lit("green")}; + + // single element + csmap_istr_insert(&m3, ip1.first, cstr_move(&ip1.second)); + puts("After the first move insertion, m3 contains:"); + print_istr(m3); + + // single element + csmap_istr_insert(&m3, ip2.first, cstr_move(&ip2.second)); + puts("After the second move insertion, m3 contains:"); + print_istr(m3); + puts(""); + } + + c_withvar (csmap_ii, 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:"); + print_ii(m4); + puts(""); + } +} \ No newline at end of file diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 2bc45a21..bf9d6aed 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -135,15 +135,15 @@ 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_breakwith continue -#define c_breakdefer continue +#define c_exitwith continue +#define c_exitdefer continue #define c_var(CX, c, ...) \ CX c = CX##_init(); c_emplace(CX, c, __VA_ARGS__) -#define c_emplace(CX, c, ...) do { \ +#define c_emplace(CX, cx, ...) do { \ const CX##_rawvalue_t _c_arr[] = __VA_ARGS__; \ - CX##_emplace_items(&(c), _c_arr, c_arraylen(_c_arr)); \ + CX##_emplace_items(&(cx), _c_arr, c_arraylen(_c_arr)); \ } while (0) #define c_del(CX, ...) do { \ -- cgit v1.2.3