From dea46e8d5a714a483aee8e14775eb8b3a69aa4ad Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 28 Oct 2016 23:03:07 +0200 Subject: [PATCH] db/Plugin: migrate from class Error to C++ exceptions --- src/db/DatabaseGlue.cxx | 2 +- src/db/DatabasePlugin.hxx | 6 +-- src/db/plugins/ProxyDatabasePlugin.cxx | 49 ++++++++----------- .../plugins/simple/SimpleDatabasePlugin.cxx | 2 +- .../plugins/simple/SimpleDatabasePlugin.hxx | 3 +- src/db/plugins/upnp/UpnpDatabasePlugin.cxx | 22 ++------- test/DumpDatabase.cxx | 8 +-- 7 files changed, 31 insertions(+), 61 deletions(-) diff --git a/src/db/DatabaseGlue.cxx b/src/db/DatabaseGlue.cxx index 233890a4b..1cb526e49 100644 --- a/src/db/DatabaseGlue.cxx +++ b/src/db/DatabaseGlue.cxx @@ -39,5 +39,5 @@ DatabaseGlobalInit(EventLoop &loop, DatabaseListener &listener, return nullptr; } - return plugin->create(loop, listener, block, error); + return plugin->create(loop, listener, block); } diff --git a/src/db/DatabasePlugin.hxx b/src/db/DatabasePlugin.hxx index 7f8867adb..3b971a42f 100644 --- a/src/db/DatabasePlugin.hxx +++ b/src/db/DatabasePlugin.hxx @@ -27,7 +27,6 @@ #define MPD_DATABASE_PLUGIN_HXX struct ConfigBlock; -class Error; class EventLoop; class DatabaseListener; class Database; @@ -45,10 +44,11 @@ struct DatabasePlugin { /** * Allocates and configures a database. + * + * Throws #std::runtime_error on error. */ Database *(*create)(EventLoop &loop, DatabaseListener &listener, - const ConfigBlock &block, - Error &error); + const ConfigBlock &block); constexpr bool RequireStorage() const { return flags & FLAG_REQUIRE_STORAGE; diff --git a/src/db/plugins/ProxyDatabasePlugin.cxx b/src/db/plugins/ProxyDatabasePlugin.cxx index 57047083c..e378e2c66 100644 --- a/src/db/plugins/ProxyDatabasePlugin.cxx +++ b/src/db/plugins/ProxyDatabasePlugin.cxx @@ -82,9 +82,9 @@ public: class ProxyDatabase final : public Database, SocketMonitor, IdleMonitor { DatabaseListener &listener; - std::string host; - unsigned port; - bool keepalive; + const std::string host; + const unsigned port; + const bool keepalive; struct mpd_connection *connection; @@ -104,14 +104,11 @@ class ProxyDatabase final : public Database, SocketMonitor, IdleMonitor { bool is_idle; public: - ProxyDatabase(EventLoop &_loop, DatabaseListener &_listener) - :Database(proxy_db_plugin), - SocketMonitor(_loop), IdleMonitor(_loop), - listener(_listener) {} + ProxyDatabase(EventLoop &_loop, DatabaseListener &_listener, + const ConfigBlock &block); static Database *Create(EventLoop &loop, DatabaseListener &listener, - const ConfigBlock &block, - Error &error); + const ConfigBlock &block); virtual void Open() override; virtual void Close() override; @@ -141,8 +138,6 @@ public: } private: - bool Configure(const ConfigBlock &block, Error &error); - void Connect(); void CheckConnection(); void EnsureConnected(); @@ -338,27 +333,22 @@ SendConstraints(mpd_connection *connection, const DatabaseSelection &selection) return true; } -Database * -ProxyDatabase::Create(EventLoop &loop, DatabaseListener &listener, - const ConfigBlock &block, Error &error) +ProxyDatabase::ProxyDatabase(EventLoop &_loop, DatabaseListener &_listener, + const ConfigBlock &block) + :Database(proxy_db_plugin), + SocketMonitor(_loop), IdleMonitor(_loop), + listener(_listener), + host(block.GetBlockValue("host", "")), + port(block.GetBlockValue("port", 0u)), + keepalive(block.GetBlockValue("keepalive", false)) { - ProxyDatabase *db = new ProxyDatabase(loop, listener); - if (!db->Configure(block, error)) { - delete db; - db = nullptr; - } - - return db; } -bool -ProxyDatabase::Configure(const ConfigBlock &block, gcc_unused Error &error) +Database * +ProxyDatabase::Create(EventLoop &loop, DatabaseListener &listener, + const ConfigBlock &block) { - host = block.GetBlockValue("host", ""); - port = block.GetBlockValue("port", 0u); - keepalive = block.GetBlockValue("keepalive", false); - - return true; + return new ProxyDatabase(loop, listener, block); } void @@ -395,6 +385,9 @@ ProxyDatabase::Connect() #if LIBMPDCLIENT_CHECK_VERSION(2, 10, 0) mpd_connection_set_keepalive(connection, keepalive); +#else + // suppress -Wunused-private-field + (void)keepalive; #endif idle_received = unsigned(-1); diff --git a/src/db/plugins/simple/SimpleDatabasePlugin.cxx b/src/db/plugins/simple/SimpleDatabasePlugin.cxx index c4f46e51c..768abc57c 100644 --- a/src/db/plugins/simple/SimpleDatabasePlugin.cxx +++ b/src/db/plugins/simple/SimpleDatabasePlugin.cxx @@ -84,7 +84,7 @@ inline SimpleDatabase::SimpleDatabase(AllocatedPath &&_path, Database * SimpleDatabase::Create(gcc_unused EventLoop &loop, gcc_unused DatabaseListener &listener, - const ConfigBlock &block, Error &) + const ConfigBlock &block) { return new SimpleDatabase(block); } diff --git a/src/db/plugins/simple/SimpleDatabasePlugin.hxx b/src/db/plugins/simple/SimpleDatabasePlugin.hxx index 4207d5407..40815f2c1 100644 --- a/src/db/plugins/simple/SimpleDatabasePlugin.hxx +++ b/src/db/plugins/simple/SimpleDatabasePlugin.hxx @@ -73,8 +73,7 @@ class SimpleDatabase : public Database { public: static Database *Create(EventLoop &loop, DatabaseListener &listener, - const ConfigBlock &block, - Error &error); + const ConfigBlock &block); gcc_pure Directory &GetRoot() { diff --git a/src/db/plugins/upnp/UpnpDatabasePlugin.cxx b/src/db/plugins/upnp/UpnpDatabasePlugin.cxx index f8d06238b..684ee4c40 100644 --- a/src/db/plugins/upnp/UpnpDatabasePlugin.cxx +++ b/src/db/plugins/upnp/UpnpDatabasePlugin.cxx @@ -77,8 +77,7 @@ public: UpnpDatabase():Database(upnp_db_plugin) {} static Database *Create(EventLoop &loop, DatabaseListener &listener, - const ConfigBlock &block, - Error &error); + const ConfigBlock &block); virtual void Open() override; virtual void Close() override; @@ -103,9 +102,6 @@ public: return 0; } -protected: - bool Configure(const ConfigBlock &block, Error &error); - private: bool VisitServer(const ContentDirectoryService &server, const std::list &vpath, @@ -150,21 +146,9 @@ private: Database * UpnpDatabase::Create(gcc_unused EventLoop &loop, gcc_unused DatabaseListener &listener, - const ConfigBlock &block, Error &error) + const ConfigBlock &) { - UpnpDatabase *db = new UpnpDatabase(); - if (!db->Configure(block, error)) { - delete db; - return nullptr; - } - - return db; -} - -inline bool -UpnpDatabase::Configure(const ConfigBlock &, Error &) -{ - return true; + return new UpnpDatabase(); } void diff --git a/test/DumpDatabase.cxx b/test/DumpDatabase.cxx index 2edf5d13a..eebf679cc 100644 --- a/test/DumpDatabase.cxx +++ b/test/DumpDatabase.cxx @@ -127,13 +127,7 @@ try { if (path != nullptr) block.AddBlockParam("path", path->value.c_str(), path->line); - Database *db = plugin->create(event_loop, database_listener, - block, error); - - if (db == nullptr) { - cerr << error.GetMessage() << endl; - return EXIT_FAILURE; - } + Database *db = plugin->create(event_loop, database_listener, block); AtScopeExit(db) { delete db; };