diff options
| author | Tyge Løvset <[email protected]> | 2022-04-27 19:07:18 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-04-27 19:07:18 +0200 |
| commit | edfcc712b48e2a582bcad1bc5a4102f18154e549 (patch) | |
| tree | 70ed252f3bfd4e5841a25d802da630b73e58b800 | |
| parent | df73eddd7933df7c96269f11e37fc1a96916f747 (diff) | |
| download | STC-modified-edfcc712b48e2a582bcad1bc5a4102f18154e549.tar.gz STC-modified-edfcc712b48e2a582bcad1bc5a4102f18154e549.zip | |
Final fixes to carc and cbox; Reverted constructor name to make; (similarity to make_shared/make_unique in c++).
| -rw-r--r-- | docs/carc_api.md | 6 | ||||
| -rw-r--r-- | docs/cbox_api.md | 14 | ||||
| -rw-r--r-- | examples/arc_containers.c | 8 | ||||
| -rw-r--r-- | examples/arc_demo.c | 2 | ||||
| -rw-r--r-- | examples/arcvec_erase.c | 2 | ||||
| -rw-r--r-- | examples/box.c | 6 | ||||
| -rw-r--r-- | examples/box2.c | 8 | ||||
| -rw-r--r-- | examples/city.c | 3 | ||||
| -rw-r--r-- | examples/music_arc.c | 10 | ||||
| -rw-r--r-- | examples/new_sptr.c | 10 | ||||
| -rw-r--r-- | examples/person_arc.c | 8 | ||||
| -rw-r--r-- | examples/threads_demo.c | 2 | ||||
| -rw-r--r-- | include/stc/carc.h | 16 | ||||
| -rw-r--r-- | include/stc/cbox.h | 29 | ||||
| -rw-r--r-- | include/stc/template.h | 9 |
15 files changed, 77 insertions, 56 deletions
diff --git a/docs/carc_api.md b/docs/carc_api.md index 301c25b1..ea5dea1f 100644 --- a/docs/carc_api.md +++ b/docs/carc_api.md @@ -32,8 +32,8 @@ See similar c++ class [std::shared_ptr](https://en.cppreference.com/w/cpp/memory ## Methods ```c carc_X carc_X_init(); // empty shared pointer -carc_X carc_X_make(i_valraw raw); // create owned value from raw type, if defined. -carc_X carc_X_from(i_val val); // create new heap allocated object. Take ownership of val. +carc_X carc_X_from(i_valraw raw); // construct a new value in an carc from raw type. +carc_X carc_X_make(i_val val); // make a carc from constructed val object. Faster than from_ptr(). carc_X carc_X_from_ptr(i_val* p); // create a carc from raw pointer. Takes ownership of p. carc_X carc_X_clone(carc_X other); // return other with increased use count @@ -45,7 +45,7 @@ void carc_X_drop(carc_X* self); // destruct (decr long carc_X_use_count(carc_X ptr); void carc_X_reset(carc_X* self); -void carc_X_reset_from(carc_X* self, i_val val); // assign new carc with value. Takes ownership of val. +void carc_X_reset_to(carc_X* self, i_val* p); // assign new carc from ptr. Takes ownership of p. uint64_t carc_X_value_hash(const i_val* x); // hash value int carc_X_value_cmp(const i_val* x, const i_val* y); // compares pointer addresses if 'i_opt c_no_cmp' diff --git a/docs/cbox_api.md b/docs/cbox_api.md index 2e16815b..266539c6 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -31,23 +31,23 @@ compare the pointer addresses when used. Additionally, `c_no_clone` or `i_is_fwd ## Methods ```c cbox_X cbox_X_init(); // return an empty cbox -cbox_X cbox_X_make(i_valraw raw); // create owned value from raw type, if defined. -cbox_X cbox_X_from(i_val val); // allocate new heap object with val. Take ownership of val. +cbox_X cbox_X_from(i_valraw raw); // construct a new boxed object from raw type, if defined. +cbox_X cbox_X_make(i_val val); // make a cbox from constructed val object. cbox_X cbox_X_from_ptr(i_val* p); // create a cbox from a pointer. Takes ownership of p. cbox_X cbox_X_clone(cbox_X other); // return deep copied clone cbox_X cbox_X_move(cbox_X* self); // transfer ownership to another cbox. void cbox_X_take(cbox_X* self, cbox_X other); // take ownership of other. void cbox_X_copy(cbox_X* self, cbox_X other); // deep copy to self -void cbox_X_drop(cbox_X* self); // destruct the contained object and free's it. +void cbox_X_drop(cbox_X* self); // destruct the contained object and free's it. void cbox_X_reset(cbox_X* self); -void cbox_X_reset_from(cbox_X* self, i_val val); // assign new cbox with value. Takes ownership of val. +void cbox_X_reset_to(cbox_X* self, i_val* p); // assign new cbox from ptr. Takes ownership of p. -uint64_t cbox_X_value_hash(const i_val* x); // hash value -int cbox_X_value_cmp(const i_val* x, const i_val* y); // compares pointer addresses if 'i_opt c_no_cmp' +uint64_t cbox_X_value_hash(const i_val* p); // hash value +int cbox_X_value_cmp(const i_val* p, const i_val* y); // compares pointer addresses if 'i_opt c_no_cmp' // is defined. Otherwise uses 'i_cmp' or default compare. -bool cbox_X_value_eq(const i_val* x, const i_val* y); // cbox_X_value_cmp == 0 +bool cbox_X_value_eq(const i_val* p, const i_val* y); // cbox_X_value_cmp == 0 ``` ## Types and constants 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"
diff --git a/include/stc/carc.h b/include/stc/carc.h index 02b932af..c7c18f53 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -103,14 +103,18 @@ _cx_memb(_from_ptr)(_cx_value* p) { }
STC_INLINE _cx_self
-_cx_memb(_from)(i_key val) { // c++: std::make_shared<i_key>(val)
+_cx_memb(_make)(_cx_value val) { // c++: std::make_shared<_cx_value>(val)
_cx_self ptr; _cx_carc_rep *rep = c_alloc(_cx_carc_rep);
*(ptr.use_count = &rep->counter) = 1;
*(ptr.get = &rep->value) = val;
return ptr;
}
-STC_INLINE i_key _cx_memb(_toraw)(const _cx_self* self) {
+STC_INLINE _cx_raw _cx_memb(_toraw)(const _cx_self* self) {
+ return i_keyto(self->get);
+}
+
+STC_INLINE _cx_value _cx_memb(_get)(const _cx_self* self) {
return *self->get;
}
@@ -138,14 +142,16 @@ _cx_memb(_reset)(_cx_self* self) { }
STC_INLINE void
-_cx_memb(_reset_from)(_cx_self* self, i_key val) {
+_cx_memb(_reset_to)(_cx_self* self, _cx_value* p) {
_cx_memb(_drop)(self);
- *self = _cx_memb(_from)(val);
+ *self = _cx_memb(_from_ptr)(p);
}
#if !defined _i_no_clone && !defined _i_no_emplace
STC_INLINE _cx_self
- _cx_memb(_make)(_cx_raw raw) { return _cx_memb(_from)(i_keyfrom(raw)); }
+ _cx_memb(_from)(_cx_raw raw) {
+ return _cx_memb(_make)(i_keyfrom(raw));
+ }
#endif // !_i_no_clone
// does not use i_keyclone, so OK to always define.
diff --git a/include/stc/cbox.h b/include/stc/cbox.h index 2d766509..f34888e8 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -86,16 +86,23 @@ STC_INLINE long _cx_memb(_use_count)(_cx_self box) { return (long)(box.get != NULL); }
STC_INLINE _cx_self
-_cx_memb(_from_ptr)(i_key* p) { return c_make(_cx_self){p}; }
+_cx_memb(_from_ptr)(_cx_value* p) { return c_make(_cx_self){p}; }
STC_INLINE _cx_self
-_cx_memb(_from)(i_key val) { // c++: std::make_unique<i_key>(val)
- _cx_self ptr = {c_alloc(i_key)};
+_cx_memb(_make)(_cx_value val) { // c++: std::make_unique<i_key>(val)
+ _cx_self ptr = {c_alloc(_cx_value)};
*ptr.get = val; return ptr;
}
-STC_INLINE i_key
-_cx_memb(_toraw)(const _cx_self* self) { return *self->get; }
+STC_INLINE _cx_raw
+_cx_memb(_toraw)(const _cx_self* self) {
+ return i_keyto(self->get);
+}
+
+STC_INLINE _cx_value
+_cx_memb(_get)(const _cx_self* self) {
+ return *self->get;
+}
// destructor
STC_INLINE void
@@ -114,17 +121,19 @@ _cx_memb(_reset)(_cx_self* self) { _cx_memb(_drop)(self); self->get = NULL;
}
-// take ownership of val
+// take ownership of p
STC_INLINE void
-_cx_memb(_reset_from)(_cx_self* self, i_key val) {
- if (self->get) { i_keydrop(self->get); *self->get = val; }
- else self->get = c_new(i_key, val);
+_cx_memb(_reset_to)(_cx_self* self, _cx_value* p) {
+ if (self->get) i_keydrop(self->get);
+ self->get = p;
}
#if !defined _i_no_clone
#if !defined _i_no_emplace
STC_INLINE _cx_self
- _cx_memb(_make)(_cx_raw raw) { return _cx_memb(_from)(i_keyfrom(raw)); }
+ _cx_memb(_from)(_cx_raw raw) {
+ return _cx_memb(_make)(i_keyfrom(raw));
+ }
#endif
STC_INLINE _cx_self
_cx_memb(_clone)(_cx_self other) {
diff --git a/include/stc/template.h b/include/stc/template.h index 2a4a43f1..b0a98d1e 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -114,6 +114,9 @@ #elif defined i_key_arcbox
#define i_key_bind i_key_arcbox
#define i_keyraw c_paste(i_key_arcbox, _value)
+ #define i_keyfrom c_paste(i_key_arcbox, _make)
+ #define i_keyto c_paste(i_key_arcbox, _get)
+ #define i_eq c_paste(i_key_arcbox, _value_eq)
#define _i_no_emplace
#endif
@@ -167,7 +170,7 @@ #define i_keyto c_default_toraw
#endif
#ifndef i_keyclone
- #define i_keyclone(key) i_keyfrom((i_keyto((&(key)))))
+ #define i_keyclone(key) (key)
#endif
#ifndef i_keydrop
#define i_keydrop c_default_drop
@@ -197,6 +200,8 @@ #elif defined i_val_arcbox
#define i_val_bind i_val_arcbox
#define i_valraw c_paste(i_val_arcbox, _value)
+ #define i_valfrom c_paste(i_val_arcbox, _make)
+ #define i_valto c_paste(i_val_arcbox, _get)
#define _i_no_emplace
#endif
@@ -237,7 +242,7 @@ #define i_valto c_default_toraw
#endif
#ifndef i_valclone
- #define i_valclone(val) i_valfrom((i_valto((&(val)))))
+ #define i_valclone(val) (val)
#endif
#ifndef i_valdrop
#define i_valdrop c_default_drop
|
