diff options
| author | Tyge Løvset <[email protected]> | 2021-09-18 12:51:45 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-09-18 12:51:45 +0200 |
| commit | 9ddc57529865a54bf964702034ff41f938e8538a (patch) | |
| tree | c2f22074226b11f1afde7e526d8720b29f3fc7ff | |
| parent | 9eb269ca92000bc4bd835f1038aed88324791e1f (diff) | |
| download | STC-modified-9ddc57529865a54bf964702034ff41f938e8538a.tar.gz STC-modified-9ddc57529865a54bf964702034ff41f938e8538a.zip | |
Changed the sharedptr.c example. Removed clist_X_erase(): was alias of clist_X_erase_at().
| -rw-r--r-- | examples/sharedptr.c | 57 | ||||
| -rw-r--r-- | include/stc/clist.h | 2 |
2 files changed, 36 insertions, 23 deletions
diff --git a/examples/sharedptr.c b/examples/sharedptr.c index 76072e2f..a8132fee 100644 --- a/examples/sharedptr.c +++ b/examples/sharedptr.c @@ -16,29 +16,44 @@ void int_del(int* x) { int main()
{
- c_forauto (cvec_int, vec)
- c_forauto (csset_int, set)
+ c_forauto (cvec_int, vec) // raii
+ c_forauto (csset_int, set) // raii
{
- csset_int_insert(&set, csptr_int_make(2021));
- csset_int_insert(&set, csptr_int_make(2012));
- csset_int_insert(&set, csptr_int_make(2022));
- csset_int_insert(&set, csptr_int_make(2015));
+ cvec_int_push_back(&vec, csptr_int_make(2021));
+ cvec_int_push_back(&vec, csptr_int_make(2012));
+ cvec_int_push_back(&vec, csptr_int_make(2022));
+ cvec_int_push_back(&vec, csptr_int_make(2015));
- // add odd numbers from set to vec:
- c_foreach (i, csset_int, set) {
- if (*i.ref->get & 1)
- cvec_int_push_back(&vec, csptr_int_clone(*i.ref));
- }
-
- printf("print set:");
- c_foreach (i, csset_int, set) {
- printf(" %d", *i.ref->get);
- }
-
- printf("\nprint vec:");
- c_foreach (i, cvec_int, vec) {
- printf(" %d", *i.ref->get);
- }
+ printf("vec:");
+ c_foreach (i, cvec_int, vec) printf(" %d", *i.ref->get);
puts("");
+
+ // add odd numbers from vec to set
+ c_foreach (i, cvec_int, vec)
+ if (*i.ref->get & 1)
+ csset_int_emplace(&set, *i.ref); // copy shared pointer => increments ref.
+
+ // erase the two last elements in vec
+ cvec_int_pop_back(&vec);
+ cvec_int_pop_back(&vec);
+
+ printf("vec:");
+ c_foreach (i, cvec_int, vec) printf(" %d", *i.ref->get);
+
+ printf("\nset:");
+ c_foreach (i, csset_int, set) printf(" %d", *i.ref->get);
+
+ puts("\nDone");
}
}
+
+/* output:
+vec: 2021 2012 2022 2015
+del: 2022
+vec: 2021 2012
+set: 2015 2021
+Done
+del: 2015
+del: 2021
+del: 2012
+*/
diff --git a/include/stc/clist.h b/include/stc/clist.h index f7d09c74..83e08c24 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -119,8 +119,6 @@ STC_INLINE i_val cx_memb(_value_clone)(i_val val) { return i_valfrom(i_valto(&val)); }
STC_INLINE void cx_memb(_pop_front)(Self* self)
{ cx_memb(_erase_after_)(self, self->last); }
-STC_INLINE cx_iter_t cx_memb(_erase)(Self* self, cx_iter_t it)
- { return cx_memb(_erase_at)(self, it); }
STC_INLINE cx_value_t* cx_memb(_emplace_back)(Self* self, i_valraw raw)
{ return cx_memb(_push_back)(self, i_valfrom(raw)); }
STC_INLINE cx_value_t* cx_memb(_emplace_front)(Self* self, i_valraw raw)
|
