diff options
Diffstat (limited to 'examples/shared_ptr.c')
| -rw-r--r-- | examples/shared_ptr.c | 38 |
1 files changed, 38 insertions, 0 deletions
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 <stc/cqueue.h>
+#include <stc/cstr.h>
+#include <stc/csptr.h>
+#include <stdio.h>
+
+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 |
