diff options
| author | Tyge Løvset <[email protected]> | 2021-12-19 13:19:25 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-12-19 13:19:25 +0100 |
| commit | 3fe68afddf494d6ac72f8919b2739e80774cffe8 (patch) | |
| tree | 10fca26d5f76e75e364fe79dc5bf05f625b579dc | |
| parent | 92b950333c6c7002bdbf1b60af44a249dc0cef9c (diff) | |
| download | STC-modified-3fe68afddf494d6ac72f8919b2739e80774cffe8.tar.gz STC-modified-3fe68afddf494d6ac72f8919b2739e80774cffe8.zip | |
Improved box.c and added a corresponding sptr.c example.
| -rw-r--r-- | examples/box.c | 41 | ||||
| -rw-r--r-- | examples/sptr.c | 66 |
2 files changed, 92 insertions, 15 deletions
diff --git a/examples/box.c b/examples/box.c index d400799b..598d0bdc 100644 --- a/examples/box.c +++ b/examples/box.c @@ -23,38 +23,49 @@ void Person_drop(Person* p) { c_drop(cstr, &p->name, &p->last);
}
-#define i_type PPtr
+#define i_type PBox
#define i_val_bind Person // binds Person_cmp, ...
#include <stc/cbox.h>
#define i_type Persons
-#define i_val_bind PPtr // binds PPtr_cmp, ...
+#define i_val_bind PBox // binds PBox_cmp, ...
#include <stc/cvec.h>
int main()
{
c_auto (Persons, vec)
- c_auto (PPtr, p, q)
+ c_auto (PBox, p, q)
{
- p = PPtr_new(Person_new("Dave", "Cooper"));
+ p = PBox_new(Person_new("Laura", "Palmer"));
- q = PPtr_clone(p);
- cstr_assign(&q.get->name, "Dale");
+ q = PBox_clone(p);
+ cstr_assign(&q.get->name, "Leland");
- printf("%s %s.\n", p.get->name.str, p.get->last.str);
- printf("%s %s.\n", q.get->name.str, q.get->last.str);
+ printf("orig: %s %s\n", p.get->name.str, p.get->last.str);
+ printf("copy: %s %s\n", q.get->name.str, q.get->last.str);
- Persons_push_back(&vec, PPtr_new(Person_new("Laura", "Palmer")));
- Persons_push_back(&vec, PPtr_new(Person_new("Shelly", "Johnson")));
+ Persons_push_back(&vec, PBox_new(Person_new("Dale", "Cooper")));
+ Persons_push_back(&vec, PBox_new(Person_new("Audrey", "Home")));
+
+ // NB! Clone p and q to the vector using emplace_back()
+ c_apply(Persons, emplace_back, &vec, {p, q});
c_foreach (i, Persons, vec)
- printf("%s: %s\n", i.ref->get->name.str, i.ref->get->last.str);
+ printf("%s %s\n", i.ref->get->name.str, i.ref->get->last.str);
+ puts("");
- // Look-up Shelly!
- c_autovar (Person s = Person_new("Shelly", "Johnson"), Person_drop(&s)) {
- const PPtr *v = Persons_get(&vec, (PPtr){&s});
- if (v) printf("found: %s: %s\n", v->get->name.str, v->get->last.str);
+ // Look-up Audrey! Use a (fake) temporary PBox for lookup.
+ c_autovar (Person a = Person_new("Audrey", "Home"), Person_drop(&a)) {
+ const PBox *v = Persons_get(&vec, (PBox){&a});
+ if (v) printf("found: %s %s\n", v->get->name.str, v->get->last.str);
}
+ puts("");
+
+ // Alternative to use cbox (when not placed in container).
+ Person *she = c_new(Person, Person_new("Shelly", "Johnson"));
+ printf("%s %s\n", she->name.str, she->last.str);
+ c_delete(Person, she); // drop and free
+ puts("");
}
}
diff --git a/examples/sptr.c b/examples/sptr.c new file mode 100644 index 00000000..7e0f7357 --- /dev/null +++ b/examples/sptr.c @@ -0,0 +1,66 @@ +/* cbox: heap allocated boxed type */
+#include <stc/cstr.h>
+
+typedef struct { cstr name, last; } Person;
+
+Person Person_new(const char* name, const char* last) {
+ return (Person){.name = cstr_from(name), .last = cstr_from(last)};
+}
+
+int Person_cmp(const Person* a, const Person* b) {
+ int c = strcmp(a->name.str, b->name.str);
+ return c ? c : strcmp(a->last.str, b->last.str);
+}
+
+Person Person_clone(Person p) {
+ p.name = cstr_clone(p.name);
+ p.last = cstr_clone(p.last);
+ return p;
+}
+
+void Person_drop(Person* p) {
+ printf("drop: %s %s\n", p->name.str, p->last.str);
+ c_drop(cstr, &p->name, &p->last);
+}
+
+#define i_type PSPtr
+#define i_val_bind Person // binds Person_cmp, ...
+#include <stc/csptr.h>
+
+#define i_type Persons
+#define i_val_bind PSPtr // binds PSPtr_cmp, ...
+#include <stc/cvec.h>
+
+
+int main()
+{
+ c_auto (Persons, vec)
+ c_auto (PSPtr, p, q)
+ {
+ p = PSPtr_new(Person_new("Laura", "Palmer"));
+
+ // We want a deep copy -- PSPtr_clone(p) only shares!
+ q = PSPtr_new(Person_clone(*p.get));
+ cstr_assign(&q.get->name, "Leland");
+
+ printf("orig: %s %s\n", p.get->name.str, p.get->last.str);
+ printf("copy: %s %s\n", q.get->name.str, q.get->last.str);
+
+ Persons_push_back(&vec, PSPtr_new(Person_new("Dale", "Cooper")));
+ Persons_push_back(&vec, PSPtr_new(Person_new("Audrey", "Home")));
+
+ // NB! Clone/share p and q to the vector using emplace_back()
+ c_apply(Persons, emplace_back, &vec, {p, q});
+
+ c_foreach (i, Persons, vec)
+ printf("%s %s\n", i.ref->get->name.str, i.ref->get->last.str);
+ puts("");
+
+ // Look-up Audrey! Use a (fake) temporary PSPtr for lookup.
+ c_autovar (Person a = Person_new("Audrey", "Home"), Person_drop(&a)) {
+ const PSPtr *v = Persons_get(&vec, (PSPtr){&a});
+ if (v) printf("found: %s %s\n", v->get->name.str, v->get->last.str);
+ }
+ puts("");
+ }
+}
|
