summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/smartpointers/map_ptr.c
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-07-24 08:48:41 +0200
committerGitHub <[email protected]>2023-07-24 08:48:41 +0200
commit374b3c27831cd4e09461867ed231669777b96951 (patch)
tree88011006f6d536cdb1ad1eca8073392ca80687cc /misc/examples/smartpointers/map_ptr.c
parent177418232a2d8a8b0df1667d3e4bd15dc37db59f (diff)
parent650b053f443f9132dadb6d1ca924c0b36849739f (diff)
downloadSTC-modified-374b3c27831cd4e09461867ed231669777b96951.tar.gz
STC-modified-374b3c27831cd4e09461867ed231669777b96951.zip
Merge pull request #65 from stclib/dev43
Dev43
Diffstat (limited to 'misc/examples/smartpointers/map_ptr.c')
-rw-r--r--misc/examples/smartpointers/map_ptr.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/misc/examples/smartpointers/map_ptr.c b/misc/examples/smartpointers/map_ptr.c
new file mode 100644
index 00000000..453322c5
--- /dev/null
+++ b/misc/examples/smartpointers/map_ptr.c
@@ -0,0 +1,34 @@
+#include <stc/ccommon.h>
+#include <stdio.h>
+#define i_implement
+#include <stc/cstr.h>
+
+// cmap of cstr => long*
+#define i_type Ptrmap
+#define i_key_str
+#define i_val long*
+#define i_valraw long
+#define i_valfrom(raw) c_new(long, raw)
+#define i_valto(x) **x
+#define i_valclone(x) c_new(long, *x)
+#define i_valdrop(x) c_free(*x)
+#include <stc/cmap.h>
+
+int main(void)
+{
+ Ptrmap map = {0};
+
+ puts("Map cstr => long*:");
+ Ptrmap_insert(&map, cstr_from("Test1"), c_new(long, 1));
+ Ptrmap_insert(&map, cstr_from("Test2"), c_new(long, 2));
+
+ // Simple: emplace() implicitly creates cstr from const char* and an owned long* from long!
+ Ptrmap_emplace(&map, "Test3", 3);
+ Ptrmap_emplace(&map, "Test4", 4);
+
+ c_forpair (name, number, Ptrmap, map)
+ printf("%s: %ld\n", cstr_str(_.name), **_.number);
+ puts("");
+
+ Ptrmap_drop(&map);
+}