diff options
| author | Tyge Lovset <[email protected]> | 2022-12-20 23:31:51 +0100 |
|---|---|---|
| committer | Tyge Lovset <[email protected]> | 2022-12-20 23:31:51 +0100 |
| commit | 5f57d597cd27aef55adbcb3b452973b0c6e33667 (patch) | |
| tree | dfd59c2fd0e36a6ef37912a9d0cc5a65970f1524 /misc/examples/rawptr_elements.c | |
| parent | 1763be8c8cbbc0896477fcf924edd4180d1345a9 (diff) | |
| download | STC-modified-5f57d597cd27aef55adbcb3b452973b0c6e33667.tar.gz STC-modified-5f57d597cd27aef55adbcb3b452973b0c6e33667.zip | |
Restructured folders: examples, benchmarks, tests into misc folder.
Diffstat (limited to 'misc/examples/rawptr_elements.c')
| -rw-r--r-- | misc/examples/rawptr_elements.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/misc/examples/rawptr_elements.c b/misc/examples/rawptr_elements.c new file mode 100644 index 00000000..4b3d2056 --- /dev/null +++ b/misc/examples/rawptr_elements.c @@ -0,0 +1,55 @@ +#include <stc/ccommon.h> +#include <stdio.h> + +#include <stc/cstr.h> +// Map of cstr => int64 pointers +typedef int64_t inttype; + +// Do it without cbox: +#define i_type SIPtrMap +#define i_key_str +#define i_val inttype* +#define i_valraw inttype +#define i_valfrom(raw) c_new(inttype, raw) +#define i_valto(x) **x +#define i_valclone(x) c_new(inttype, *x) +#define i_valdrop(x) c_free(*x) +#include <stc/cmap.h> + +// With cbox: +#define i_type IBox +#define i_val int +#include <stc/cbox.h> //<stc/carc.h> + +#define i_type SIBoxMap +#define i_key_str +#define i_valboxed IBox +#include <stc/cmap.h> + +int main() +{ + c_auto (SIPtrMap, map, m1) + c_auto (SIBoxMap, m2) + { + printf("\nMap with pointer elements:\n"); + SIPtrMap_insert(&map, cstr_from("testing"), c_new(inttype, 1)); + SIPtrMap_insert(&map, cstr_from("done"), c_new(inttype, 2)); + + // Emplace: implicit key, val construction: + SIPtrMap_emplace(&map, "hello", 3); + SIPtrMap_emplace(&map, "goodbye", 4); + + m1 = SIPtrMap_clone(map); + + c_forpair (name, number, SIPtrMap, m1) + printf("%s: %" PRId64 "\n", cstr_str(_.name), **_.number); + + + puts("\nIBox map:"); + SIBoxMap_insert(&m2, cstr_from("Hello"), IBox_make(123)); + SIBoxMap_emplace(&m2, "World", 999); + c_forpair (name, number, SIBoxMap, m2) + printf("%s: %d\n", cstr_str(_.name), *_.number->get); + puts(""); + } +} |
