From 9e3e3e514c9d24aa4f90aee1b1c3244ea4984161 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 12 Nov 2015 08:41:46 -0500 Subject: [PATCH] core/tags/track: Replace constructor with a backwards pointer Signed-off-by: Anna Schumaker --- core/audio.cpp | 2 +- core/playlist.cpp | 6 ++-- core/queue.cpp | 2 +- core/tags/track.cpp | 28 ++++++++---------- gui/queue/model.cpp | 2 +- gui/queue/window.cpp | 2 +- include/core/tags/track.h | 6 ++-- tests/core/audio.cpp | 2 +- tests/core/deck.cpp | 20 ++++++------- tests/core/queue.cpp | 20 ++++++------- tests/core/tags/track.cpp | 60 ++++++++++++++------------------------- 11 files changed, 64 insertions(+), 86 deletions(-) diff --git a/core/audio.cpp b/core/audio.cpp index bbcd25c5..4c2ea5d4 100644 --- a/core/audio.cpp +++ b/core/audio.cpp @@ -18,7 +18,7 @@ static AudioDriver *cur_driver = NULL; static void save_state() { file_open(&f_cur_track, OPEN_WRITE); - file_writef(&f_cur_track, "%u\n", cur_track->dbe_index); + file_writef(&f_cur_track, "%u\n", cur_track->tr_dbe.dbe_index); file_close(&f_cur_track); } diff --git a/core/playlist.cpp b/core/playlist.cpp index 1adecd10..6fd06514 100644 --- a/core/playlist.cpp +++ b/core/playlist.cpp @@ -94,7 +94,7 @@ void playlist :: init() bool playlist :: has(struct track *track, const std::string &name) { - return index_has(&playlist_db, name.c_str(), track->dbe_index); + return index_has(&playlist_db, name.c_str(), track->tr_dbe.dbe_index); } void playlist :: add(struct track *track, const std::string &name) @@ -103,7 +103,7 @@ void playlist :: add(struct track *track, const std::string &name) return; if (!has(track, name)) { - index_insert(&playlist_db, name.c_str(), track->dbe_index); + index_insert(&playlist_db, name.c_str(), track->tr_dbe.dbe_index); if (cur_plist == name) playlist_q.add(track); if (name == "Banned") @@ -113,7 +113,7 @@ void playlist :: add(struct track *track, const std::string &name) void playlist :: del(struct track *track, const std::string &name) { - index_remove(&playlist_db, name.c_str(), track->dbe_index); + index_remove(&playlist_db, name.c_str(), track->tr_dbe.dbe_index); if (cur_plist == name) playlist_q.del(track); if (name == "Banned") diff --git a/core/queue.cpp b/core/queue.cpp index 4070ee40..571a015c 100644 --- a/core/queue.cpp +++ b/core/queue.cpp @@ -35,7 +35,7 @@ void Queue :: write(file &file) { file_writef(&file, "%u %zu", _flags, _tracks.size()); for (unsigned int i = 0; i < _tracks.size(); i++) - file_writef(&file, " %u", _tracks[i]->dbe_index); + file_writef(&file, " %u", _tracks[i]->tr_dbe.dbe_index); } void Queue :: read(file &file) diff --git a/core/tags/track.cpp b/core/tags/track.cpp index 22f52b0c..c4115cd7 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -20,11 +20,6 @@ static gchar *__track_key(struct library *library, const std::string &path) return g_strdup_printf("%u/%s", library->li_dbe.dbe_index, path.c_str()); } -track :: track() - : tr_album(NULL), tr_artist(NULL), tr_genre(NULL), tr_library(NULL), - tr_count(0), tr_length(0), tr_track(0) -{} - struct db_entry *track_alloc(const gchar *key) { const TagLib_AudioProperties *audio; @@ -45,6 +40,7 @@ struct db_entry *track_alloc(const gchar *key) } track = new struct track; + dbe_init(&track->tr_dbe, track); tag = taglib_file_tag(file); audio = taglib_file_audioproperties(file); @@ -69,12 +65,12 @@ struct db_entry *track_alloc(const gchar *key) out: g_free(fullpath); g_free(path); - return track; + return track ? &track->tr_dbe : NULL; } static void track_free(struct db_entry *dbe) { - struct track *track = (struct track *)dbe; + struct track *track = TRACK(dbe); if (track->tr_library) track->tr_library->li_size--; @@ -83,18 +79,17 @@ static void track_free(struct db_entry *dbe) static void track_setup(struct db_entry *dbe) { - struct track *track = (struct track *)dbe; + struct track *track = TRACK(dbe); - filter_add(track->tr_lower.c_str(), track->dbe_index); - filter_add(track->tr_artist->ar_lower, track->dbe_index); - filter_add(track->tr_album->al_lower, track->dbe_index); + filter_add(track->tr_lower.c_str(), dbe->dbe_index); + filter_add(track->tr_artist->ar_lower, dbe->dbe_index); + filter_add(track->tr_album->al_lower, dbe->dbe_index); track->tr_library->li_size++; } static gchar *track_key(struct db_entry *dbe) { - struct track *track = (struct track *)dbe; - return __track_key(track->tr_library, track->tr_path); + return __track_key(TRACK(dbe)->tr_library, TRACK(dbe)->tr_path); } static struct db_entry *track_read(struct file *file) @@ -103,6 +98,7 @@ static struct db_entry *track_read(struct file *file) struct track *track = new struct track; gchar *path, *name, *lower; + dbe_init(&track->tr_dbe, track); file_readf(file, "%u %u %u %u %u", &library_id, &artist_id, &album_id, &genre_id, &track->tr_track); date_read(file, &track->tr_date); @@ -122,12 +118,12 @@ static struct db_entry *track_read(struct file *file) track->tr_artist = artist_get(artist_id); track->tr_album = album_get(album_id); track->tr_genre = genre_get(genre_id); - return track; + return &track->tr_dbe; } static void track_write(struct file *file, struct db_entry *dbe) { - struct track *track = (struct track *)dbe; + struct track *track = TRACK(dbe); file_writef(file, "%u %u %u %u %u ", track->tr_library->li_dbe.dbe_index, track->tr_artist->ar_dbe.dbe_index, track->tr_album->al_dbe.dbe_index, @@ -187,7 +183,7 @@ struct track *track_add(struct library *library, const std::string &filepath) void track_remove(struct track *track) { - db_remove(&track_db, track); + db_remove(&track_db, &track->tr_dbe); } void track_remove_all(struct library *library) diff --git a/gui/queue/model.cpp b/gui/queue/model.cpp index 66af0750..d9ebb329 100644 --- a/gui/queue/model.cpp +++ b/gui/queue/model.cpp @@ -61,7 +61,7 @@ unsigned int QueueModel :: iter_to_id(const Gtk::TreeIter &iter) const unsigned int QueueModel::path_to_id(const Gtk::TreePath &path) const { - return _queue->operator[](path[0])->dbe_index; + return _queue->operator[](path[0])->tr_dbe.dbe_index; } diff --git a/gui/queue/window.cpp b/gui/queue/window.cpp index 64465fba..0364c737 100644 --- a/gui/queue/window.cpp +++ b/gui/queue/window.cpp @@ -50,7 +50,7 @@ bool QueueWindow :: filter_ids(const Gtk::TreeIter &iter) return true; id = q_model->iter_to_id(iter); - return set_has(&_q_search_res, _queue->operator[](id)->dbe_index); + return set_has(&_q_search_res, _queue->operator[](id)->tr_dbe.dbe_index); } bool QueueWindow :: on_key_press(GdkEventKey *event) diff --git a/include/core/tags/track.h b/include/core/tags/track.h index 3e1a5151..9967902e 100644 --- a/include/core/tags/track.h +++ b/include/core/tags/track.h @@ -20,7 +20,7 @@ extern "C" { * The Track tag is used to store information about tracks that * have been added to the tag database. */ -struct track : public db_entry { +struct track { struct album *tr_album; /* This track's associated album. */ struct artist *tr_artist; /* This track's associated artist. */ struct genre *tr_genre; /* This track's associated genre. */ @@ -35,10 +35,10 @@ struct track : public db_entry { std::string tr_title; /* This track's title. */ std::string tr_lower; /* This track's title (lowercased). */ - track(); /**< Track constructor. */ + struct db_entry tr_dbe; }; -#define TRACK(dbe) ((struct track *)dbe) +#define TRACK(dbe) ((struct track *)DBE_DATA(dbe)) /* Called to initialize the track database. */ diff --git a/tests/core/audio.cpp b/tests/core/audio.cpp index 2b1e804c..057a8738 100644 --- a/tests/core/audio.cpp +++ b/tests/core/audio.cpp @@ -128,7 +128,7 @@ void test_track_controls() audio :: pause(); audio :: next(); - test_not_equal(audio :: current_track()->dbe_index, (unsigned)2); + test_not_equal(audio :: current_track()->tr_dbe.dbe_index, (unsigned)2); test_equal(driver->is_playing(), false); audio :: play(); diff --git a/tests/core/deck.cpp b/tests/core/deck.cpp index 5b8ce55d..e0bbe0b0 100644 --- a/tests/core/deck.cpp +++ b/tests/core/deck.cpp @@ -32,12 +32,12 @@ static void test_init() it = deck :: get_queues().begin(); test_equal(it->size(), (unsigned)4); for (unsigned int i = 0; i < 4; i++) - test_equal((*it)[i]->dbe_index, i); + test_equal((*it)[i]->tr_dbe.dbe_index, i); it++; test_equal(it->size(), (unsigned)5); for (unsigned int i = 0; i < 5; i++) - test_equal((*it)[i]->dbe_index, i + 4); + test_equal((*it)[i]->tr_dbe.dbe_index, i + 4); /* * Test that we saved the deck in the new format @@ -125,19 +125,19 @@ static void test_next_prev() test_equal(deck :: prev(), TRACK_NULL); for (unsigned int i = 0; i < 2; i++) { - test_equal(deck :: next()->dbe_index, (unsigned)0); - test_equal(deck :: next()->dbe_index, (unsigned)1); - test_equal(deck :: next()->dbe_index, (unsigned)2); - test_equal(deck :: next()->dbe_index, (unsigned)3); + test_equal(deck :: next()->tr_dbe.dbe_index, (unsigned)0); + test_equal(deck :: next()->tr_dbe.dbe_index, (unsigned)1); + test_equal(deck :: next()->tr_dbe.dbe_index, (unsigned)2); + test_equal(deck :: next()->tr_dbe.dbe_index, (unsigned)3); test_equal(q->size(), (unsigned)4); } for (unsigned int i = 0; i < 2; i++) { if (i == 1) - test_equal(deck :: prev()->dbe_index, (unsigned)3); - test_equal(deck :: prev()->dbe_index, (unsigned)2); - test_equal(deck :: prev()->dbe_index, (unsigned)1); - test_equal(deck :: prev()->dbe_index, (unsigned)0); + test_equal(deck :: prev()->tr_dbe.dbe_index, (unsigned)3); + test_equal(deck :: prev()->tr_dbe.dbe_index, (unsigned)2); + test_equal(deck :: prev()->tr_dbe.dbe_index, (unsigned)1); + test_equal(deck :: prev()->tr_dbe.dbe_index, (unsigned)0); } test_equal(deck :: get_queues().size(), (size_t)1); diff --git a/tests/core/queue.cpp b/tests/core/queue.cpp index 59726cf6..272bb59c 100644 --- a/tests/core/queue.cpp +++ b/tests/core/queue.cpp @@ -193,7 +193,7 @@ void test_next() for (unsigned int i = 0; i < 24; i++) { track = q.next(); test_loop_not_equal(track, NULL, i); - test_loop_equal(track->dbe_index, i % 24, i); + test_loop_equal(track->tr_dbe.dbe_index, i % 24, i); } test_loop_passed(); test_equal(q.size(), (unsigned)0); test_equal(q.length(), 0); @@ -207,7 +207,7 @@ void test_next() for (unsigned int i = 0; i < 24; i++) { track = q.next(); test_loop_not_equal(track, NULL, i); - test_loop_equal(track->dbe_index, expected_rand[i], i); + test_loop_equal(track->tr_dbe.dbe_index, expected_rand[i], i); } test_loop_passed(); test_equal(q.size(), (unsigned)0); test_equal(q.length(), 0); @@ -221,7 +221,7 @@ void test_next() for (unsigned int i = 0; i < 48; i++) { track = q.next(); test_loop_not_equal(track, NULL, i); - test_loop_equal(track->dbe_index, i % 24, i); + test_loop_equal(track->tr_dbe.dbe_index, i % 24, i); } test_loop_passed(); test_equal(q.size(), (unsigned)24); } @@ -235,12 +235,12 @@ void test_select() test_equal(q.size(), (unsigned)24); q.track_selected(10); test_equal(q.size(), (unsigned)23); - test_equal(q.next()->dbe_index, (unsigned)11); + test_equal(q.next()->tr_dbe.dbe_index, (unsigned)11); q.set_flag(Q_REPEAT); q.track_selected(0); test_equal(q.size(), (unsigned)22); - test_equal(q.next()->dbe_index, (unsigned)1); + test_equal(q.next()->tr_dbe.dbe_index, (unsigned)1); } @@ -257,18 +257,18 @@ void test_sorting() test_equal(q.get_sorder().size(), (size_t)1); test_fill_q(&q); for (unsigned int i = 0; i < 24; i++) - test_loop_equal(q[i]->dbe_index, exp_sort_title[i], i); + test_loop_equal(q[i]->tr_dbe.dbe_index, exp_sort_title[i], i); test_loop_passed(); q.sort(SORT_TITLE, false); test_equal(q.get_sorder().size(), (size_t)1); for (unsigned int i = 0; i < 24; i++) - test_loop_equal(q[i]->dbe_index, exp_sort_title[23 - i], i); + test_loop_equal(q[i]->tr_dbe.dbe_index, exp_sort_title[23 - i], i); test_loop_passed(); q.sort(SORT_LENGTH, true); for (unsigned int i = 0; i < 24; i++) - test_loop_equal(q[i]->dbe_index, i, i); + test_loop_equal(q[i]->tr_dbe.dbe_index, i, i); test_loop_passed(); q.sort(SORT_YEAR, true); @@ -276,7 +276,7 @@ void test_sorting() q.sort(SORT_TITLE, false); test_equal(q.get_sorder().size(), (size_t)2); for (unsigned int i = 0; i < 24; i++) - test_loop_equal(q[i]->dbe_index, exp_sort_ye_ti[i], i); + test_loop_equal(q[i]->tr_dbe.dbe_index, exp_sort_ye_ti[i], i); test_loop_passed(); } @@ -303,7 +303,7 @@ void test_saving() test_equal(r.length(), q.length()); for (unsigned int i = 0; i < 24; i++) - test_loop_equal(q[i]->dbe_index, r[i]->dbe_index, i); + test_loop_equal(q[i]->tr_dbe.dbe_index, r[i]->tr_dbe.dbe_index, i); test_loop_passed(); } diff --git a/tests/core/tags/track.cpp b/tests/core/tags/track.cpp index 687cb7a3..9b8601da 100644 --- a/tests/core/tags/track.cpp +++ b/tests/core/tags/track.cpp @@ -18,25 +18,6 @@ static struct track *test_alloc(const gchar *key) return TRACK(track_ops->dbe_alloc(key)); } -static void test_verify_empty(struct track *track) -{ - const struct db_ops *track_ops = test_track_ops(); - test_equal(track->tr_album, (struct album *)NULL); - test_equal(track->tr_artist, (struct artist *)NULL); - test_equal(track->tr_genre, (struct genre *)NULL); - test_equal(track->tr_library, (struct library *)NULL); - - test_equal(track->tr_title, ""); - test_equal(track->tr_lower, ""); - test_equal(track_path(track), ""); - test_str_equal(track_ops->dbe_key(track), ""); - test_equal(track_last_play(track), "Never"); - - test_equal(track->tr_track, 0); - test_equal(track->tr_length, 0); - test_equal(track->tr_count, 0); -} - static void test_verify_tags(struct track *track) { test_equal(track->tr_album->al_name, "Hyrule Symphony"); @@ -53,7 +34,7 @@ static void test_verify_track(struct track *track) test_equal(track->tr_title, "Title Theme"); test_equal(track->tr_lower, "title theme"); - test_str_equal(track_ops->dbe_key(track), + test_str_equal(track_ops->dbe_key(&track->tr_dbe), "0/Hyrule Symphony/01 - Title Theme.ogg"); test_equal(track_path(track), "tests/Music/Hyrule Symphony/01 - Title Theme.ogg"); @@ -71,7 +52,7 @@ static void test_verify_notrack(struct track *track) test_equal(track->tr_title, ""); test_equal(track->tr_lower, ""); - test_str_equal(track_ops->dbe_key(track), + test_str_equal(track_ops->dbe_key(&track->tr_dbe), "0/Hyrule Symphony/00 - No Track.ogg"); test_equal(track_path(track), "tests/Music/Hyrule Symphony/00 - No Track.ogg"); @@ -87,14 +68,11 @@ static void test_track() const struct db_ops *track_ops = test_track_ops(); time_t rawtime = time(NULL); struct tm *now = localtime(&rawtime); - struct track *track = new struct track;; struct library *library; + struct track *track; gchar *date; file f; - test_verify_empty(track); - track_ops->dbe_free(track); - std::setlocale(LC_TIME, "C"); filter_init(); tags_init(); @@ -103,31 +81,31 @@ static void test_track() library = library_find("tests/Music"); track = test_alloc("0/Hyrule Symphony/01 - Title Theme.ogg"); - track->dbe_index = 0; - track_ops->dbe_setup(track); + track->tr_dbe.dbe_index = 0; + track_ops->dbe_setup(&track->tr_dbe); test_verify_track(track); test_equal(library->li_size, 1); file_open(&f, OPEN_WRITE); file_writef(&f, "0 0 0 0 0 0 0 0 0 0 \n"); file_writef(&f, "Hyrule Symphony/00 - No Track.ogg\n"); - track_ops->dbe_write(&f, track); + track_ops->dbe_write(&f, &track->tr_dbe); file_close(&f); - track_ops->dbe_free(track); + track_ops->dbe_free(&track->tr_dbe); test_equal(library->li_size, 0); file_open(&f, OPEN_READ); - track = (struct track *)track_ops->dbe_read(&f); - track->dbe_index = 0; - track_ops->dbe_setup(track); + track = TRACK(track_ops->dbe_read(&f)); + track->tr_dbe.dbe_index = 0; + track_ops->dbe_setup(&track->tr_dbe); test_verify_notrack(track); test_equal(library->li_size, 1); - track_ops->dbe_free(track); + track_ops->dbe_free(&track->tr_dbe); - track = (struct track *)track_ops->dbe_read(&f); - track->dbe_index = 0; - track_ops->dbe_setup(track); + track = TRACK(track_ops->dbe_read(&f)); + track->tr_dbe.dbe_index = 0; + track_ops->dbe_setup(&track->tr_dbe); test_verify_track(track); test_equal(library->li_size, 1); file_close(&f); @@ -136,7 +114,7 @@ static void test_track() test_equal(track->tr_count, 1); test_equal(track_last_play(track), date); - track_ops->dbe_free(track); + track_ops->dbe_free(&track->tr_dbe); g_free(date); } @@ -172,8 +150,8 @@ static void test_track_compare() test_equal(track_compare(kokiri, title), -1); test_equal(track_compare(title, kokiri), 1); - track_ops->dbe_free(title); - track_ops->dbe_free(kokiri); + track_ops->dbe_free(&title->tr_dbe); + track_ops->dbe_free(&kokiri->tr_dbe); } static void test_track_db() @@ -212,6 +190,10 @@ static void test_track_db() test_not_equal(track, NULL); test_equal(track_db_get()->db_size, 2); + track = track_add(library, "tests/Music/invalid_track"); + test_equal(track, NULL); + test_equal(track_db_get()->db_size, 2); + track_remove_all(library); test_equal(track_db_get()->db_size, 0);