summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Lovset <[email protected]>2022-08-15 08:17:01 +0200
committerTyge Lovset <[email protected]>2022-08-15 08:17:01 +0200
commit2cfad29db0fda313873f2b4a809ff60aca897785 (patch)
tree2af00342f8fde6f71876937fabd6852be69b5890 /examples
parentdd6e6a60b8d1af9127f2694efc314e810b71c9b1 (diff)
downloadSTC-modified-2cfad29db0fda313873f2b4a809ff60aca897785.tar.gz
STC-modified-2cfad29db0fda313873f2b4a809ff60aca897785.zip
Improved docs/ex. Fix range bug when container is empty cvec/cdeq.
Diffstat (limited to 'examples')
-rw-r--r--examples/music_arc.c11
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()