diff --git a/core/tags/album.cpp b/core/tags/album.cpp index 465adcd4..662584e4 100644 --- a/core/tags/album.cpp +++ b/core/tags/album.cpp @@ -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); } diff --git a/core/tags/artist.cpp b/core/tags/artist.cpp index 2cce05e2..2abff335 100644 --- a/core/tags/artist.cpp +++ b/core/tags/artist.cpp @@ -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); } diff --git a/core/tags/genre.cpp b/core/tags/genre.cpp index 1b5722af..af4ef454 100644 --- a/core/tags/genre.cpp +++ b/core/tags/genre.cpp @@ -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); } diff --git a/core/tags/library.cpp b/core/tags/library.cpp index 9bbe3895..74724f08 100644 --- a/core/tags/library.cpp +++ b/core/tags/library.cpp @@ -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) diff --git a/core/tags/track.cpp b/core/tags/track.cpp index 67dccfc7..0c7e93fb 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -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) diff --git a/include/core/database.h b/include/core/database.h index 2c6dc7b9..38b825a9 100644 --- a/include/core/database.h +++ b/include/core/database.h @@ -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 *); template void db_remove(struct database *, T *); + +/* Returns the database item at the requested index. */ +template +T *db_at(struct database *, unsigned int); + #include "database.hpp" #endif /* OCARINA_CORE_DATABASE_H */ diff --git a/include/core/database.hpp b/include/core/database.hpp index e81c1270..3e2de354 100644 --- a/include/core/database.hpp +++ b/include/core/database.hpp @@ -98,7 +98,7 @@ void db_remove(struct database *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::iterator database :: next(iterator &it) } template -T *database :: at(unsigned int index) +T *db_at(struct database *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 diff --git a/tests/core/database.cpp b/tests/core/database.cpp index 26f59825..3e0d317e 100644 --- a/tests/core/database.cpp +++ b/tests/core/database.cpp @@ -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("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("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 {