db/Plugin: migrate from class Error to C++ exceptions

This commit is contained in:
Max Kellermann 2016-10-28 23:03:07 +02:00
parent 7e5ce623fe
commit dea46e8d5a
7 changed files with 31 additions and 61 deletions

View File

@ -39,5 +39,5 @@ DatabaseGlobalInit(EventLoop &loop, DatabaseListener &listener,
return nullptr; return nullptr;
} }
return plugin->create(loop, listener, block, error); return plugin->create(loop, listener, block);
} }

View File

@ -27,7 +27,6 @@
#define MPD_DATABASE_PLUGIN_HXX #define MPD_DATABASE_PLUGIN_HXX
struct ConfigBlock; struct ConfigBlock;
class Error;
class EventLoop; class EventLoop;
class DatabaseListener; class DatabaseListener;
class Database; class Database;
@ -45,10 +44,11 @@ struct DatabasePlugin {
/** /**
* Allocates and configures a database. * Allocates and configures a database.
*
* Throws #std::runtime_error on error.
*/ */
Database *(*create)(EventLoop &loop, DatabaseListener &listener, Database *(*create)(EventLoop &loop, DatabaseListener &listener,
const ConfigBlock &block, const ConfigBlock &block);
Error &error);
constexpr bool RequireStorage() const { constexpr bool RequireStorage() const {
return flags & FLAG_REQUIRE_STORAGE; return flags & FLAG_REQUIRE_STORAGE;

View File

@ -82,9 +82,9 @@ public:
class ProxyDatabase final : public Database, SocketMonitor, IdleMonitor { class ProxyDatabase final : public Database, SocketMonitor, IdleMonitor {
DatabaseListener &listener; DatabaseListener &listener;
std::string host; const std::string host;
unsigned port; const unsigned port;
bool keepalive; const bool keepalive;
struct mpd_connection *connection; struct mpd_connection *connection;
@ -104,14 +104,11 @@ class ProxyDatabase final : public Database, SocketMonitor, IdleMonitor {
bool is_idle; bool is_idle;
public: public:
ProxyDatabase(EventLoop &_loop, DatabaseListener &_listener) ProxyDatabase(EventLoop &_loop, DatabaseListener &_listener,
:Database(proxy_db_plugin), const ConfigBlock &block);
SocketMonitor(_loop), IdleMonitor(_loop),
listener(_listener) {}
static Database *Create(EventLoop &loop, DatabaseListener &listener, static Database *Create(EventLoop &loop, DatabaseListener &listener,
const ConfigBlock &block, const ConfigBlock &block);
Error &error);
virtual void Open() override; virtual void Open() override;
virtual void Close() override; virtual void Close() override;
@ -141,8 +138,6 @@ public:
} }
private: private:
bool Configure(const ConfigBlock &block, Error &error);
void Connect(); void Connect();
void CheckConnection(); void CheckConnection();
void EnsureConnected(); void EnsureConnected();
@ -338,27 +333,22 @@ SendConstraints(mpd_connection *connection, const DatabaseSelection &selection)
return true; return true;
} }
Database * ProxyDatabase::ProxyDatabase(EventLoop &_loop, DatabaseListener &_listener,
ProxyDatabase::Create(EventLoop &loop, DatabaseListener &listener, const ConfigBlock &block)
const ConfigBlock &block, Error &error) :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 Database *
ProxyDatabase::Configure(const ConfigBlock &block, gcc_unused Error &error) ProxyDatabase::Create(EventLoop &loop, DatabaseListener &listener,
const ConfigBlock &block)
{ {
host = block.GetBlockValue("host", ""); return new ProxyDatabase(loop, listener, block);
port = block.GetBlockValue("port", 0u);
keepalive = block.GetBlockValue("keepalive", false);
return true;
} }
void void
@ -395,6 +385,9 @@ ProxyDatabase::Connect()
#if LIBMPDCLIENT_CHECK_VERSION(2, 10, 0) #if LIBMPDCLIENT_CHECK_VERSION(2, 10, 0)
mpd_connection_set_keepalive(connection, keepalive); mpd_connection_set_keepalive(connection, keepalive);
#else
// suppress -Wunused-private-field
(void)keepalive;
#endif #endif
idle_received = unsigned(-1); idle_received = unsigned(-1);

View File

@ -84,7 +84,7 @@ inline SimpleDatabase::SimpleDatabase(AllocatedPath &&_path,
Database * Database *
SimpleDatabase::Create(gcc_unused EventLoop &loop, SimpleDatabase::Create(gcc_unused EventLoop &loop,
gcc_unused DatabaseListener &listener, gcc_unused DatabaseListener &listener,
const ConfigBlock &block, Error &) const ConfigBlock &block)
{ {
return new SimpleDatabase(block); return new SimpleDatabase(block);
} }

View File

@ -73,8 +73,7 @@ class SimpleDatabase : public Database {
public: public:
static Database *Create(EventLoop &loop, DatabaseListener &listener, static Database *Create(EventLoop &loop, DatabaseListener &listener,
const ConfigBlock &block, const ConfigBlock &block);
Error &error);
gcc_pure gcc_pure
Directory &GetRoot() { Directory &GetRoot() {

View File

@ -77,8 +77,7 @@ public:
UpnpDatabase():Database(upnp_db_plugin) {} UpnpDatabase():Database(upnp_db_plugin) {}
static Database *Create(EventLoop &loop, DatabaseListener &listener, static Database *Create(EventLoop &loop, DatabaseListener &listener,
const ConfigBlock &block, const ConfigBlock &block);
Error &error);
virtual void Open() override; virtual void Open() override;
virtual void Close() override; virtual void Close() override;
@ -103,9 +102,6 @@ public:
return 0; return 0;
} }
protected:
bool Configure(const ConfigBlock &block, Error &error);
private: private:
bool VisitServer(const ContentDirectoryService &server, bool VisitServer(const ContentDirectoryService &server,
const std::list<std::string> &vpath, const std::list<std::string> &vpath,
@ -150,21 +146,9 @@ private:
Database * Database *
UpnpDatabase::Create(gcc_unused EventLoop &loop, UpnpDatabase::Create(gcc_unused EventLoop &loop,
gcc_unused DatabaseListener &listener, gcc_unused DatabaseListener &listener,
const ConfigBlock &block, Error &error) const ConfigBlock &)
{ {
UpnpDatabase *db = new UpnpDatabase(); return new UpnpDatabase();
if (!db->Configure(block, error)) {
delete db;
return nullptr;
}
return db;
}
inline bool
UpnpDatabase::Configure(const ConfigBlock &, Error &)
{
return true;
} }
void void

View File

@ -127,13 +127,7 @@ try {
if (path != nullptr) if (path != nullptr)
block.AddBlockParam("path", path->value.c_str(), path->line); block.AddBlockParam("path", path->value.c_str(), path->line);
Database *db = plugin->create(event_loop, database_listener, Database *db = plugin->create(event_loop, database_listener, block);
block, error);
if (db == nullptr) {
cerr << error.GetMessage() << endl;
return EXIT_FAILURE;
}
AtScopeExit(db) { delete db; }; AtScopeExit(db) { delete db; };