From 8d1f5cfac2515738e41cfc8f9e7b96d8c264c349 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 3 Jan 2021 13:18:44 +0100 Subject: Added macro: c_defcnt(): define container and push items onto it. --- examples/words.c | 7 +++---- stc/ccommon.h | 3 +++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/words.c b/examples/words.c index 7e80c452..c8e17deb 100644 --- a/examples/words.c +++ b/examples/words.c @@ -11,18 +11,17 @@ using_cmap_strkey(si, int); int main1() { - clist_str lwords = clist_inits; - c_push_items(&lwords, clist_str, { + c_defcnt (clist_str, lwords, { "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" }); + clist_str_push_back(&lwords, cstr_from_fmt("%f", 123897.0 / 23.0)); c_foreach (w, clist_str, lwords) printf("%s\n", w.ref->str); puts(""); - cvec_str words = cvec_inits; - c_push_items(&words, cvec_str, { + c_defcnt (cvec_str, words, { "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" }); diff --git a/stc/ccommon.h b/stc/ccommon.h index 797180ce..d595c0eb 100644 --- a/stc/ccommon.h +++ b/stc/ccommon.h @@ -120,6 +120,9 @@ ctype##_push_n(self, __arr, sizeof __arr/sizeof *__arr); \ } while (0) +#define c_defcnt(ctype, cnt, ...) \ + ctype cnt = ctype##_init(); c_push_items(&cnt, ctype, __VA_ARGS__) + #define c_del(ctype, ...) do { \ ctype##_t* __arr[] = {__VA_ARGS__}; \ for (size_t __i=0; __i