From d63d4a2a23dca342f418a5ccf8adf46e0d08d95a Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 23 Dec 2021 15:14:47 +0100 Subject: Fix new_sptr.c example. --- examples/new_sptr.c | 34 +++++++++++++++++++++------------- include/stc/ccommon.h | 4 ++-- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/examples/new_sptr.c b/examples/new_sptr.c index 23fe5791..09bb7137 100644 --- a/examples/new_sptr.c +++ b/examples/new_sptr.c @@ -5,43 +5,51 @@ struct Person { cstr name, last; } typedef Person; Person Person_new(const char* name, const char* last) { return (Person){.name = cstr_from(name), .last = cstr_from(last)}; } +Person Person_clone(Person p) { + p.name = cstr_clone(p.name), p.last = cstr_clone(p.last); + return p; +} void Person_drop(Person* p) { printf("drop: %s %s\n", p->name.str, p->last.str); c_drop(cstr, &p->name, &p->last); } -#define i_val Person -#define i_drop Person_drop +#define i_val_bind Person #define i_opt c_no_cmp #define i_tag person #include // ... - +#define i_type SPtr #define i_val int #define i_drop(x) printf("drop: %d\n", *(x)) #include -#define i_val_bind carc_int +#define i_val_sptr SPtr #define i_tag iptr #include int main(void) { - c_autovar (carc_person p = carc_person_from(Person_new("John", "Smiths")), carc_person_drop(&p)) - c_autovar (carc_person q = carc_person_clone(p), carc_person_drop(&q)) // share the pointer + c_auto (carc_person, p, q, r, s) { - printf("%s %s. uses: %lu\n", q.get->name.str, q.get->last.str, *q.use_count); + puts("Ex1"); + p = carc_person_from(Person_new("John", "Smiths")); + q = carc_person_clone(p); + r = carc_person_clone(p); + s = carc_person_from(Person_clone(*p.get)); // deep copy + printf("%s %s. uses: %lu\n", r.get->name.str, s.get->last.str, *p.use_count); } c_auto (cstack_iptr, stk) { - cstack_iptr_push(&stk, carc_int_from(10)); - cstack_iptr_push(&stk, carc_int_from(20)); - cstack_iptr_push(&stk, carc_int_from(30)); - cstack_iptr_push(&stk, *cstack_iptr_top(&stk)); - cstack_iptr_push(&stk, *cstack_iptr_begin(&stk).ref); + puts("Ex2"); + cstack_iptr_emplace(&stk, 10); + cstack_iptr_emplace(&stk, 20); + cstack_iptr_emplace(&stk, 30); + cstack_iptr_push(&stk, SPtr_clone(*cstack_iptr_top(&stk))); + cstack_iptr_push(&stk, SPtr_clone(*cstack_iptr_begin(&stk).ref)); c_foreach (i, cstack_iptr, stk) - printf(" %d", *i.ref->get); + printf(" (%d, uses %ld)", *i.ref->get, *i.ref->use_count); puts(""); } } \ No newline at end of file diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 4e2ee97d..8ae0dfae 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -192,12 +192,12 @@ STC_INLINE uint64_t c_default_hash(const void* key, size_t len) { #define c_apply(v, method, T, ...) do { \ const T _c_arr[] = __VA_ARGS__; \ for (size_t index = 0; index < c_arraylen(_c_arr); ++index) \ - { const T v = _c_arr[index]; method; } \ + { T v = _c_arr[index]; method; } \ } while (0) #define c_apply_arr(v, method, T, arr, n) do { \ const T* _c_arr = arr; size_t _n = n; \ for (size_t index = 0; index < _n; ++index) \ - { const T v = _c_arr[index]; method; } \ + { T v = _c_arr[index]; method; } \ } while (0) #define c_apply_cnt(v, method, C, ...) do { \ size_t index = 0; \ -- cgit v1.2.3