core/database: Move db_at() out of the database struct

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-20 08:19:47 -04:00
parent 929279259d
commit f2f23cb225
8 changed files with 23 additions and 27 deletions

View File

@ -63,5 +63,5 @@ Album *tags :: get_album(const std::string &name, unsigned int year)
Album *tags ::get_album(const unsigned int index)
{
return album_db.at(index);
return db_at(&album_db, index);
}

View File

@ -30,5 +30,5 @@ Artist *tags :: get_artist(const std::string &name)
Artist *tags :: get_artist(const unsigned int index)
{
return artist_db.at(index);
return db_at(&artist_db, index);
}

View File

@ -30,5 +30,5 @@ Genre *tags :: get_genre(const std::string &name)
Genre *tags :: get_genre(const unsigned int index)
{
return genre_db.at(index);
return db_at(&genre_db, index);
}

View File

@ -82,7 +82,7 @@ Library *tags :: get_library(const std::string &path)
Library *tags :: get_library(const unsigned int index)
{
return library_db.at(index);
return db_at(&library_db, index);
}
void tags :: remove_library(Library *library)

View File

@ -162,7 +162,7 @@ Track *tags :: add_track(Album *album, Artist *artist, Genre *genre,
Track *tags :: get_track(const unsigned int index)
{
return track_db.at(index);
return db_at(&track_db, index);
}
void tags :: remove_track(Track *track)

View File

@ -124,15 +124,6 @@ struct database {
*/
iterator next(iterator &);
/**
* Returns the database item at the requested index.
*
* @param index The database index to access.
* @return A pointer to the requested DatabaseEntry.
* @return NULL if there is no valid DatabaseEntry at the requested index.
*/
T *at(unsigned int);
/**
* Find a DatabaseItem with a specific primary key.
*
@ -172,6 +163,11 @@ T *db_insert(struct database<T> *, T *);
template <class T>
void db_remove(struct database<T> *, T *);
/* Returns the database item at the requested index. */
template <class T>
T *db_at(struct database<T> *, unsigned int);
#include "database.hpp"
#endif /* OCARINA_CORE_DATABASE_H */

View File

@ -98,7 +98,7 @@ void db_remove(struct database<T> *db, T *item)
{
if (item == NULL)
return;
if (db->at(item->index()) != item)
if (db_at(db, item->index()) != item)
return;
db->db_entries[item->index()] = NULL;
db->db_keys.erase(item->primary_key());
@ -141,11 +141,11 @@ typename database<T>::iterator database<T> :: next(iterator &it)
}
template <class T>
T *database<T> :: at(unsigned int index)
T *db_at(struct database<T> *db, unsigned int index)
{
if (index >= db_actual_size(this))
if (index >= db_actual_size(db))
return NULL;
return db_entries[index];
return db->db_entries[index];
}
template <class T>

View File

@ -91,14 +91,14 @@ static void test_stress(unsigned int N)
test_equal(db.db_size, N);
test_equal(db_actual_size(&db), N);
/* database.at() */
/* db_at() */
for (i = 0; i < N; i++) {
dbe = db.at(i);
dbe = db_at(&db, i);
test_loop_not_equal(dbe, NULL, i);
test_loop_equal(dbe, ptrs.at(i), i);
test_loop_equal(dbe->ie_val, i, i);
} test_loop_passed();
test_equal(db.at(N), NULL);
test_equal(db_at(&db, N), NULL);
/* database.find() */
for (i = 0; i < N; i++) {
@ -119,7 +119,7 @@ static void test_stress(unsigned int N)
key = g_strdup_printf("%u", i);
dbe = db.find(key);
db_remove(&db, dbe);
test_loop_equal(db.at(i), NULL, i);
test_loop_equal(db_at(&db, i), NULL, i);
test_loop_equal(db.find(key), NULL, i);
g_free(key);
} test_loop_passed();
@ -158,20 +158,20 @@ static void test_save_load()
test_equal(db2.db_size, N);
test_equal(db_actual_size(&db2), N);
for (i = 0; i < N; i++) {
dbe = db2.at(i);
dbe = db_at(&db2, i);
test_loop_not_equal(dbe, NULL, i);
test_loop_equal(dbe->ie_val, i, i);
} test_loop_passed();
/* Removing 5 items, should also trigger autosaving. */
for (i = 0; i < N; i += 2)
db_remove(&db1, db1.at(i));
db_remove(&db1, db_at(&db1, i));
db2 = database<struct int_entry>("save_load.db", false);
db_load(&db2);
test_equal(db2.db_size, N / 2);
for (i = 1; i < N; i++) {
dbe = db2.at(i);
dbe = db_at(&db2, i);
if ((i % 2) == 0) {
test_loop_equal(dbe, NULL, i);
} else {
@ -184,7 +184,7 @@ static void test_save_load()
for (i = N; i < (2 * N); i++)
db_insert(&db2, new int_entry(i));
for (i = N; i < (2 * N); i += 2)
db_remove(&db2, db2.at(i));
db_remove(&db2, db_at(&db2, i));
db1 = database<struct int_entry>("save_load.db", false);
db_load(&db1);
@ -197,7 +197,7 @@ static void test_save_load()
test_equal(db1.db_size, N);
test_equal(db_actual_size(&db1), 2 * N);
for (i = 1; i < (2 * N); i++) {
dbe = db1.at(i);
dbe = db_at(&db1, i);
if ((i % 2) == 0) {
test_loop_equal(dbe, NULL, i);
} else {