From 48b25945cc00fa4e96ec8c0b52d271f1ef6ff0a8 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sun, 2 Nov 2014 10:02:35 -0500 Subject: [PATCH] DatabaseEntry: Rename id to _index (and make it private) This variable should only be set by the Database when a DatabaseItem is first created. This means I should hide _index from the rest of the world to prevent accidental modifications. I also add an accessor function for other code that needs to read _index. Signed-off-by: Anna Schumaker --- core/audio.cpp | 2 +- core/database.cpp | 7 ++++++- core/library.cpp | 4 ++-- core/playlist.cpp | 6 +++--- core/queue.cpp | 2 +- core/tags.cpp | 22 +++++++++++----------- gui/tabs.cpp | 2 +- include/core/database.h | 16 +++++++++++++--- include/core/database.hpp | 6 +++--- lib/colmgr.cpp | 2 +- lib/model.cpp | 2 +- tests/core/audio.cpp | 2 +- tests/core/database.cpp | 8 ++++---- tests/core/deck.cpp | 20 ++++++++++---------- tests/core/queue.cpp | 20 ++++++++++---------- 15 files changed, 68 insertions(+), 53 deletions(-) diff --git a/core/audio.cpp b/core/audio.cpp index 3ec8f8d6..25332838 100644 --- a/core/audio.cpp +++ b/core/audio.cpp @@ -21,7 +21,7 @@ static File f_cur_track("cur_track", 0); static void save_state() { f_cur_track.open(OPEN_WRITE); - f_cur_track << cur_track->id << std::endl; + f_cur_track << cur_track->index() << std::endl; f_cur_track.close(); } diff --git a/core/database.cpp b/core/database.cpp index c70f8082..544c0095 100644 --- a/core/database.cpp +++ b/core/database.cpp @@ -6,8 +6,13 @@ DatabaseEntry :: DatabaseEntry() - : id(0) + : _index(0) { } DatabaseEntry :: ~DatabaseEntry() {} + +const unsigned int DatabaseEntry :: index() +{ + return _index; +} diff --git a/core/library.cpp b/core/library.cpp index 289c4f3d..2023b244 100644 --- a/core/library.cpp +++ b/core/library.cpp @@ -125,7 +125,7 @@ static void validate_library(Library *&library) if (g_file_test(track->path().c_str(), G_FILE_TEST_EXISTS) == false) { library_q.del(track); - tagdb :: remove_track(track->id); + tagdb :: remove_track(track->index()); } } } @@ -166,7 +166,7 @@ void library :: remove(Library *library) { if (library) { set_enabled(library, false); - tagdb :: remove_library(library->id); + tagdb :: remove_library(library->index()); } } diff --git a/core/playlist.cpp b/core/playlist.cpp index ad7b6df1..15a0b6b3 100644 --- a/core/playlist.cpp +++ b/core/playlist.cpp @@ -58,7 +58,7 @@ bool playlist :: has(Track *track, const std::string &name) if (ent == NULL) return false; - it = ent->values.find(track->id); + it = ent->values.find(track->index()); return it != ent->values.end(); } @@ -68,7 +68,7 @@ void playlist :: add(Track *track, const std::string &name) return; if (!has(track, name)) { - playlist_db.insert(name, track->id); + playlist_db.insert(name, track->index()); if (cur_plist == name) playlist_q.add(track); if (name == "Banned") @@ -78,7 +78,7 @@ void playlist :: add(Track *track, const std::string &name) void playlist :: del(Track *track, const std::string &name) { - playlist_db.remove(name, track->id); + playlist_db.remove(name, track->index()); if (cur_plist == name) playlist_q.del(track); if (name == "Banned") diff --git a/core/queue.cpp b/core/queue.cpp index dcb6ae27..abfd29b0 100644 --- a/core/queue.cpp +++ b/core/queue.cpp @@ -31,7 +31,7 @@ void Queue :: write(File &f) { f << _flags << " " << _tracks.size(); for (unsigned int i = 0; i < _tracks.size(); i++) - f << " " << _tracks[i]->id; + f << " " << _tracks[i]->index(); } void Queue :: read(File &f) diff --git a/core/tags.cpp b/core/tags.cpp index 656f529d..f28b23e5 100644 --- a/core/tags.cpp +++ b/core/tags.cpp @@ -191,17 +191,17 @@ void Track :: read(File &f) album = album_db.at(album_id); genre = genre_db.at(genre_id); - title_lower = filter :: add(title, id); - filter :: add(artist->name, id); - filter :: add(album->name, id); + title_lower = filter :: add(title, index()); + filter :: add(artist->name, index()); + filter :: add(album->name, index()); library->count++; set_length_str(); } void Track :: write(File &f) { - f << library->id << " " << artist->id << " " << album->id << " "; - f << genre->id << " " << track << " "; + f << library->index() << " " << artist->index() << " "; + f << album->index() << " " << genre->index() << " " << track << " "; f << last_year << " " << last_month << " " << last_day << " "; f << play_count << " " << length << " " << title << std::endl; f << filepath << std::endl; @@ -256,11 +256,11 @@ bool Track :: tag() length = audio->length(); title = format_tag(tag->title()); - title_lower = filter :: add(title, id); + title_lower = filter :: add(title, index()); set_length_str(); - filter :: add(artist->name, id); - filter :: add(album->name, id); + filter :: add(artist->name, index()); + filter :: add(album->name, index()); return true; } @@ -370,7 +370,7 @@ Track *tagdb :: add_track(const std::string &filepath, Library *library) { Track *track = track_db.insert(Track(filepath, library)); if (track && !track->tag()) { - remove_track(track->id); + remove_track(track->index()); track = NULL; } return track; @@ -390,8 +390,8 @@ void tagdb :: remove_library(unsigned int library_id) { Database::iterator it; for (it = track_db.begin(); it != track_db.end(); it = track_db.next(it)) { - if ((*it)->library->id == library_id) - track_db.remove((*it)->id); + if ((*it)->library->index() == library_id) + track_db.remove((*it)->index()); } tagdb :: commit(); library_db.remove(library_id); diff --git a/gui/tabs.cpp b/gui/tabs.cpp index db0b5b7c..be1ba519 100644 --- a/gui/tabs.cpp +++ b/gui/tabs.cpp @@ -359,7 +359,7 @@ bool Tab :: on_filter_visible(const Gtk::TreeIter &iter) return true; pq_id = tab_model->iter_to_id(iter); - it = visible_ids.find(tab_pq->operator[](pq_id)->id); + it = visible_ids.find(tab_pq->operator[](pq_id)->index()); return it != visible_ids.end(); } diff --git a/include/core/database.h b/include/core/database.h index a59079c0..963d92b0 100644 --- a/include/core/database.h +++ b/include/core/database.h @@ -10,18 +10,28 @@ #include #include +template class Database; /** * The DatabaseEntry class is the base class for storing * generic data inside a Database. */ class DatabaseEntry { -public: - unsigned int id; /**< The location of this item in the Database */ +private: + template friend class Database; + unsigned int _index; /**< The location of an item in the Database. */ - DatabaseEntry(); /** Initialize id to 0. */ +public: + DatabaseEntry(); /**< Initialize _index to 0. */ virtual ~DatabaseEntry() = 0; /**< Virtual destructor */ + /** + * Called to access a DatabaseEntry's index. + * + * @return DatabaseEntry::_index + */ + const unsigned int index(); + /** * The primary key of a DatabaseEntry is a unique string representing * a single DatabaseEntry instance. This is used for preventing diff --git a/include/core/database.hpp b/include/core/database.hpp index 2a7d7d53..bef56d79 100644 --- a/include/core/database.hpp +++ b/include/core/database.hpp @@ -68,7 +68,7 @@ void Database :: load() _db[i] = NULL; else { _db[i] = new T; - _db[i]->id = i; + _db[i]->_index = i; _db[i]->read(_file); _keys[_db[i]->primary_key()] = i; _size++; @@ -87,10 +87,10 @@ T *Database :: insert(const T &val) return NULL; t = new T(val); + t->_index = actual_size(); _db.push_back(t); - t->id = actual_size() - 1; - _keys[t->primary_key()] = t->id; + _keys[t->primary_key()] = t->index(); _size++; autosave(); return t; diff --git a/lib/colmgr.cpp b/lib/colmgr.cpp index 5eb92377..d52055c3 100644 --- a/lib/colmgr.cpp +++ b/lib/colmgr.cpp @@ -43,7 +43,7 @@ static void list_path(Library *lib) return; row = *(c_list->append()); - row[c_cols.c_id] = lib->id; + row[c_cols.c_id] = lib->index(); row[c_cols.c_enabled] = lib->enabled; row[c_cols.c_size] = lib->count; row[c_cols.c_path] = lib->root_path; diff --git a/lib/model.cpp b/lib/model.cpp index ea1fedb5..60dfb741 100644 --- a/lib/model.cpp +++ b/lib/model.cpp @@ -69,7 +69,7 @@ unsigned int QueueModel :: iter_to_id(const Gtk::TreeIter &iter) unsigned int QueueModel::path_to_id(const Gtk::TreePath &path) { - return queue->operator[](path[0])->id; + return queue->operator[](path[0])->index(); } diff --git a/tests/core/audio.cpp b/tests/core/audio.cpp index 72bc3cc0..7509bdab 100644 --- a/tests/core/audio.cpp +++ b/tests/core/audio.cpp @@ -97,7 +97,7 @@ void test_track_controls() audio :: pause(); audio :: next(); - test_not_equal(audio :: current_track()->id, (unsigned)2); + test_not_equal(audio :: current_track()->index(), (unsigned)2); test_equal(driver->is_playing(), false); audio :: play(); diff --git a/tests/core/database.cpp b/tests/core/database.cpp index a8235b5a..7578230b 100644 --- a/tests/core/database.cpp +++ b/tests/core/database.cpp @@ -86,7 +86,7 @@ static void test_insertion(struct TestArgs *args) for (unsigned int i = 0; i < args->n; i++) { IntEntry *it = args->db->insert(IntEntry(i)); check_not_equal(it, INT_NULL); - check_equal(it->id, i); + check_equal(it->index(), i); check_equal(it->val, i); /* @@ -179,7 +179,7 @@ static void test_iterator(struct TestArgs *args) for (it = args->db->begin(); it != args->db->end(); it = args->db->next(it)) { check_not_equal((*it), INT_NULL); check_equal((*it)->val, index); - check_equal((*it)->id, index); + check_equal((*it)->index(), index); index += 2; }; test :: success(); @@ -226,7 +226,7 @@ static void test_insertion3(struct TestArgs *args) args->size++; args->actual++; - check_equal(it->id, args->n + (i / 2)); + check_equal(it->index(), args->n + (i / 2)); } test :: success(); @@ -262,7 +262,7 @@ static void test_autosave(struct TestArgs *args) check_equal(*it2, INT_NULL); else { check_not_equal(*it2, INT_NULL); - check_equal((*it1)->id, (*it2)->id); + check_equal((*it1)->index(), (*it2)->index()); check_equal((*it1)->val, (*it2)->val); } it2++; diff --git a/tests/core/deck.cpp b/tests/core/deck.cpp index eb96968e..2e248379 100644 --- a/tests/core/deck.cpp +++ b/tests/core/deck.cpp @@ -28,12 +28,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]->id, i); + test_equal((*it)[i]->index(), i); it++; test_equal(it->size(), (unsigned)5); for (unsigned int i = 0; i < 5; i++) - test_equal((*it)[i]->id, i + 4); + test_equal((*it)[i]->index(), i + 4); /* * Test that we saved the deck in the new format @@ -120,19 +120,19 @@ static void test_next_prev() test_equal(deck :: prev(), TRACK_NULL); for (unsigned int i = 0; i < 2; i++) { - test_equal(deck :: next()->id, (unsigned)0); - test_equal(deck :: next()->id, (unsigned)1); - test_equal(deck :: next()->id, (unsigned)2); - test_equal(deck :: next()->id, (unsigned)3); + test_equal(deck :: next()->index(), (unsigned)0); + test_equal(deck :: next()->index(), (unsigned)1); + test_equal(deck :: next()->index(), (unsigned)2); + test_equal(deck :: next()->index(), (unsigned)3); test_equal(q->size(), (unsigned)4); } for (unsigned int i = 0; i < 2; i++) { if (i == 1) - test_equal(deck :: prev()->id, (unsigned)3); - test_equal(deck :: prev()->id, (unsigned)2); - test_equal(deck :: prev()->id, (unsigned)1); - test_equal(deck :: prev()->id, (unsigned)0); + test_equal(deck :: prev()->index(), (unsigned)3); + test_equal(deck :: prev()->index(), (unsigned)2); + test_equal(deck :: prev()->index(), (unsigned)1); + test_equal(deck :: prev()->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 b7bf8402..77bad843 100644 --- a/tests/core/queue.cpp +++ b/tests/core/queue.cpp @@ -247,7 +247,7 @@ void test_next() for (unsigned int i = 0; i < 24; i++) { track = q.next(); check_not_equal(track, TRACK_NULL); - check_equal(track->id, i); + check_equal(track->index(), i); } test :: success(); test_equal(q.size(), (unsigned)0); @@ -263,7 +263,7 @@ void test_next() for (unsigned int i = 0; i < 24; i++) { track = q.next(); check_not_equal(track, TRACK_NULL); - check_equal(track->id, expected_rand[i]); + check_equal(track->index(), expected_rand[i]); } test :: success(); test_equal(q.size(), (unsigned)0); @@ -278,7 +278,7 @@ void test_next() for (unsigned int i = 0; i < 48; i++) { track = q.next(); check_not_equal(track, TRACK_NULL); - check_equal(track->id, i % 24); + check_equal(track->index(), i % 24); } test :: success(); test_equal(q.size(), (unsigned)24); @@ -292,12 +292,12 @@ void test_select() test_equal(q.size(), (unsigned)24); q.track_selected(10); test_equal(q.size(), (unsigned)23); - test_equal(q.next()->id, (unsigned)11); + test_equal(q.next()->index(), (unsigned)11); q.set_flag(Q_REPEAT); q.track_selected(0); test_equal(q.size(), (unsigned)22); - test_equal(q.next()->id, (unsigned)1); + test_equal(q.next()->index(), (unsigned)1); } unsigned int exp_sort_title[] = { 1, 18, 19, 16, 20, 8, 2, 9, 23, 10, 17, 11, @@ -314,20 +314,20 @@ void test_sorting() test_fill_q(&q); test :: begin(); for (unsigned int i = 0; i < 24; i++) - check_equal(q[i]->id, exp_sort_title[i]); + check_equal(q[i]->index(), exp_sort_title[i]); test :: success(); q.sort(SORT_TITLE, false); test_equal(q.get_sorder().size(), (size_t)1); test :: begin(); for (unsigned int i = 0; i < 24; i++) - check_equal(q[i]->id, exp_sort_title[23 - i]); + check_equal(q[i]->index(), exp_sort_title[23 - i]); test :: success(); q.sort(SORT_LENGTH, true); test :: begin(); for (unsigned int i = 0; i < 24; i++) - check_equal(q[i]->id, i); + check_equal(q[i]->index(), i); test :: success(); q.sort(SORT_YEAR, true); @@ -336,7 +336,7 @@ void test_sorting() test_equal(q.get_sorder().size(), (size_t)2); test :: begin(); for (unsigned int i = 0; i < 24; i++) - check_equal(q[i]->id, exp_sort_ye_ti[i]); + check_equal(q[i]->index(), exp_sort_ye_ti[i]); test :: success(); } @@ -367,7 +367,7 @@ void test_saving() test :: begin(); for (unsigned int i = 0; i < q.size(); i++) - check_equal(r[i]->id, q[i]->id); + check_equal(r[i]->index(), q[i]->index()); test :: success(); }