From 049156a523badd68ce0d6eb09fce81e5f002ff2b Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Mon, 2 Nov 2015 16:59:55 -0500 Subject: [PATCH] core/database: Introduce the dbe_alloc() database operation For allocating new database items from a given key. Tracks do not need this function since we have special handling for creation and insertion. Signed-off-by: Anna Schumaker --- core/index.cpp | 14 ++++++++------ core/tags/album.cpp | 7 +++++-- core/tags/artist.cpp | 11 +++++++---- core/tags/genre.cpp | 11 +++++++---- core/tags/library.cpp | 9 +++++++-- include/core/database.h | 3 ++- include/core/database.hpp | 3 ++- include/core/index.h | 8 -------- include/core/tags/album.h | 5 ----- include/core/tags/artist.h | 7 ------- include/core/tags/genre.h | 7 ------- include/core/tags/library.h | 7 ------- tests/core/database.cpp | 12 ++++++++++-- tests/core/index.cpp | 3 ++- tests/core/tags/album.cpp | 11 ++++++++--- tests/core/tags/artist.cpp | 11 ++++++++--- tests/core/tags/genre.cpp | 11 ++++++++--- tests/core/tags/library.cpp | 8 +++++--- 18 files changed, 79 insertions(+), 69 deletions(-) diff --git a/core/index.cpp b/core/index.cpp index 179d8373..a37c6331 100644 --- a/core/index.cpp +++ b/core/index.cpp @@ -9,17 +9,18 @@ index_entry :: index_entry() ie_set = SET_INIT(); } -index_entry :: index_entry(const std::string &key) - : ie_key(key) -{ - ie_set = SET_INIT(); -} - index_entry :: ~index_entry() { set_deinit(&ie_set); } +static struct db_entry *index_alloc(const gchar *key) +{ + struct index_entry *ent = new struct index_entry; + ent->ie_key = key; + return ent; +} + const std::string index_entry :: primary_key() const { return ie_key; @@ -43,6 +44,7 @@ void index_entry :: read(file &file) static const struct db_ops index_ops = { + index_alloc, }; diff --git a/core/tags/album.cpp b/core/tags/album.cpp index 679adf6c..726050db 100644 --- a/core/tags/album.cpp +++ b/core/tags/album.cpp @@ -36,9 +36,11 @@ static void __album_from_key(struct album *album, const gchar *key) album :: album() : al_year(0), al_name("") {} -album :: album(const std::string &key) +static struct db_entry *album_alloc(const gchar *key) { - __album_from_key(this, key.c_str()); + struct album *album = new struct album; + __album_from_key(album, key); + return album; } const std::string album :: primary_key() const @@ -60,6 +62,7 @@ void album :: write(file &file) static const struct db_ops album_ops = { + album_alloc, }; diff --git a/core/tags/artist.cpp b/core/tags/artist.cpp index e47bb70c..aea835ec 100644 --- a/core/tags/artist.cpp +++ b/core/tags/artist.cpp @@ -12,14 +12,16 @@ static database artist_db; artist :: artist() {} -artist :: artist(const std::string &name) +static db_entry *artist_alloc(const gchar *name) { - gchar *lower = string_lowercase(name.c_str()); + struct artist *artist = new struct artist; + gchar *lower = string_lowercase(name); - ar_name = name; - ar_lower = lower; + artist->ar_name = name; + artist->ar_lower = lower; g_free(lower); + return artist; } const std::string artist :: primary_key() const @@ -46,6 +48,7 @@ void artist :: write(file &file) static const struct db_ops artist_ops = { + artist_alloc, }; diff --git a/core/tags/genre.cpp b/core/tags/genre.cpp index a81e5cca..62deee35 100644 --- a/core/tags/genre.cpp +++ b/core/tags/genre.cpp @@ -11,14 +11,16 @@ static database genre_db; genre :: genre() {} -genre :: genre(const std::string &name) +static struct db_entry *genre_alloc(const gchar *name) { - gchar *lower = string_lowercase(name.c_str()); + struct genre *genre = new struct genre; + gchar *lower = string_lowercase(name); - ge_name = name; - ge_lower = lower; + genre->ge_name = name; + genre->ge_lower = lower; g_free(lower); + return genre; } const std::string genre :: primary_key() const @@ -45,6 +47,7 @@ void genre :: write(file &file) static const struct db_ops genre_ops = { + genre_alloc, }; diff --git a/core/tags/library.cpp b/core/tags/library.cpp index 5a5f64e9..7a647d15 100644 --- a/core/tags/library.cpp +++ b/core/tags/library.cpp @@ -12,9 +12,13 @@ library :: library() { } -library :: library(const std::string &path) - : li_size(0), li_enabled(true), li_path(path) +static struct db_entry *library_alloc(const gchar *path) { + struct library *library = new struct library; + library->li_size = 0; + library->li_enabled = true; + library->li_path = path; + return library; } const std::string library :: primary_key() const @@ -42,6 +46,7 @@ void library :: write(file &file) static const struct db_ops library_ops = { + library_alloc, }; diff --git a/include/core/database.h b/include/core/database.h index 265ec5ce..6cb861ca 100644 --- a/include/core/database.h +++ b/include/core/database.h @@ -20,7 +20,6 @@ struct db_entry { unsigned int dbe_index; /* The db_entry's position in the database. */ db_entry(); /**< Initialize _index to 0. */ - db_entry(const std::string); virtual ~db_entry() = 0; /**< Virtual destructor */ /** @@ -52,6 +51,8 @@ struct db_entry { struct db_ops { + /* Allocate a new struct db_entry from a given key. */ + struct db_entry *(*dbe_alloc)(const gchar *); }; diff --git a/include/core/database.hpp b/include/core/database.hpp index b9952dd9..3e3212ec 100644 --- a/include/core/database.hpp +++ b/include/core/database.hpp @@ -167,6 +167,7 @@ T *db_find(struct database *db, const gchar *key) T *dbe = db_get(db, key); if (dbe) return dbe; - return db_insert(db, new T(key)); + dbe = (T *)db->db_ops->dbe_alloc(key); + return db_insert(db, dbe); } #endif /* OCARINA_DATABASE_HPP */ diff --git a/include/core/index.h b/include/core/index.h index ef86e2f3..a63a29ff 100644 --- a/include/core/index.h +++ b/include/core/index.h @@ -21,16 +21,8 @@ struct index_entry : public db_entry { struct set ie_set; index_entry(); /**< Create an empty IndexEntry. */ - - /** - * Create an IndexEntry with a specific key. - * - * @param key The key associated with this IndexEntry. - */ - index_entry(const std::string &); ~index_entry(); - /** * Access the key stored by this IndexEntry. * diff --git a/include/core/tags/album.h b/include/core/tags/album.h index 5b37c1de..13e8f0ad 100644 --- a/include/core/tags/album.h +++ b/include/core/tags/album.h @@ -24,11 +24,6 @@ struct album : public db_entry { album(); /**< Album tag constructor */ - /** - * Album tag constructor - */ - album(const std::string &); - /** * The album's primary key is the concatenation of year * and name, allowing for multiple albums with the same diff --git a/include/core/tags/artist.h b/include/core/tags/artist.h index df8bf91a..fc1f16c7 100644 --- a/include/core/tags/artist.h +++ b/include/core/tags/artist.h @@ -16,13 +16,6 @@ struct artist : public db_entry { artist(); /**< Artist tag constructor. */ - /** - * Artist tag constructor - * - * @param name Artist name. - */ - artist(const std::string &); - const std::string primary_key() const; void read(file &); void write(file &); diff --git a/include/core/tags/genre.h b/include/core/tags/genre.h index f0e16b05..917b4512 100644 --- a/include/core/tags/genre.h +++ b/include/core/tags/genre.h @@ -17,13 +17,6 @@ public: genre(); /**< Genre tag constructor. */ - /** - * Genre tag constructor. - * - * @param name Genre name. - */ - genre(const std::string &); - const std::string primary_key() const; void read(file &); void write(file &); diff --git a/include/core/tags/library.h b/include/core/tags/library.h index 5b416f5e..674d36de 100644 --- a/include/core/tags/library.h +++ b/include/core/tags/library.h @@ -24,13 +24,6 @@ struct library : public db_entry { library(); /**< Library tag constructor. */ - /** - * Library tag constructor. - * - * @param path Path to the library directory on disk. - */ - library(const std::string &); - /** * Called to access the library tag's primary key. * diff --git a/tests/core/database.cpp b/tests/core/database.cpp index 1c796d73..c3f138ed 100644 --- a/tests/core/database.cpp +++ b/tests/core/database.cpp @@ -17,7 +17,6 @@ struct int_entry : public db_entry { int_entry() : ie_val(0) {}; int_entry(unsigned int v) : ie_val(v) {}; - int_entry(const std::string &k) { sscanf(k.c_str(), "%u", &ie_val); } const std::string primary_key() const { @@ -31,16 +30,25 @@ struct int_entry : public db_entry { void read(file &f) { file_readf(&f, "%u", &ie_val); } }; +static struct db_entry *int_alloc(const gchar *key) +{ + struct int_entry *ent = new int_entry; + sscanf(key, "%u", &ent->ie_val); + return ent; +} + static const struct db_ops int_ops = { + int_alloc, }; static void test_db_entry() { - struct int_entry *ent = new struct int_entry(1); + struct int_entry *ent; struct file f; + ent = (struct int_entry *)int_ops.dbe_alloc("1"); test_equal(ent->dbe_index, 0); test_equal(ent->ie_val, 1); test_equal(ent->primary_key(), "1"); diff --git a/tests/core/index.cpp b/tests/core/index.cpp index 5c783de5..9ea6290e 100644 --- a/tests/core/index.cpp +++ b/tests/core/index.cpp @@ -8,11 +8,12 @@ static void test_entry() { - index_entry *ie = new index_entry("Link"); struct set_iter it; + index_entry *ie; unsigned int i; struct file f; + ie = (index_entry *)test_index_ops()->dbe_alloc("Link"); test_equal(ie->primary_key(), "Link"); set_insert(&ie->ie_set, 0); set_insert(&ie->ie_set, 1); diff --git a/tests/core/tags/album.cpp b/tests/core/tags/album.cpp index 80a32ed9..430ff106 100644 --- a/tests/core/tags/album.cpp +++ b/tests/core/tags/album.cpp @@ -22,9 +22,11 @@ static void test_verify_hyrule(struct album *album) static void test_album() { - struct album *album = new struct album("1998 Hyrule Symphony"); + const struct db_ops *album_ops = test_album_ops(); + struct album *album; file f; + album = (struct album *)album_ops->dbe_alloc("1998 Hyrule Symphony"); test_verify_hyrule(album); file_init(&f, "album_tag", 0); @@ -49,8 +51,11 @@ static void test_album() static void test_album_compare() { - struct album *twilight = new struct album("2006 Twilight Princess"); - struct album *skyward = new struct album("2011 skyward sword"); + const struct db_ops *album_ops = test_album_ops(); + struct album *twilight, *skyward; + + twilight = (struct album *)album_ops->dbe_alloc("2006 Twilight Princess"); + skyward = (struct album *)album_ops->dbe_alloc("2011 skyward sword"); test_equal(album_compare(twilight, twilight), 0); test_equal(album_compare(twilight, skyward), 1); diff --git a/tests/core/tags/artist.cpp b/tests/core/tags/artist.cpp index 243c704a..ad822a1f 100644 --- a/tests/core/tags/artist.cpp +++ b/tests/core/tags/artist.cpp @@ -20,10 +20,12 @@ static void test_verify_koji(struct artist *artist) static void test_artist() { - struct artist *artist = new struct artist("Koji Kondo"); + const struct db_ops *artist_ops = test_artist_ops(); + struct artist *artist; unsigned int i; file f; + artist = (struct artist *)artist_ops->dbe_alloc("Koji Kondo"); test_verify_koji(artist); file_init(&f, "artist_tag", 0); @@ -50,8 +52,11 @@ static void test_artist() static void test_artist_compare() { - struct artist *koji = new struct artist("Koji Kondo"); - struct artist *hajime = new struct artist("hajime wakai"); + const struct db_ops *artist_ops = test_artist_ops(); + struct artist *koji, *hajime; + + koji = (struct artist *)artist_ops->dbe_alloc("Koji Kondo"); + hajime = (struct artist *)artist_ops->dbe_alloc("hajime wakai"); test_equal(artist_compare(koji, koji), 0); test_equal(artist_compare(koji, hajime), 1); diff --git a/tests/core/tags/genre.cpp b/tests/core/tags/genre.cpp index 20fcc90f..412074d8 100644 --- a/tests/core/tags/genre.cpp +++ b/tests/core/tags/genre.cpp @@ -20,10 +20,12 @@ static void test_verify_vg(struct genre *genre) static void test_genre() { - struct genre *genre = new struct genre("Video Game Music"); + const struct db_ops *genre_ops = test_genre_ops(); + struct genre *genre; unsigned int i; file f; + genre = (struct genre *)genre_ops->dbe_alloc("Video Game Music"); test_verify_vg(genre); file_init(&f, "genre_tag", 0); @@ -50,8 +52,11 @@ static void test_genre() static void test_genre_compare() { - struct genre *video = new struct genre("Video Game Music"); - struct genre *game = new struct genre("game music"); + const struct db_ops *genre_ops = test_genre_ops(); + struct genre *video, *game; + + video = (struct genre *)genre_ops->dbe_alloc("Video Game Music"); + game = (struct genre *)genre_ops->dbe_alloc("game music"); test_equal(genre_compare(video, video), 0); test_equal(genre_compare(video, game), 1); diff --git a/tests/core/tags/library.cpp b/tests/core/tags/library.cpp index 197aa60b..e8dbaf76 100644 --- a/tests/core/tags/library.cpp +++ b/tests/core/tags/library.cpp @@ -27,11 +27,13 @@ static void test_verify_link(struct library *library) static void test_library() { - struct library *link = new struct library("/home/Link/Music"); - struct library *zelda = new struct library("/home/Zelda/Music"); - struct library *library = new struct library(); + const struct db_ops *library_ops = test_library_ops(); + struct library *link, *zelda, *library = new struct library(); file f; + link = (struct library *)library_ops->dbe_alloc("/home/Link/Music"); + zelda = (struct library *)library_ops->dbe_alloc("/home/Zelda/Music"); + library_set_enabled(link, false); test_verify_link(link); test_verify_zelda(zelda);