From bad270ce0034ffbd237bc9ac4a407b0cfe6b1088 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 24 Sep 2020 08:27:35 +0200 Subject: Added shared_ptr.c example --- examples/shared_ptr.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 examples/shared_ptr.c (limited to 'examples/shared_ptr.c') diff --git a/examples/shared_ptr.c b/examples/shared_ptr.c new file mode 100644 index 00000000..40f67e11 --- /dev/null +++ b/examples/shared_ptr.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +typedef struct { cstr_t name, last; } Person; + +void Person_del(Person* p) { + printf("del %s\n", p->name.str); + cstr_del(&p->name); cstr_del(&p->last); +} + +//using_clist(p, Person, Person_del); +//using_cqueue(p, clist_p); + +using_csptr(p, Person, Person_del); +using_clist(pp, csptr_p, csptr_p_del); +using_cqueue(pp, clist_pp); + +int main() { + cqueue_pp queue = cqueue_pp_init(); + + // push() and pop() a few. + c_forrange (i, 20) { + csptr_p p = csptr_p_make(c_new(Person)); + p.get->name = cstr_from("Name %d", i); + p.get->last = cstr_from("Last %d", i); + cqueue_pp_push(&queue, p); + } + + c_forrange (5) + cqueue_pp_pop(&queue); + + c_foreach (i, cqueue_pp, queue) + printf(" %s %s\n", i.get->get->name.str, i.get->get->last.str); + + cqueue_pp_del(&queue); +} \ No newline at end of file -- cgit v1.2.3