From 086652dd505285695418f46d83d18a82679ace37 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 26 Oct 2016 18:47:19 +0200 Subject: [PATCH] Instance: add GetDatabaseOrThrow() --- src/Instance.cxx | 10 ++++++++++ src/Instance.hxx | 8 ++++++++ src/Partition.cxx | 6 ++++++ src/Partition.hxx | 3 +++ src/client/Client.cxx | 6 ++++++ src/client/Client.hxx | 6 ++++++ src/command/DatabaseCommands.cxx | 6 ++---- src/command/PlaylistCommands.cxx | 6 ++---- src/command/StickerCommands.cxx | 22 ++++++++++------------ src/db/Count.cxx | 8 +++----- src/db/DatabasePrint.cxx | 18 +++++++----------- src/db/DatabaseQueue.cxx | 6 ++---- 12 files changed, 65 insertions(+), 40 deletions(-) diff --git a/src/Instance.cxx b/src/Instance.cxx index ab47105dd..c7ceec229 100644 --- a/src/Instance.cxx +++ b/src/Instance.cxx @@ -44,6 +44,16 @@ Instance::GetDatabase(Error &error) return database; } +const Database & +Instance::GetDatabaseOrThrow() const +{ + if (database == nullptr) + throw DatabaseError(DatabaseErrorCode::DISABLED, + "No database"); + + return *database; +} + void Instance::OnDatabaseModified() { diff --git a/src/Instance.hxx b/src/Instance.hxx index 765f588d1..f5cada618 100644 --- a/src/Instance.hxx +++ b/src/Instance.hxx @@ -110,6 +110,14 @@ struct Instance final * music_directory was configured). */ Database *GetDatabase(Error &error); + + /** + * Returns the global #Database instance. Throws + * DatabaseError if this MPD configuration has no database (no + * music_directory was configured). + */ + gcc_pure + const Database &GetDatabaseOrThrow() const; #endif private: diff --git a/src/Partition.cxx b/src/Partition.cxx index 131c0c4a5..c5df91e69 100644 --- a/src/Partition.cxx +++ b/src/Partition.cxx @@ -50,6 +50,12 @@ Partition::GetDatabase(Error &error) const return instance.GetDatabase(error); } +const Database & +Partition::GetDatabaseOrThrow() const +{ + return instance.GetDatabaseOrThrow(); +} + void Partition::DatabaseModified(const Database &db) { diff --git a/src/Partition.hxx b/src/Partition.hxx index d5a619e08..ec9478f9c 100644 --- a/src/Partition.hxx +++ b/src/Partition.hxx @@ -185,6 +185,9 @@ struct Partition final : QueueListener, PlayerListener, MixerListener { */ const Database *GetDatabase(Error &error) const; + gcc_pure + const Database &GetDatabaseOrThrow() const; + /** * The database has been modified. Propagate the change to * all subsystems. diff --git a/src/client/Client.cxx b/src/client/Client.cxx index cab98573e..d9e12fcfa 100644 --- a/src/client/Client.cxx +++ b/src/client/Client.cxx @@ -33,6 +33,12 @@ Client::GetDatabase(Error &error) const return partition.instance.GetDatabase(error); } +const Database & +Client::GetDatabaseOrThrow() const +{ + return partition.instance.GetDatabaseOrThrow(); +} + const Storage * Client::GetStorage() const { diff --git a/src/client/Client.hxx b/src/client/Client.hxx index 2d267b6d5..712824108 100644 --- a/src/client/Client.hxx +++ b/src/client/Client.hxx @@ -187,6 +187,12 @@ public: gcc_pure const Database *GetDatabase(Error &error) const; + /** + * Wrapper for Instance::GetDatabaseOrThrow(). + */ + gcc_pure + const Database &GetDatabaseOrThrow() const; + gcc_pure const Storage *GetStorage() const; diff --git a/src/command/DatabaseCommands.cxx b/src/command/DatabaseCommands.cxx index ae5ef48f5..ed50517f6 100644 --- a/src/command/DatabaseCommands.cxx +++ b/src/command/DatabaseCommands.cxx @@ -143,11 +143,9 @@ handle_searchaddpl(Client &client, Request args, Response &r) } Error error; - const Database *db = client.GetDatabase(error); - if (db == nullptr) - return print_error(r, error); + const Database &db = client.GetDatabaseOrThrow(); - return search_add_to_playlist(*db, *client.GetStorage(), + return search_add_to_playlist(db, *client.GetStorage(), "", playlist, &filter, error) ? CommandResult::OK : print_error(r, error); diff --git a/src/command/PlaylistCommands.cxx b/src/command/PlaylistCommands.cxx index 823fc534a..16b3d6e61 100644 --- a/src/command/PlaylistCommands.cxx +++ b/src/command/PlaylistCommands.cxx @@ -168,11 +168,9 @@ handle_playlistadd(Client &client, Request args, Response &r) success = spl_append_uri(playlist, loader, uri, error); } else { #ifdef ENABLE_DATABASE - const Database *db = client.GetDatabase(error); - if (db == nullptr) - return print_error(r, error); + const Database &db = client.GetDatabaseOrThrow(); - success = search_add_to_playlist(*db, *client.GetStorage(), + success = search_add_to_playlist(db, *client.GetStorage(), uri, playlist, nullptr, error); #else diff --git a/src/command/StickerCommands.cxx b/src/command/StickerCommands.cxx index 5c4de3292..6c5553d9d 100644 --- a/src/command/StickerCommands.cxx +++ b/src/command/StickerCommands.cxx @@ -54,19 +54,17 @@ static CommandResult handle_sticker_song(Response &r, Partition &partition, Request args) { Error error; - const Database *db = partition.GetDatabase(error); - if (db == nullptr) - return print_error(r, error); + const Database &db = partition.GetDatabaseOrThrow(); const char *const cmd = args.front(); /* get song song_id key */ if (args.size == 4 && StringIsEqual(cmd, "get")) { - const LightSong *song = db->GetSong(args[2]); + const LightSong *song = db.GetSong(args[2]); const auto value = sticker_song_get_value(*song, args[3], error); - db->ReturnSong(song); + db.ReturnSong(song); if (value.empty()) { if (error.IsDefined()) return print_error(r, error); @@ -80,11 +78,11 @@ handle_sticker_song(Response &r, Partition &partition, Request args) return CommandResult::OK; /* list song song_id */ } else if (args.size == 3 && StringIsEqual(cmd, "list")) { - const LightSong *song = db->GetSong(args[2]); + const LightSong *song = db.GetSong(args[2]); assert(song != nullptr); Sticker *sticker = sticker_song_get(*song, error); - db->ReturnSong(song); + db.ReturnSong(song); if (sticker) { sticker_print(r, *sticker); sticker_free(sticker); @@ -94,12 +92,12 @@ handle_sticker_song(Response &r, Partition &partition, Request args) return CommandResult::OK; /* set song song_id id key */ } else if (args.size == 5 && StringIsEqual(cmd, "set")) { - const LightSong *song = db->GetSong(args[2]); + const LightSong *song = db.GetSong(args[2]); assert(song != nullptr); bool ret = sticker_song_set_value(*song, args[3], args[4], error); - db->ReturnSong(song); + db.ReturnSong(song); if (!ret) { if (error.IsDefined()) return print_error(r, error); @@ -113,13 +111,13 @@ handle_sticker_song(Response &r, Partition &partition, Request args) /* delete song song_id [key] */ } else if ((args.size == 3 || args.size == 4) && StringIsEqual(cmd, "delete")) { - const LightSong *song = db->GetSong(args[2]); + const LightSong *song = db.GetSong(args[2]); assert(song != nullptr); bool ret = args.size == 3 ? sticker_song_delete(*song, error) : sticker_song_delete_value(*song, args[3], error); - db->ReturnSong(song); + db.ReturnSong(song); if (!ret) { if (error.IsDefined()) return print_error(r, error); @@ -163,7 +161,7 @@ handle_sticker_song(Response &r, Partition &partition, Request args) args[3], }; - if (!sticker_song_find(*db, base_uri, data.name, + if (!sticker_song_find(db, base_uri, data.name, op, value, sticker_song_find_print_cb, &data, error)) { diff --git a/src/db/Count.cxx b/src/db/Count.cxx index c1a861d87..1bd75448b 100644 --- a/src/db/Count.cxx +++ b/src/db/Count.cxx @@ -113,9 +113,7 @@ PrintSongCount(Response &r, const Partition &partition, const char *name, TagType group, Error &error) { - const Database *db = partition.GetDatabase(error); - if (db == nullptr) - return false; + const Database &db = partition.GetDatabaseOrThrow(); const DatabaseSelection selection(name, true, filter); @@ -127,7 +125,7 @@ PrintSongCount(Response &r, const Partition &partition, const char *name, using namespace std::placeholders; const auto f = std::bind(stats_visitor_song, std::ref(stats), _1); - if (!db->Visit(selection, f, error)) + if (!db.Visit(selection, f, error)) return false; PrintSearchStats(r, stats); @@ -140,7 +138,7 @@ PrintSongCount(Response &r, const Partition &partition, const char *name, using namespace std::placeholders; const auto f = std::bind(GroupCountVisitor, std::ref(map), group, _1); - if (!db->Visit(selection, f, error)) + if (!db.Visit(selection, f, error)) return false; Print(r, group, map); diff --git a/src/db/DatabasePrint.cxx b/src/db/DatabasePrint.cxx index e0c623005..39b645660 100644 --- a/src/db/DatabasePrint.cxx +++ b/src/db/DatabasePrint.cxx @@ -155,9 +155,7 @@ db_selection_print(Response &r, Partition &partition, unsigned window_start, unsigned window_end, Error &error) { - const Database *db = partition.GetDatabase(error); - if (db == nullptr) - return false; + const Database &db = partition.GetDatabaseOrThrow(); unsigned i = 0; @@ -182,7 +180,7 @@ db_selection_print(Response &r, Partition &partition, return !in_window || s(song, error2); }; - return db->Visit(selection, d, s, p, error); + return db.Visit(selection, d, s, p, error); } bool @@ -226,9 +224,7 @@ PrintUniqueTags(Response &r, Partition &partition, const SongFilter *filter, Error &error) { - const Database *db = partition.GetDatabase(error); - if (db == nullptr) - return false; + const Database &db = partition.GetDatabaseOrThrow(); const DatabaseSelection selection("", true, filter); @@ -236,15 +232,15 @@ PrintUniqueTags(Response &r, Partition &partition, using namespace std::placeholders; const auto f = std::bind(PrintSongURIVisitor, std::ref(r), std::ref(partition), _1); - return db->Visit(selection, f, error); + return db.Visit(selection, f, error); } else { assert(type < TAG_NUM_OF_ITEM_TYPES); using namespace std::placeholders; const auto f = std::bind(PrintUniqueTag, std::ref(r), (TagType)type, _1); - return db->VisitUniqueTags(selection, (TagType)type, - group_mask, - f, error); + return db.VisitUniqueTags(selection, (TagType)type, + group_mask, + f, error); } } diff --git a/src/db/DatabaseQueue.cxx b/src/db/DatabaseQueue.cxx index 2dac3d5cd..07869c032 100644 --- a/src/db/DatabaseQueue.cxx +++ b/src/db/DatabaseQueue.cxx @@ -41,11 +41,9 @@ bool AddFromDatabase(Partition &partition, const DatabaseSelection &selection, Error &error) { - const Database *db = partition.instance.GetDatabase(error); - if (db == nullptr) - return false; + const Database &db = partition.instance.GetDatabaseOrThrow(); using namespace std::placeholders; const auto f = std::bind(AddToQueue, std::ref(partition), _1); - return db->Visit(selection, f, error); + return db.Visit(selection, f, error); }