summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/music_arc.c
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-03-26 00:27:45 +0100
committerTyge Løvset <[email protected]>2023-03-26 00:27:45 +0100
commiteb85069b669e754836b9d4587ba03d3af1a5e975 (patch)
tree45c9a0d3fe40c59a8b33ae8ecd2e7aa78bef6240 /misc/examples/music_arc.c
parente8be14dfc894eeac859f0287d4d5b4f4745c0585 (diff)
downloadSTC-modified-eb85069b669e754836b9d4587ba03d3af1a5e975.tar.gz
STC-modified-eb85069b669e754836b9d4587ba03d3af1a5e975.zip
development branch for 4.2
Removed uses of c_auto and c_with in documentation examples and code examples. Still using c_defer a few places. Renamed c11/fmt.h to c11/print.h. Some additions in ccommon.h, e.g. c_const_cast(T, x). Improved docs.
Diffstat (limited to 'misc/examples/music_arc.c')
-rw-r--r--misc/examples/music_arc.c58
1 files changed, 27 insertions, 31 deletions
diff --git a/misc/examples/music_arc.c b/misc/examples/music_arc.c
index fcd24beb..3714e1d5 100644
--- a/misc/examples/music_arc.c
+++ b/misc/examples/music_arc.c
@@ -11,8 +11,6 @@ typedef struct
int Song_cmp(const Song* x, const Song* y)
{ return cstr_cmp(&x->title, &y->title); }
-uint64_t Song_hash(const Song* x) { return cstr_hash(&x->title); }
-
Song Song_make(const char* artist, const char* title)
{ return (Song){cstr_from(artist), cstr_from(title)}; }
@@ -21,47 +19,45 @@ void Song_drop(Song* s) {
c_drop(cstr, &s->artist, &s->title);
}
-// Define the reference counted type
+// Define the shared pointer:
#define i_type SongArc
#define i_valclass Song
-//#define i_opt c_no_hash
+#define i_opt c_no_hash // arc require hash fn, disable as we don't need it.
#include <stc/carc.h>
-// ... and a vector of it
+// ... and a vector of them
#define i_type SongVec
-#define i_valboxed SongArc
+#define i_valboxed SongArc // use i_valboxed on carc / cbox instead of i_val
#include <stc/cstack.h>
void example3()
{
- c_auto (SongVec, vec1, vec2)
- {
- vec1 = c_make(SongVec, {
- Song_make("Bob Dylan", "The Times They Are A Changing"),
- Song_make("Aretha Franklin", "Bridge Over Troubled Water"),
- Song_make("Thalia", "Entre El Mar y Una Estrella")
- });
+ SongVec vec1 = c_make(SongVec, {
+ Song_make("Bob Dylan", "The Times They Are A Changing"),
+ Song_make("Aretha Franklin", "Bridge Over Troubled Water"),
+ Song_make("Thalia", "Entre El Mar y Una Estrella")
+ });
- // Share all entries in vec with vec2, except Bob Dylan.
- c_foreach (s, SongVec, vec1)
- if (!cstr_equals(&s.ref->get->artist, "Bob Dylan"))
- SongVec_push(&vec2, SongArc_clone(*s.ref));
+ SongVec vec2 = {0};
+ // Share all entries in vec with vec2, except Bob Dylan.
+ c_foreach (s, SongVec, vec1)
+ if (!cstr_equals(&s.ref->get->artist, "Bob Dylan"))
+ 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_make("Michael Jackson", "Billie Jean"));
- SongVec_emplace(&vec2, Song_make("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_make("Rihanna", "Stay")));
+ // Add a few more to vec2. We can use emplace when creating new entries
+ // Emplace calls SongArc_from() on the argument to create the Arc:
+ SongVec_emplace(&vec2, Song_make("Michael Jackson", "Billie Jean"));
+ SongVec_emplace(&vec2, Song_make("Rihanna", "Stay"));
- // We now have two vectors with some shared, some unique entries.
- c_forlist (i, SongVec, {vec1, vec2}) {
- puts("VEC:");
- c_foreach (s, SongVec, *i.ref)
- printf(" %s - %s, REFS: %ld\n", cstr_str(&s.ref->get->artist),
- cstr_str(&s.ref->get->title),
- *s.ref->use_count);
- }
- } // because the shared elem. are ref. counted, they are only dropped once here.
+ // We now have two vectors with some shared, some unique entries.
+ c_forlist (i, SongVec, {vec1, vec2}) {
+ puts("VEC:");
+ c_foreach (s, SongVec, *i.ref)
+ printf(" %s - %s, REFS: %ld\n", cstr_str(&s.ref->get->artist),
+ cstr_str(&s.ref->get->title),
+ *s.ref->use_count);
+ }
+ c_drop(SongVec, &vec1, &vec2);
}
int main()