From 7c88fc11ef62d5be83fe34fe72da6eadfd64ba6b Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 20 Apr 2022 11:24:11 +0200 Subject: Some cleanup after carc / cbox updates. --- docs/carc_api.md | 7 +------ docs/cbox_api.md | 7 +------ examples/arc_demo.c | 36 ++++++++++++++++++------------------ include/stc/carc.h | 6 ++---- include/stc/cbox.h | 6 +++--- 5 files changed, 25 insertions(+), 37 deletions(-) diff --git a/docs/carc_api.md b/docs/carc_api.md index 23c7ad0a..cc5b3f41 100644 --- a/docs/carc_api.md +++ b/docs/carc_api.md @@ -15,11 +15,6 @@ additional synchronization even if these instances are copies and share ownershi When declaring a container with shared pointers, define `i_val_bind` as the carc type, see example. -Make sure to pass the result of create functions like *carc_X_from()* **only** to *insert()*, -*push_back()*, and *push()* functions, as it is *moved* into the container. Use *emplace()* -method for sharing existing **carc**s between containers or other existing shared pointers, -as they clone/share the input internally. - See similar c++ class [std::shared_ptr](https://en.cppreference.com/w/cpp/memory/shared_ptr) for a functional reference, or Rust [std::sync::Arc](https://doc.rust-lang.org/std/sync/struct.Arc.html) / [std::rc::Rc](https://doc.rust-lang.org/std/rc/struct.Rc.html). ## Header file and declaration @@ -37,7 +32,7 @@ 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); // like carc_X_from(), but construct owned value from raw. +carc_X carc_X_make(i_valraw raw); // construct owned value from raw type carc_X carc_X_from(i_val val); // create new heap allocated object. Take ownership of val. carc_X carc_X_from_ptr(i_val* p); // create a carc from raw pointer. Takes ownership of p. diff --git a/docs/cbox_api.md b/docs/cbox_api.md index 962727b1..556c56df 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -9,10 +9,6 @@ When declaring a container of **cbox** values, define `i_val_arcbox` with the cbox type instead of defining `i_val`. This will auto-set `i_valdrop`, `i_valfrom`, and `i_cmp` using functions defined by the specified **cbox**. -For containers, make sure to pass the result of create functions like *cbox_X_make()* **only** to -*insert()*, *push_back()*, and *push()* functions. Use the *emplace()* functions in order to -auto-*clone* an already existing/owned cbox element. - See similar c++ class [std::unique_ptr](https://en.cppreference.com/w/cpp/memory/unique_ptr) for a functional reference, or Rust [std::boxed::Box](https://doc.rust-lang.org/std/boxed/struct.Box.html) ## Header file and declaration @@ -35,8 +31,7 @@ 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); // like cbox_X_from(), but create owned value from raw. - // NB! available only if i_valraw is defined. +cbox_X cbox_X_make(i_valraw raw); // create owned value from raw type. cbox_X cbox_X_from(i_val val); // allocate new heap object with val. Take ownership of val. cbox_X cbox_X_from_ptr(i_val* p); // create a cbox from a pointer. Takes ownership of p. diff --git a/examples/arc_demo.c b/examples/arc_demo.c index 12f81b8d..132e940a 100644 --- a/examples/arc_demo.c +++ b/examples/arc_demo.c @@ -8,46 +8,46 @@ void int_drop(int* x) { // carc implements its own clone method using reference counting, // so 'i_valfrom' need not be defined (will be ignored). -#define i_type iref // set type name to be defined (instead of 'carc_int') +#define i_type Arc // set type name to be defined (instead of 'carc_int') #define i_val int -#define i_valdrop int_drop // optional, just to display the elements destroyed -#include // iref +//#define i_valdrop int_drop // optional, just to display the elements destroyed +#include // Arc -#define i_key_arcbox iref // note: use i_key_bind instead of i_key for carc/cbox elements -#include // csset_iref (like: std::set>) +#define i_key_arcbox Arc // note: use i_key_bind instead of i_key for carc/cbox elements +#include // csset_Arc (like: std::set>) -#define i_val_arcbox iref // note: as above. -#include // cvec_iref (like: std::vector>) +#define i_val_arcbox Arc // note: as above. +#include // cvec_Arc (like: std::vector>) int main() { - c_auto (cvec_iref, vec) // declare and init vec, call cvec_iref_drop() at scope exit - c_auto (csset_iref, set) // declare and init set, call csset_iref_drop() at scope exit + c_auto (cvec_Arc, vec) // declare and init vec, call cvec_Arc_drop() at scope exit + c_auto (csset_Arc, set) // declare and init set, call csset_Arc_drop() at scope exit { const int years[] = {2021, 2012, 2022, 2015}; c_forrange (i, c_arraylen(years)) - cvec_iref_push_back(&vec, iref_from(years[i])); + cvec_Arc_push_back(&vec, Arc_make(years[i])); printf("vec:"); - c_foreach (i, cvec_iref, vec) printf(" %d", *i.ref->get); + c_foreach (i, cvec_Arc, vec) printf(" %d", *i.ref->get); puts(""); // add odd numbers from vec to set - c_foreach (i, cvec_iref, vec) + c_foreach (i, cvec_Arc, vec) if (*i.ref->get & 1) - csset_iref_insert(&set, iref_clone(*i.ref)); // copy shared pointer => increments counter. + csset_Arc_insert(&set, Arc_clone(*i.ref)); // copy shared pointer => increments counter. // erase the two last elements in vec - cvec_iref_pop_back(&vec); - cvec_iref_pop_back(&vec); + cvec_Arc_pop_back(&vec); + cvec_Arc_pop_back(&vec); printf("vec:"); - c_foreach (i, cvec_iref, vec) printf(" %d", *i.ref->get); + c_foreach (i, cvec_Arc, vec) printf(" %d", *i.ref->get); printf("\nset:"); - c_foreach (i, csset_iref, set) printf(" %d", *i.ref->get); + c_foreach (i, csset_Arc, set) printf(" %d", *i.ref->get); - c_autovar (iref p = iref_clone(vec.data[0]), iref_drop(&p)) { + c_autovar (Arc p = Arc_clone(vec.data[0]), Arc_drop(&p)) { printf("\n%d is now owned by %ld objects\n", *p.get, *p.use_count); } diff --git a/include/stc/carc.h b/include/stc/carc.h index 2ed7da4c..a061a606 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -143,10 +143,8 @@ _cx_memb(_reset_from)(_cx_self* self, i_val val) { *self = _cx_memb(_from)(val); } -#if !defined _i_no_clone - STC_INLINE _cx_self - _cx_memb(_make)(_cx_raw raw) { return _cx_memb(_from)(i_valfrom(raw)); } -#endif +STC_INLINE _cx_self +_cx_memb(_make)(_cx_raw raw) { return _cx_memb(_from)(i_valfrom(raw)); } // does not use i_valfrom, so we can bypass c_no_clone STC_INLINE _cx_self diff --git a/include/stc/cbox.h b/include/stc/cbox.h index 4305701b..690c8a7f 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -117,10 +117,10 @@ _cx_memb(_reset_from)(_cx_self* self, i_val val) { else self->get = c_new(i_val, val); } -#if !defined _i_no_clone - STC_INLINE _cx_self - _cx_memb(_make)(_cx_raw raw) { return _cx_memb(_from)(i_valfrom(raw)); } +STC_INLINE _cx_self +_cx_memb(_make)(_cx_raw raw) { return _cx_memb(_from)(i_valfrom(raw)); } +#if !defined _i_no_clone STC_INLINE _cx_self _cx_memb(_clone)(_cx_self other) { if (!other.get) return other; -- cgit v1.2.3