From 23c177bbe65f83353aa5fd716d75cb4121acdba6 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 29 Sep 2021 09:35:19 +0200 Subject: Changed how to specify shared pointers in containers. --- docs/csptr_api.md | 27 +++++++++++++++------------ examples/new_sptr.c | 2 +- examples/sharedptr.c | 31 ++++++++++--------------------- examples/sptr_ex.c | 10 +++++++--- examples/words.c | 2 +- include/stc/template.h | 20 ++++++++------------ 6 files changed, 42 insertions(+), 50 deletions(-) diff --git a/docs/csptr_api.md b/docs/csptr_api.md index 176c4238..026ce6c3 100644 --- a/docs/csptr_api.md +++ b/docs/csptr_api.md @@ -13,7 +13,7 @@ All **csptr** functions can be called by multiple threads on different instances additional synchronization even if these instances are copies and share ownership of the same object. **csptr** uses thread-safe atomic reference counting, through the *csptr_X_clone()* and *csptr_X_del()* methods. -When declaring a container with shared pointers, define the `i_val_csptr` with the csptr's `i_tag`. +When declaring a container with shared pointers, define the `i_val_csptr` with the full csptr type. See example. Also for containers, make sure to pass the result of *csptr_X_make()* to *insert*, *push_back*, @@ -74,25 +74,28 @@ void int_del(int* x) { } #define i_val int -#define i_valdel int_del // optional func to show elements destroyed -#include // define csptr_int shared pointers +#define i_valdel int_del // optional func to display elements destroyed +#include // csptr_int -#define i_key_csptr int // refer to csptr_int definition above -#include // define a sorted set of csptr_int +#define i_key_csptr csptr_int +#define i_tag int +#include // csset_int: csset -#define i_val_csptr int // refer to csptr_int definition above -#include // define a vector of csptr_int +#define i_val_csptr csptr_int +#define i_tag int +#include // cvec_int: cvec int main() { c_auto (cvec_int, vec) // declare and init vec, call del at scope exit c_auto (csset_int, set) // declare and init set, call del at scope exit { - 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)); - + c_apply(cvec_int, push_back, &vec, { + csptr_int_make(2021)), + csptr_int_make(2012)), + csptr_int_make(2022)), + csptr_int_make(2015)), + }); printf("vec:"); c_foreach (i, cvec_int, vec) printf(" %d", *i.ref->get); puts(""); diff --git a/examples/new_sptr.c b/examples/new_sptr.c index 002f6ade..aad6192a 100644 --- a/examples/new_sptr.c +++ b/examples/new_sptr.c @@ -23,7 +23,7 @@ void Person_del(Person* p) { #include #define i_tag iptr -#define i_key_csptr int +#define i_key_csptr csptr_int #include int main(void) { diff --git a/examples/sharedptr.c b/examples/sharedptr.c index e684b9a2..e062025a 100644 --- a/examples/sharedptr.c +++ b/examples/sharedptr.c @@ -5,20 +5,20 @@ void int_del(int* x) { } #define i_val int -#define i_valdel int_del // optional func to show elements destroyed -#define i_nonatomic // only for single thread; faster. -#include // define csptr_int shared pointers +#define i_valdel int_del // optional func to show elements destroyed +#include // csptr_int: shared pointer to int -#define i_key_csptr int // refer to csptr_int definition above -#include // define a sorted set of csptr_int - -#define i_val_csptr int -#include +#define i_key_csptr csptr_int +#define i_tag int +#include // csset_int: csset +#define i_val_csptr csptr_int +#define i_tag int +#include // cvec_int: cvec int main() { - c_auto (cvec_int, vec) // declare and init vec, call del at scope exit - c_auto (csset_int, set) // declare and init set, call del at scope exit + c_auto (cvec_int, vec) + c_auto (csset_int, set) // declare and init set, call del at scope exit { cvec_int_push_back(&vec, csptr_int_make(2021)); cvec_int_push_back(&vec, csptr_int_make(2012)); @@ -51,14 +51,3 @@ int main() puts("Done"); } } - -/* output: -vec: 2021 2012 2022 2015 -del: 2022 -vec: 2021 2012 -set: 2015 2021 -Done -del: 2015 -del: 2021 -del: 2012 -*/ diff --git a/examples/sptr_ex.c b/examples/sptr_ex.c index 2464ed79..184acd05 100644 --- a/examples/sptr_ex.c +++ b/examples/sptr_ex.c @@ -33,7 +33,8 @@ void Song_del(Song* s) { #define i_del Song_del #include -#define i_val_csptr song +#define i_val_csptr csptr_song +#define i_tag song #include void example3() @@ -48,9 +49,12 @@ void example3() c_foreach (s, cvec_song, v) if (!cstr_equalto(s.ref->get->artist, "Bob Dylan")) - cvec_song_emplace_back(&v2, *s.ref); // calls csptr_song_clone() + cvec_song_emplace_back(&v2, *s.ref); // note: calls csptr_song_clone() - cvec_song_push_back(&v2, csptr_song_make(Song_from("Pink Floyd", "Dogs"))); + c_apply(cvec_song, push_back, &v2, { + csptr_song_make(Song_from("Michael Jackson", "Billie Jean")), + csptr_song_make(Song_from("Rihanna", "Stay")), + }); c_foreach (s, cvec_song, v2) printf("%s - %s: refs %u\n", s.ref->get->artist.str, s.ref->get->title.str, diff --git a/examples/words.c b/examples/words.c index 87327d71..951c3ded 100644 --- a/examples/words.c +++ b/examples/words.c @@ -46,7 +46,7 @@ int main2() std::unordered_map word_map; for (const auto &w : words) { - ++word_map[w]; + word_map[w] += 1; } for (const auto &pair : word_map) { diff --git a/include/stc/template.h b/include/stc/template.h index f0b0a8d7..0faf6950 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -57,28 +57,25 @@ #define i_prefix #endif -#ifndef i_prefix_csptr -#define i_prefix_csptr csptr_ -#endif #ifdef i_key_csptr #ifndef i_tag #define i_tag i_key_csptr #endif - #define i_key c_PASTE(i_prefix_csptr, i_key_csptr) - #define i_cmp c_PASTE3(i_prefix_csptr, i_key_csptr, _compare) - #define i_keydel c_PASTE3(i_prefix_csptr, i_key_csptr, _del) - #define i_keyfrom c_PASTE3(i_prefix_csptr, i_key_csptr, _clone) + #define i_key i_key_csptr + #define i_cmp c_PASTE(i_key_csptr, _compare) + #define i_keydel c_PASTE(i_key_csptr, _del) + #define i_keyfrom c_PASTE(i_key_csptr, _clone) #endif #ifdef i_val_csptr #if !defined i_tag && !defined i_key #define i_tag i_val_csptr #endif - #define i_val c_PASTE(i_prefix_csptr, i_val_csptr) + #define i_val i_val_csptr #ifndef i_key - #define i_cmp c_PASTE3(i_prefix_csptr, i_val_csptr, _compare) + #define i_cmp c_PASTE(i_val_csptr, _compare) #endif - #define i_valdel c_PASTE3(i_prefix_csptr, i_val_csptr, _del) - #define i_valfrom c_PASTE3(i_prefix_csptr, i_val_csptr, _clone) + #define i_valdel c_PASTE(i_val_csptr, _del) + #define i_valfrom c_PASTE(i_val_csptr, _clone) #endif #ifdef i_key_str @@ -190,7 +187,6 @@ #undef i_keyraw #undef i_key_csptr #undef i_val_csptr -#undef i_prefix_csptr #undef i_cnt #undef Self -- cgit v1.2.3