diff options
| author | Tyge Løvset <[email protected]> | 2020-09-25 21:19:23 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-09-25 21:19:23 +0200 |
| commit | 45bfe3f95e0c6287b0c6a6da488744718c8f43f9 (patch) | |
| tree | ecf7ef5fcfcfd65b1f3e72cd489a2960257d9707 | |
| parent | d9fa3685127ee1166491458e3d7e3fe1b50bdf31 (diff) | |
| download | STC-modified-45bfe3f95e0c6287b0c6a6da488744718c8f43f9.tar.gz STC-modified-45bfe3f95e0c6287b0c6a6da488744718c8f43f9.zip | |
Added ex.
| -rw-r--r-- | examples/share_ptr2.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/examples/share_ptr2.c b/examples/share_ptr2.c new file mode 100644 index 00000000..726a9bc0 --- /dev/null +++ b/examples/share_ptr2.c @@ -0,0 +1,43 @@ +#include <stc/csptr.h>
+#include <stc/cmap.h>
+#include <stc/cstr.h>
+#include <stdio.h>
+
+typedef struct { cstr_t name, last; } Person;
+
+Person* Person_make(Person* p, cstr_t name, cstr_t last) {
+ printf("make %s\n", name.str);
+ p->name = name, p->last = last;
+ return p;
+}
+Person* Person_from(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);
+}
+
+using_csptr(pe, Person, Person_del);
+using_cmap(pe, int, csptr_pe, csptr_pe_del);
+
+
+int main() {
+ cmap_pe map = cmap_pe_init();
+
+ puts("Emplace 10:");
+ // c_try_emplace: The last argument is completely ignored if key already exist in map, so no memory leak happens!
+ c_forrange (i, 20) { // When i>9, all key will exist, so value arg is not executed.
+ c_try_emplace(&map, cmap_pe, (i * 7) % 10,
+ csptr_pe_make(Person_make(c_new(Person), cstr_from("Name %d", (i * 7) % 10),
+ cstr_from("Last %d", (i * 9) % 10))));
+ }
+ c_try_emplace(&map, cmap_pe, 11, csptr_pe_make(Person_from(c_new(Person), "Hello", "world!")));
+
+ c_foreach (i, cmap_pe, map)
+ printf(" %d: %s\n", i.val->first, i.val->second.get->name.str);
+
+ puts("Destroy map:");
+ cmap_pe_del(&map);
+}
\ No newline at end of file |
