From c2ab107bc1bcd196b10b1d5f130a1a32449f7773 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 25 Sep 2020 10:27:07 +0200 Subject: Added cptr.h and good examples for cptr and csptr (share_ptr.c). Some internal refactor. --- examples/shared_ptr.c | 48 ------------------------------------------------ 1 file changed, 48 deletions(-) delete mode 100644 examples/shared_ptr.c (limited to 'examples/shared_ptr.c') diff --git a/examples/shared_ptr.c b/examples/shared_ptr.c deleted file mode 100644 index 7bff502c..00000000 --- a/examples/shared_ptr.c +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include -#include -#include - -typedef struct { cstr_t name, last; } Person; - -Person* Person_make(Person* p, const char* name, const char* last) { - p->name = cstr(name), p->last = cstr(last); - return p; -} -void Person_del(Person* p) { - printf("del %s\n", p->name.str); - c_del(cstr, &p->name, &p->last); -} -int Person_compare(const Person* p, const Person* q) { - int cmp = cstr_equals_s(p->name, q->name); - return cmp == 0 ? cstr_equals_s(p->last, q->last) : cmp; -} - -//using_clist(p, Person, Person_del); -//using_cqueue(p, clist_p); - -using_csptr(p, Person, Person_del); -int csptr_p_compare(const csptr_p* p, const csptr_p* q) {return Person_compare(p->get, q->get);} - -using_clist(pp, csptr_p, csptr_p_del, csptr_p_compare); -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.val->get->name.str, i.val->get->last.str); - - cqueue_pp_del(&queue); -} \ No newline at end of file -- cgit v1.2.3