summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-09-24 08:27:35 +0200
committerTyge Løvset <[email protected]>2020-09-24 08:27:35 +0200
commitbad270ce0034ffbd237bc9ac4a407b0cfe6b1088 (patch)
treecaa99ba6eb64843bd4558aa94a8a3f3715ed8cc5
parent0ca173d91a5d75b3f4939766085129d394085d0b (diff)
downloadSTC-modified-bad270ce0034ffbd237bc9ac4a407b0cfe6b1088.tar.gz
STC-modified-bad270ce0034ffbd237bc9ac4a407b0cfe6b1088.zip
Added shared_ptr.c example
-rw-r--r--examples/shared_ptr.c38
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