diff options
| author | Tyge Løvset <[email protected]> | 2020-12-09 10:44:10 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-12-09 10:44:10 +0100 |
| commit | 07779ebe405086ea3a88ea379a00d6e2762c7f13 (patch) | |
| tree | 85477a2084cc5b27d8661e3ffff64ec948c76eb3 /docs/cptr_api.md | |
| parent | 8c9d0c640c19d6325f1c53918da64831c9ae2669 (diff) | |
| download | STC-modified-07779ebe405086ea3a88ea379a00d6e2762c7f13.tar.gz STC-modified-07779ebe405086ea3a88ea379a00d6e2762c7f13.zip | |
Update cptr_api.md
Diffstat (limited to 'docs/cptr_api.md')
| -rw-r--r-- | docs/cptr_api.md | 63 |
1 files changed, 49 insertions, 14 deletions
diff --git a/docs/cptr_api.md b/docs/cptr_api.md index ee4b8aa7..506cdcff 100644 --- a/docs/cptr_api.md +++ b/docs/cptr_api.md @@ -1,8 +1,8 @@ # Container cptr: Shared Ptr -This describes the API of the queue type **cptr**. +This describes the API of the type **cptr** and the shared pointer type **csptr**. The type **csptr** is a wrapper struct for - Declaration +## Declaration ```c #define using_cptr(X, Value, valueDestroy=c_default_del, @@ -31,7 +31,7 @@ affect the names of all cptr types and methods. E.g. declaring `using_cptr(my, c | `csptr_X_iter_t` | " | csptr iterator | -Header file +## Header file All cptr definitions and prototypes may be included in your C source file by including a single header file. @@ -39,7 +39,7 @@ All cptr definitions and prototypes may be included in your C source file by inc #include "stc/cptr.h" ``` - Methods +## Methods ```c cptr_X cptr_X_init(void); @@ -60,8 +60,7 @@ int csptr_X_compare(csptr_X* x, csptr_X* y); int csptr_pointer_compare(cptr_X_value_t* x, cptr_X_value_t* y); ``` - - Example +## Example ```c #include <stc/cptr.h> #include <stc/cmap.h> @@ -75,24 +74,60 @@ Person* Person_from(Person* p, cstr_t name, cstr_t last) { return p; } void Person_del(Person* p) { + printf("del: %s %s\n", p->name.str, p->last.str); c_del(cstr, &p->name, &p->last); } +using_cmap(pv, int, Person, Person_del); // mapped: Person + +using_cptr(pp, Person, Person_del, c_no_compare); +using_cmap(pp, int, cptr_pp, cptr_pp_del); // mapped: Person* + using_csptr(ps, Person, Person_del, c_no_compare); -using_cmap(ps, int, csptr_ps, csptr_ps_del); +using_cmap(ps, int, csptr_ps, csptr_ps_del); // mapped: csptr<Person> int main() { - cmap_ps map = cmap_inits; - cmap_ps_emplace(&map, 1990, csptr_ps_from(Person_from(c_new(Person), cstr_from("Joe"), cstr_from("Average")); - cmap_ps_emplace(&map, 1985, csptr_ps_from(Person_from(c_new(Person), cstr_from("John"), cstr_from("Smith")); + cmap_pv map1 = cmap_inits; // mapped: Person + cmap_pv_put(&map1, 1990, (Person){cstr_from("Joe 1"), cstr_from("Average")}); + cmap_pv_put(&map1, 1985, (Person){cstr_from("John 1"), cstr_from("Smith")}); + c_foreach (i, cmap_pv, map1) + printf(" %d: %s\n", i.val->first, i.val->second.name.str); + cmap_pv_del(&map1); + + cmap_pp map2 = cmap_inits; // mapped: Person* + cmap_pp_put(&map2, 1990, Person_from(c_new(Person), cstr_from("Joe 2"), cstr_from("Average"))); + cmap_pp_put(&map2, 1985, Person_from(c_new(Person), cstr_from("John 2"), cstr_from("Smith"))); + c_foreach (i, cmap_pp, map2) + printf(" %d: %s\n", i.val->first, i.val->second->name.str); + cmap_pp_del(&map2); + + cmap_ps map3 = cmap_inits; // mapped: csptr<Person> + cmap_ps_put(&map3, 1990, csptr_ps_from(Person_from(c_new(Person), cstr_from("Joe 3"), cstr_from("Average")))); + cmap_ps_put(&map3, 1985, csptr_ps_from(Person_from(c_new(Person), cstr_from("John 3"), cstr_from("Smith")))); + + csptr_ps x = csptr_ps_share(*cmap_ps_at(&map3, 1990)); // share an item in the map + c_foreach (i, cmap_ps, map3) + printf(" %d: %s\n", i.val->first, i.val->second.get->name.str); + cmap_ps_del(&map3); - c_foreach (i, cmap_ps, map) - printf(" %d: %s\n", i.val->first, i.val->second.get->name.str); - cmap_ps_del(&map); + printf("x: 1990: %s\n", x.get->name.str); + csptr_ps_del(&x); } ``` Output: ``` -top: 81 + 1990: Joe 1 + 1985: John 1 +del: Joe 1 Average +del: John 1 Smith + 1990: Joe 2 + 1985: John 2 +del: Joe 2 Average +del: John 2 Smith + 1990: Joe 3 + 1985: John 3 +del: John 3 Smith +x: 1990: Joe 3 +del: Joe 3 Average ``` |
