From 7254bcc776d4dd5dd2e3e20598858618d5eddcdc Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Wed, 20 Apr 2022 09:10:50 +0200 Subject: Support for cloning containers with carc and cbox. Disabled emplace methods for containers with carc and cbox. --- examples/new_sptr.c | 9 +++++---- include/stc/carr2.h | 3 +-- include/stc/carr3.h | 3 +-- include/stc/cdeq.h | 8 +++----- include/stc/clist.h | 5 ++--- include/stc/cmap.h | 4 ++-- include/stc/cpque.h | 5 ++--- include/stc/csmap.h | 7 +++---- include/stc/cstack.h | 6 +++--- include/stc/cvec.h | 5 ++--- include/stc/template.h | 16 ++++++++++++---- 11 files changed, 36 insertions(+), 35 deletions(-) diff --git a/examples/new_sptr.c b/examples/new_sptr.c index 2c2c5d8a..f037c0a8 100644 --- a/examples/new_sptr.c +++ b/examples/new_sptr.c @@ -22,7 +22,8 @@ void Person_drop(Person* p) { // ... #define i_type SPtr #define i_val int -#define i_valdrop(x) printf("drop: %d\n", *(x)) +#define i_valdrop(x) printf("drop: %d\n", *x) +#define i_valfrom(i) i #include #define i_val_arcbox SPtr @@ -42,9 +43,9 @@ int main(void) { c_auto (cstack_iptr, stk) { puts("Ex2"); - cstack_iptr_emplace(&stk, 10); - cstack_iptr_emplace(&stk, 20); - cstack_iptr_emplace(&stk, 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/include/stc/carr2.h b/include/stc/carr2.h index 6086c926..93ab26bc 100644 --- a/include/stc/carr2.h +++ b/include/stc/carr2.h @@ -118,9 +118,8 @@ STC_DEF _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, i_val value) { STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { _cx_self _arr = _cx_memb(_init)(src.xdim, src.ydim); - i_valraw r; for (_cx_value* p = _arr.data[0], *q = src.data[0], *e = p + _cx_memb(_size)(src); p != e; ++p, ++q) - r = i_valto(q), *p = i_valfrom(r); + *p = i_valclone((*q)); return _arr; } diff --git a/include/stc/carr3.h b/include/stc/carr3.h index f7ec3227..a326105c 100644 --- a/include/stc/carr3.h +++ b/include/stc/carr3.h @@ -123,9 +123,8 @@ STC_DEF _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, size_t zdim, i STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { _cx_self _arr = _cx_memb(_init)(src.xdim, src.ydim, src.zdim); - i_valraw r; for (_cx_value* p = **_arr.data, *q = **src.data, *e = p + _cx_memb(_size)(src); p != e; ++p, ++q) - r = i_valto(q), *p = i_valfrom(r); + *p = i_valclone((*q)); return _arr; } diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 0dfe79dd..33747e02 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -75,7 +75,7 @@ STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw) { return _cx_memb(_push_back)(self, i_valfrom(raw)); } #endif STC_INLINE i_val _cx_memb(_value_clone)(i_val val) - { i_valraw r = i_valto((&val)); return i_valfrom(r); } + { return i_valclone(val); } STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->data == other.data) return; _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); @@ -303,9 +303,8 @@ _cx_memb(_clone)(_cx_self cx) { const size_t sz = cdeq_rep_(&cx)->size; _cx_self out = _cx_memb(_with_capacity)(sz); cdeq_rep_(&out)->size = sz; - i_valraw _r; for (size_t i = 0; i < sz; ++i) - _r = i_valto((cx.data + i)), out.data[i] = i_valfrom(_r); + out.data[i] = i_valclone(cx.data[i]); return out; } #endif @@ -387,9 +386,8 @@ _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2) { pos = _cx_memb(_insert_space_)(self, pos, p2 - p1); _cx_iter it = {pos}; - i_valraw r; for (; p1 != p2; ++p1) - r = i_valto(p1), *pos++ = i_valfrom(r); + *pos++ = i_valclone((*p1)); return it; } #endif // !_i_no_clone diff --git a/include/stc/clist.h b/include/stc/clist.h index c3c15900..b871dd3c 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -109,7 +109,7 @@ STC_API _cx_node* _cx_memb(_erase_after_)(_cx_self* self, _cx_node* node); #if !defined _i_no_clone STC_API _cx_self _cx_memb(_clone)(_cx_self cx); STC_INLINE i_val _cx_memb(_value_clone)(i_val val) - { i_valraw r = i_valto((&val)); return i_valfrom(r); } + { return i_valclone(val); } STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->last == other.last) return; @@ -190,9 +190,8 @@ _cx_memb(_get_mut)(_cx_self* self, i_valraw val) { STC_DEF _cx_self _cx_memb(_clone)(_cx_self cx) { _cx_self out = _cx_memb(_init)(); - i_valraw r; c_foreach (it, _cx_self, cx) - r = i_valto(it.ref), _cx_memb(_push_back)(&out, i_valfrom(r)); + _cx_memb(_push_back)(&out, i_valclone((*it.ref))); return out; } #endif diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 106a140d..07ba9fe5 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -145,8 +145,8 @@ STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { STC_INLINE _cx_value _cx_memb(_value_clone)(_cx_value _val) { - i_keyraw k = i_keyto(_i_keyref(&_val)); *_i_keyref(&_val) = i_keyfrom(k); - _i_MAP_ONLY( i_valraw m = i_valto((&_val.second)); _val.second = i_valfrom(m); ) + *_i_keyref(&_val) = i_keyclone((*_i_keyref(&_val))); + _i_MAP_ONLY( _val.second = i_valclone(_val.second); ) return _val; } diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 794c73ce..9844fc93 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -96,7 +96,7 @@ STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } STC_INLINE i_val _cx_memb(_value_clone)(_cx_value val) - { i_valraw r = i_valto((&val)); return i_valfrom(r); } + { return i_valclone(val); } #if !defined _i_no_emplace STC_INLINE void _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) @@ -127,9 +127,8 @@ _cx_memb(_make_heap)(_cx_self* self) { #if !defined _i_no_clone STC_DEF _cx_self _cx_memb(_clone)(_cx_self q) { _cx_self out = _cx_memb(_with_capacity)(q.size); - i_valraw r; for (; out.size < out.capacity; ++q.data) - r = i_valto(q.data), out.data[out.size++] = i_valfrom(r); + out.data[out.size++] = i_valclone((*q.data)); return out; } #endif diff --git a/include/stc/csmap.h b/include/stc/csmap.h index 21b8f6e1..d89a9fdc 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -475,13 +475,12 @@ _cx_memb(_copy)(_cx_self *self, _cx_self other) { STC_DEF _cx_value _cx_memb(_value_clone)(_cx_value _val) { - i_keyraw k = i_keyto(_i_keyref(&_val)); - *_i_keyref(&_val) = i_keyfrom(k); - _i_MAP_ONLY( i_valraw m = i_valto((&_val.second)); - _val.second = i_valfrom(m); ) + *_i_keyref(&_val) = i_keyclone((*_i_keyref(&_val))); + _i_MAP_ONLY( _val.second = i_valclone(_val.second); ) return _val; } + static _cx_size _cx_memb(_clone_r_)(_cx_self* self, _cx_node* src, _cx_size sn) { if (sn == 0) return 0; diff --git a/include/stc/cstack.h b/include/stc/cstack.h index b33319c5..7e20a632 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -111,18 +111,18 @@ STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, _cx_raw raw) STC_INLINE _cx_self _cx_memb(_clone)(_cx_self v) { _cx_self out = {(_cx_value *) c_malloc(v.size*sizeof(_cx_value)), v.size, v.size}; - i_valraw r; for (size_t i = 0; i < v.size; ++v.data) - r = i_valto(v.data), out.data[i++] = i_valfrom(r); + out.data[i++] = i_valclone((*v.data)); return out; } + STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->data == other.data) return; _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } STC_INLINE i_val _cx_memb(_value_clone)(_cx_value val) - { i_valraw r = i_valto((&val)); return i_valfrom(r); } + { return i_valclone(val); } STC_INLINE i_valraw _cx_memb(_value_toraw)(_cx_value* val) { return i_valto(val); } diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 52cac08a..b666e791 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -97,7 +97,7 @@ STC_API _cx_self _cx_memb(_clone)(_cx_self cx); STC_API _cx_iter _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2); STC_INLINE i_val _cx_memb(_value_clone)(_cx_value val) - { i_valraw r = i_valto((&val)); return i_valfrom(r); } + { return i_valclone(val); } STC_INLINE i_val _cx_memb(_value_fromraw)(i_valraw raw) { return i_valfrom(raw); } STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->data == other.data) return; @@ -363,9 +363,8 @@ _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2) { pos = _cx_memb(_insert_space_)(self, pos, p2 - p1); _cx_iter it = {pos}; - i_valraw r; for (; p1 != p2; ++p1) - r = i_valto(p1), *pos++ = i_valfrom(r); + *pos++ = i_valclone((*p1)); return it; } diff --git a/include/stc/template.h b/include/stc/template.h index 4509d59c..1c63a096 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_keyclone c_paste(i_key_arcbox, _clone) #define _i_no_emplace #endif @@ -96,11 +97,9 @@ #if defined i_keyraw && !defined i_keyfrom #error "if i_keyraw is defined, i_keyfrom (and normally i_keyto) must be defined" -#endif -#if defined i_drop +#elif defined i_drop #error "i_drop not supported. Define i_keydrop/i_valdrop instead." -#endif -#if defined i_from +#elif defined i_from #error "i_from not supported. Define i_keyfrom/i_valfrom instead." #endif @@ -113,6 +112,7 @@ #elif defined i_val_arcbox #define i_val_bind i_val_arcbox #define i_valraw c_paste(i_val_arcbox, _value) + #define i_valclone c_paste(i_val_arcbox, _clone) #define _i_no_emplace #endif @@ -190,6 +190,9 @@ #ifndef i_keyto #define i_keyto c_default_toraw #endif + #ifndef i_keyclone + #define i_keyclone(key) i_keyfrom((i_keyto((&(key))))) + #endif #ifndef i_keydrop #define i_keydrop c_default_drop #endif @@ -210,6 +213,9 @@ #ifndef i_valto #define i_valto c_default_toraw #endif +#ifndef i_valclone + #define i_valclone(val) i_valfrom((i_valto((&(val))))) +#endif #ifndef i_valdrop #define i_valdrop c_default_drop #endif @@ -244,6 +250,7 @@ #undef i_valfrom #undef i_valto #undef i_valdrop +#undef i_valclone #undef i_key #undef i_key_str @@ -253,6 +260,7 @@ #undef i_keyfrom #undef i_keyto #undef i_keydrop +#undef i_keyclone #undef _i_prefix #undef _i_no_clone -- cgit v1.2.3