From a9c0f215e3e98c165de07d9661595b0dd64cc4fb Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Tue, 19 Apr 2022 21:15:49 +0200 Subject: Renamed cbox_new/carc_new to cbox_make/carc_make. Disabled container emplace on arcbox elements. --- docs/carc_api.md | 2 +- docs/cbox_api.md | 4 ++-- examples/arc_containers.c | 6 +++--- include/stc/carc.h | 2 +- include/stc/cbox.h | 2 +- include/stc/template.h | 2 ++ 6 files changed, 10 insertions(+), 8 deletions(-) diff --git a/docs/carc_api.md b/docs/carc_api.md index 8fb752f6..23c7ad0a 100644 --- a/docs/carc_api.md +++ b/docs/carc_api.md @@ -37,7 +37,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_new(i_valraw raw); // like carc_X_from(), but construct owned value from raw. +carc_X carc_X_make(i_valraw raw); // like carc_X_from(), but construct owned value from raw. 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 0faea829..f1e93e62 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -9,7 +9,7 @@ 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_new()* **only** to +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. @@ -35,7 +35,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_new(i_valraw raw); // like cbox_X_from(), but create owned value from raw. +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_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_containers.c b/examples/arc_containers.c index cd049c4d..d68b078a 100644 --- a/examples/arc_containers.c +++ b/examples/arc_containers.c @@ -43,15 +43,15 @@ int main() c_apply(v, Map_emplace(map, c_pair(v)), Map_raw, { {"Steve", 1979}, {"Rick", 1974}, {"Tracy", 2003} }); - + // Share two Maps from the stack with the list using emplace (clone the carc): List_push_back(&list, Arc_clone(stack.data[0])); List_push_back(&list, Arc_clone(stack.data[1])); - + // 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; - + // Add one more element to the cloned map: Map_emplace_or_assign(map, "CLONED", 2021); diff --git a/include/stc/carc.h b/include/stc/carc.h index 374044f3..2ed7da4c 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -145,7 +145,7 @@ _cx_memb(_reset_from)(_cx_self* self, i_val val) { #if !defined _i_no_clone STC_INLINE _cx_self - _cx_memb(_new)(_cx_raw raw) { return _cx_memb(_from)(i_valfrom(raw)); } + _cx_memb(_make)(_cx_raw raw) { return _cx_memb(_from)(i_valfrom(raw)); } #endif // does not use i_valfrom, so we can bypass c_no_clone diff --git a/include/stc/cbox.h b/include/stc/cbox.h index d7c9ece9..4305701b 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -119,7 +119,7 @@ _cx_memb(_reset_from)(_cx_self* self, i_val val) { #if !defined _i_no_clone STC_INLINE _cx_self - _cx_memb(_new)(_cx_raw raw) { return _cx_memb(_from)(i_valfrom(raw)); } + _cx_memb(_make)(_cx_raw raw) { return _cx_memb(_from)(i_valfrom(raw)); } STC_INLINE _cx_self _cx_memb(_clone)(_cx_self other) { diff --git a/include/stc/template.h b/include/stc/template.h index e46b7390..4509d59c 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -63,6 +63,7 @@ #elif defined i_key_arcbox #define i_key_bind i_key_arcbox #define i_keyraw c_paste(i_key_arcbox, _value) + #define _i_no_emplace #endif #ifdef i_key_bind @@ -112,6 +113,7 @@ #elif defined i_val_arcbox #define i_val_bind i_val_arcbox #define i_valraw c_paste(i_val_arcbox, _value) + #define _i_no_emplace #endif #ifdef i_val_bind -- cgit v1.2.3