From edfcc712b48e2a582bcad1bc5a4102f18154e549 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 27 Apr 2022 19:07:18 +0200 Subject: Final fixes to carc and cbox; Reverted constructor name to make; (similarity to make_shared/make_unique in c++). --- examples/arc_containers.c | 8 ++++---- examples/arc_demo.c | 2 +- examples/arcvec_erase.c | 2 +- examples/box.c | 6 +++--- examples/box2.c | 8 ++++---- examples/city.c | 3 ++- examples/music_arc.c | 10 +++++----- examples/new_sptr.c | 10 +++++----- examples/person_arc.c | 8 ++++---- examples/threads_demo.c | 2 +- 10 files changed, 30 insertions(+), 29 deletions(-) (limited to 'examples') diff --git a/examples/arc_containers.c b/examples/arc_containers.c index d68b078a..a58e24ab 100644 --- a/examples/arc_containers.c +++ b/examples/arc_containers.c @@ -29,17 +29,17 @@ int main() { // POPULATE stack with shared pointers to Maps: Map *map; - map = Stack_push(&stack, Arc_from(Map_init()))->get; + map = Stack_push(&stack, Arc_make(Map_init()))->get; c_apply(v, Map_emplace(map, c_pair(v)), Map_raw, { {"Joey", 1990}, {"Mary", 1995}, {"Joanna", 1992} }); - map = Stack_push(&stack, Arc_from(Map_init()))->get; + map = Stack_push(&stack, Arc_make(Map_init()))->get; c_apply(v, Map_emplace(map, c_pair(v)), Map_raw, { {"Rosanna", 2001}, {"Brad", 1999}, {"Jack", 1980} }); // POPULATE list: - map = List_push_back(&list, Arc_from(Map_init()))->get; + map = List_push_back(&list, Arc_make(Map_init()))->get; c_apply(v, Map_emplace(map, c_pair(v)), Map_raw, { {"Steve", 1979}, {"Rick", 1974}, {"Tracy", 2003} }); @@ -50,7 +50,7 @@ int main() // Clone (deep copy) a Map from the stack to the list // List will contain two shared and two unshared maps. - map = List_push_back(&list, Arc_from(Map_clone(*stack.data[1].get)))->get; + map = List_push_back(&list, Arc_make(Map_clone(*stack.data[1].get)))->get; // Add one more element to the cloned map: Map_emplace_or_assign(map, "CLONED", 2021); diff --git a/examples/arc_demo.c b/examples/arc_demo.c index a55c868e..132e940a 100644 --- a/examples/arc_demo.c +++ b/examples/arc_demo.c @@ -26,7 +26,7 @@ int main() { const int years[] = {2021, 2012, 2022, 2015}; c_forrange (i, c_arraylen(years)) - cvec_Arc_push_back(&vec, Arc_from(years[i])); + cvec_Arc_push_back(&vec, Arc_make(years[i])); printf("vec:"); c_foreach (i, cvec_Arc, vec) printf(" %d", *i.ref->get); diff --git a/examples/arcvec_erase.c b/examples/arcvec_erase.c index 10d8d4c0..e4497486 100644 --- a/examples/arcvec_erase.c +++ b/examples/arcvec_erase.c @@ -22,7 +22,7 @@ int main() { const int v[] = {2012, 1990, 2012, 2019, 2015}; c_forrange (i, c_arraylen(v)) - Vec_push_back(&vec, Arc_from(v[i])); + Vec_push_back(&vec, Arc_make(v[i])); // clone the second 2012 and push it back. // note: cloning make sure that vec.data[2] has ref count 2. diff --git a/examples/box.c b/examples/box.c index c50ced1e..423e9cb1 100644 --- a/examples/box.c +++ b/examples/box.c @@ -40,7 +40,7 @@ int main() c_auto (Persons, vec) c_auto (PBox, p, q) { - p = PBox_from(Person_new("Laura", "Palmer")); + p = PBox_make(Person_new("Laura", "Palmer")); q = PBox_clone(p); cstr_assign(&q.get->name, "Leland"); @@ -48,8 +48,8 @@ int main() printf("orig: %s %s\n", cstr_str(&p.get->name), cstr_str(&p.get->last)); printf("copy: %s %s\n", cstr_str(&q.get->name), cstr_str(&q.get->last)); - Persons_push_back(&vec, PBox_from(Person_new("Dale", "Cooper"))); - Persons_push_back(&vec, PBox_from(Person_new("Audrey", "Home"))); + Persons_push_back(&vec, PBox_make(Person_new("Dale", "Cooper"))); + Persons_push_back(&vec, PBox_make(Person_new("Audrey", "Home"))); // NB! Clone p and q to the vector using emplace_back() c_apply(v, Persons_push_back(&vec, PBox_clone(v)), PBox, {p, q}); diff --git a/examples/box2.c b/examples/box2.c index 6d118e2c..2d1b82fc 100644 --- a/examples/box2.c +++ b/examples/box2.c @@ -38,7 +38,7 @@ Point origin(void) { cbox_Point boxed_origin(void) { // Allocate this point on the heap, and return a pointer to it - return cbox_Point_from((Point){ .x=0.0, .y=0.0 }); + return cbox_Point_make((Point){ .x=0.0, .y=0.0 }); } @@ -56,16 +56,16 @@ int main(void) { c_auto (cbox_Point, boxed_point) c_auto (cbox_BoxPoint, box_in_a_box) { - boxed_rectangle = cbox_Rectangle_from((Rectangle){ + boxed_rectangle = cbox_Rectangle_make((Rectangle){ .top_left = origin(), .bottom_right = (Point){ .x=3.0, .y=-4.0 } }); // The output of functions can be boxed - boxed_point = cbox_Point_from(origin()); + boxed_point = cbox_Point_make(origin()); // Double indirection - box_in_a_box = cbox_BoxPoint_from(boxed_origin()); + box_in_a_box = cbox_BoxPoint_make(boxed_origin()); printf("Point occupies %" PRIuMAX " bytes on the stack\n", sizeof(point)); diff --git a/examples/city.c b/examples/city.c index ec3338a7..d2178494 100644 --- a/examples/city.c +++ b/examples/city.c @@ -13,6 +13,7 @@ static inline int City_cmp(const City* a, const City* b) { } static inline uint64_t City_hash(const City* a) { + printf("hash %s\n", cstr_str(&a->name)); return cstr_hash(&a->name) ^ cstr_hash(&a->country); } @@ -51,7 +52,7 @@ int main(void) { struct City_s { const char *name, *country; float lat, lon; int pop; }; - c_apply(c, Cities_push(&cities, CityArc_from((City){cstr_from(c.name), cstr_from(c.country), + c_apply(c, Cities_push(&cities, CityArc_make((City){cstr_from(c.name), cstr_from(c.country), c.lat, c.lon, c.pop})), struct City_s, { {"New York", "US", 4.3, 23.2, 9000000}, {"Paris", "France", 4.3, 23.2, 9000000}, diff --git a/examples/music_arc.c b/examples/music_arc.c index e36477f2..24cb25f1 100644 --- a/examples/music_arc.c +++ b/examples/music_arc.c @@ -32,9 +32,9 @@ void example3() c_auto (SongVec, vec, vec2) { c_apply(v, SongVec_push_back(&vec, v), SongPtr, { - SongPtr_from(Song_new("Bob Dylan", "The Times They Are A Changing")), - SongPtr_from(Song_new("Aretha Franklin", "Bridge Over Troubled Water")), - SongPtr_from(Song_new("Thalia", "Entre El Mar y Una Estrella")) + SongPtr_make(Song_new("Bob Dylan", "The Times They Are A Changing")), + SongPtr_make(Song_new("Aretha Franklin", "Bridge Over Troubled Water")), + SongPtr_make(Song_new("Thalia", "Entre El Mar y Una Estrella")) }); c_foreach (s, SongVec, vec) @@ -42,8 +42,8 @@ void example3() SongVec_push_back(&vec2, SongPtr_clone(*s.ref)); c_apply(v, SongVec_push_back(&vec2, v), SongPtr, { - SongPtr_from(Song_new("Michael Jackson", "Billie Jean")), - SongPtr_from(Song_new("Rihanna", "Stay")), + SongPtr_make(Song_new("Michael Jackson", "Billie Jean")), + SongPtr_make(Song_new("Rihanna", "Stay")), }); c_foreach (s, SongVec, vec2) diff --git a/examples/new_sptr.c b/examples/new_sptr.c index 4f80c478..57c67f94 100644 --- a/examples/new_sptr.c +++ b/examples/new_sptr.c @@ -34,18 +34,18 @@ int main(void) { c_auto (carc_person, p, q, r, s) { puts("Ex1"); - p = carc_person_from(Person_new("John", "Smiths")); + p = carc_person_make(Person_new("John", "Smiths")); q = carc_person_clone(p); r = carc_person_clone(p); - s = carc_person_from(Person_clone(*p.get)); // deep copy + s = carc_person_make(Person_clone(*p.get)); // deep copy printf("%s %s. uses: %lu\n", cstr_str(&r.get->name), cstr_str(&s.get->last), *p.use_count); } c_auto (cstack_iptr, stk) { puts("Ex2"); - cstack_iptr_push(&stk, SPtr_from(10)); - cstack_iptr_push(&stk, SPtr_from(20)); - cstack_iptr_push(&stk, SPtr_from(30)); + cstack_iptr_push(&stk, SPtr_make(10)); + cstack_iptr_push(&stk, SPtr_make(20)); + cstack_iptr_push(&stk, SPtr_make(30)); cstack_iptr_push(&stk, SPtr_clone(*cstack_iptr_top(&stk))); cstack_iptr_push(&stk, SPtr_clone(*cstack_iptr_begin(&stk).ref)); diff --git a/examples/person_arc.c b/examples/person_arc.c index 3e3f662f..40707740 100644 --- a/examples/person_arc.c +++ b/examples/person_arc.c @@ -41,17 +41,17 @@ int main() c_auto (Persons, vec) c_auto (PSPtr, p, q) { - p = PSPtr_from(Person_new("Laura", "Palmer")); + p = PSPtr_make(Person_new("Laura", "Palmer")); // We want a deep copy -- PSPtr_clone(p) only shares! - q = PSPtr_from(Person_clone(*p.get)); + q = PSPtr_make(Person_clone(*p.get)); cstr_assign(&q.get->name, "Leland"); printf("orig: %s %s\n", cstr_str(&p.get->name), cstr_str(&p.get->last)); printf("copy: %s %s\n", cstr_str(&q.get->name), cstr_str(&q.get->last)); - Persons_push_back(&vec, PSPtr_from(Person_new("Dale", "Cooper"))); - Persons_push_back(&vec, PSPtr_from(Person_new("Audrey", "Home"))); + Persons_push_back(&vec, PSPtr_make(Person_new("Dale", "Cooper"))); + Persons_push_back(&vec, PSPtr_make(Person_new("Audrey", "Home"))); // Clone/share p and q to the vector c_apply(v, Persons_push_back(&vec, PSPtr_clone(v)), PSPtr, {p, q}); diff --git a/examples/threads_demo.c b/examples/threads_demo.c index 24ac951a..98c86e52 100644 --- a/examples/threads_demo.c +++ b/examples/threads_demo.c @@ -37,7 +37,7 @@ void* thr(BaseArc* p) int main() { - BaseArc p = BaseArc_from((Base){0}); + BaseArc p = BaseArc_make((Base){0}); mtx_init(&mtx, mtx_plain); printf("Created a shared Base\n" -- cgit v1.2.3