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 <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-11-02 10:02:35 -05:00
parent c3dc601f72
commit 48b25945cc
15 changed files with 68 additions and 53 deletions

View File

@ -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();
}

View File

@ -6,8 +6,13 @@
DatabaseEntry :: DatabaseEntry()
: id(0)
: _index(0)
{
}
DatabaseEntry :: ~DatabaseEntry() {}
const unsigned int DatabaseEntry :: index()
{
return _index;
}

View File

@ -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());
}
}

View File

@ -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")

View File

@ -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)

View File

@ -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<Track>::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);

View File

@ -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();
}

View File

@ -10,18 +10,28 @@
#include <map>
#include <vector>
template<class T> 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 <class T> 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

View File

@ -68,7 +68,7 @@ void Database<T> :: 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<T> :: 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;

View File

@ -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;

View File

@ -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();
}

View File

@ -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();

View File

@ -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++;

View File

@ -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);

View File

@ -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();
}