diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/music_arc.c | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/examples/music_arc.c b/examples/music_arc.c index 02470b0f..6f9c1c72 100644 --- a/examples/music_arc.c +++ b/examples/music_arc.c @@ -19,12 +19,14 @@ void Song_drop(Song* s) { c_drop(cstr, &s->artist, &s->title); } +// Define the reference counted type #define i_type SongArc #define i_val Song #define i_valdrop Song_drop #define i_cmp Song_cmp #include <stc/carc.h> +// ... and a vector of it #define i_type SongVec #define i_val_arcbox SongArc #include <stc/cvec.h> @@ -39,13 +41,18 @@ void example3() Song_from("Thalia", "Entre El Mar y Una Estrella") }) SongVec_emplace(&vec, *v); + // Share all entries in vec with vec2, except Bob Dylan. c_foreach (s, SongVec, vec) if (!cstr_equals(&s.ref->get->artist, "Bob Dylan")) - SongVec_push_back(&vec2, SongArc_clone(*s.ref)); + SongVec_push(&vec2, SongArc_clone(*s.ref)); + // Add a few more to vec2. We can use emplace when creating new entries SongVec_emplace(&vec2, Song_from("Michael Jackson", "Billie Jean")); SongVec_emplace(&vec2, Song_from("Rihanna", "Stay")); + // If we use push, we would need to construct the Arc explicitly (as in c++, make_shared): + // SongVec_push(&vec2, SongArc_from(Song_from("Rihanna", "Stay"))); + // We now have two vectors with some shared, some unique entries. c_forarray (SongVec, v, {vec, vec2}) { puts("VEC:"); c_foreach (s, SongVec, *v) @@ -53,7 +60,7 @@ void example3() cstr_str(&s.ref->get->title), *s.ref->use_count); } - } + } // because the shared elem. are ref. counted, they are only dropped once here. } int main() |
