From 871063dab749cb03729d49a838f76634426f30ad Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 5 Sep 2016 10:53:54 +0200 Subject: [PATCH] neighbor/Plugin: migrate from class Error to C++ exceptions --- src/Main.cxx | 10 ++-- src/neighbor/Explorer.hxx | 5 +- src/neighbor/Glue.cxx | 46 +++++++------------ src/neighbor/Glue.hxx | 8 ++-- src/neighbor/NeighborPlugin.hxx | 4 +- .../plugins/SmbclientNeighborPlugin.cxx | 10 ++-- src/neighbor/plugins/UpnpNeighborPlugin.cxx | 11 ++--- test/run_neighbor_explorer.cxx | 13 ++---- 8 files changed, 40 insertions(+), 67 deletions(-) diff --git a/src/Main.cxx b/src/Main.cxx index acb4b6b67..cb93a4c13 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -461,10 +461,7 @@ int mpd_main(int argc, char *argv[]) #ifdef ENABLE_NEIGHBOR_PLUGINS instance->neighbors = new NeighborGlue(); - if (!instance->neighbors->Init(io_thread_get(), *instance, error)) { - LogError(error); - return EXIT_FAILURE; - } + instance->neighbors->Init(io_thread_get(), *instance); if (instance->neighbors->IsEmpty()) { delete instance->neighbors; @@ -563,9 +560,8 @@ try { io_thread_start(); #ifdef ENABLE_NEIGHBOR_PLUGINS - if (instance->neighbors != nullptr && - !instance->neighbors->Open(error)) - FatalError(error); + if (instance->neighbors != nullptr) + instance->neighbors->Open(); #endif ZeroconfInit(instance->event_loop); diff --git a/src/neighbor/Explorer.hxx b/src/neighbor/Explorer.hxx index 305fd5f25..ff41ed1ea 100644 --- a/src/neighbor/Explorer.hxx +++ b/src/neighbor/Explorer.hxx @@ -22,7 +22,6 @@ #include -class Error; class NeighborListener; struct NeighborInfo; @@ -54,8 +53,10 @@ public: /** * Start exploring the neighborhood. + * + * Throws std::runtime_error on error. */ - virtual bool Open(Error &error) = 0; + virtual void Open() = 0; /** * Stop exploring. diff --git a/src/neighbor/Glue.cxx b/src/neighbor/Glue.cxx index 3953dfb25..364ef8f39 100644 --- a/src/neighbor/Glue.cxx +++ b/src/neighbor/Glue.cxx @@ -26,7 +26,6 @@ #include "config/ConfigGlobal.hxx" #include "config/ConfigError.hxx" #include "config/Block.hxx" -#include "util/Error.hxx" #include "util/RuntimeError.hxx" #include @@ -40,63 +39,50 @@ NeighborGlue::~NeighborGlue() {} static NeighborExplorer * CreateNeighborExplorer(EventLoop &loop, NeighborListener &listener, - const ConfigBlock &block, Error &error) + const ConfigBlock &block) { const char *plugin_name = block.GetBlockValue("plugin"); - if (plugin_name == nullptr) { - error.Set(config_domain, - "Missing \"plugin\" configuration"); - return nullptr; - } + if (plugin_name == nullptr) + throw std::runtime_error("Missing \"plugin\" configuration"); const NeighborPlugin *plugin = GetNeighborPluginByName(plugin_name); - if (plugin == nullptr) { - error.Format(config_domain, "No such neighbor plugin: %s", - plugin_name); - return nullptr; - } + if (plugin == nullptr) + throw FormatRuntimeError("No such neighbor plugin: %s", + plugin_name); - return plugin->create(loop, listener, block, error); + return plugin->create(loop, listener, block); } -bool -NeighborGlue::Init(EventLoop &loop, NeighborListener &listener, Error &error) +void +NeighborGlue::Init(EventLoop &loop, NeighborListener &listener) { for (const auto *block = config_get_block(ConfigBlockOption::NEIGHBORS); block != nullptr; block = block->next) { try { auto *explorer = - CreateNeighborExplorer(loop, listener, *block, - error); - if (explorer == nullptr) { - error.FormatPrefix("Line %i: ", block->line); - return false; - } - + CreateNeighborExplorer(loop, listener, *block); explorers.emplace_front(explorer); } catch (...) { std::throw_with_nested(FormatRuntimeError("Line %i: ", block->line)); } } - - return true; } -bool -NeighborGlue::Open(Error &error) +void +NeighborGlue::Open() { for (auto i = explorers.begin(), end = explorers.end(); i != end; ++i) { - if (!i->explorer->Open(error)) { + try { + i->explorer->Open(); + } catch (...) { /* roll back */ for (auto k = explorers.begin(); k != i; ++k) k->explorer->Close(); - return false; + throw; } } - - return true; } void diff --git a/src/neighbor/Glue.hxx b/src/neighbor/Glue.hxx index 27e1b7157..aaa0b5dd3 100644 --- a/src/neighbor/Glue.hxx +++ b/src/neighbor/Glue.hxx @@ -27,7 +27,6 @@ #include struct config_param; -class Error; class EventLoop; class NeighborExplorer; class NeighborListener; @@ -60,9 +59,12 @@ public: return explorers.empty(); } - bool Init(EventLoop &loop, NeighborListener &listener, Error &error); + /** + * Throws std::runtime_error on error. + */ + void Init(EventLoop &loop, NeighborListener &listener); - bool Open(Error &error); + void Open(); void Close(); /** diff --git a/src/neighbor/NeighborPlugin.hxx b/src/neighbor/NeighborPlugin.hxx index 3fd89818a..e589a7338 100644 --- a/src/neighbor/NeighborPlugin.hxx +++ b/src/neighbor/NeighborPlugin.hxx @@ -21,7 +21,6 @@ #define MPD_NEIGHBOR_PLUGIN_HXX struct ConfigBlock; -class Error; class EventLoop; class NeighborListener; class NeighborExplorer; @@ -33,8 +32,7 @@ struct NeighborPlugin { * Allocates and configures a #NeighborExplorer instance. */ NeighborExplorer *(*create)(EventLoop &loop, NeighborListener &listener, - const ConfigBlock &block, - Error &error); + const ConfigBlock &block); }; #endif diff --git a/src/neighbor/plugins/SmbclientNeighborPlugin.cxx b/src/neighbor/plugins/SmbclientNeighborPlugin.cxx index 32125896a..ce076f2fd 100644 --- a/src/neighbor/plugins/SmbclientNeighborPlugin.cxx +++ b/src/neighbor/plugins/SmbclientNeighborPlugin.cxx @@ -72,7 +72,7 @@ public: :NeighborExplorer(_listener) {} /* virtual methods from class NeighborExplorer */ - virtual bool Open(Error &error) override; + void Open() override; virtual void Close() override; virtual List GetList() const override; @@ -82,12 +82,11 @@ private: static void ThreadFunc(void *ctx); }; -bool -SmbclientNeighborExplorer::Open(gcc_unused Error &error) +void +SmbclientNeighborExplorer::Open() { quit = false; thread.Start(ThreadFunc, this); - return true; } void @@ -270,8 +269,7 @@ SmbclientNeighborExplorer::ThreadFunc(void *ctx) static NeighborExplorer * smbclient_neighbor_create(gcc_unused EventLoop &loop, NeighborListener &listener, - gcc_unused const ConfigBlock &block, - gcc_unused Error &error) + gcc_unused const ConfigBlock &block) { SmbclientInit(); diff --git a/src/neighbor/plugins/UpnpNeighborPlugin.cxx b/src/neighbor/plugins/UpnpNeighborPlugin.cxx index e83563a3f..bec8e5a5c 100644 --- a/src/neighbor/plugins/UpnpNeighborPlugin.cxx +++ b/src/neighbor/plugins/UpnpNeighborPlugin.cxx @@ -60,7 +60,7 @@ public: :NeighborExplorer(_listener) {} /* virtual methods from class NeighborExplorer */ - virtual bool Open(Error &error) override; + void Open() override; virtual void Close() override; virtual List GetList() const override; @@ -70,8 +70,8 @@ private: virtual void LostUPnP(const ContentDirectoryService &service) override; }; -bool -UpnpNeighborExplorer::Open(gcc_unused Error &error) +void +UpnpNeighborExplorer::Open() { UpnpClient_Handle handle; UpnpClientGlobalInit(handle); @@ -85,8 +85,6 @@ UpnpNeighborExplorer::Open(gcc_unused Error &error) UpnpClientGlobalFinish(); throw; } - - return true; } void @@ -130,8 +128,7 @@ UpnpNeighborExplorer::LostUPnP(const ContentDirectoryService &service) static NeighborExplorer * upnp_neighbor_create(gcc_unused EventLoop &loop, NeighborListener &listener, - gcc_unused const ConfigBlock &block, - gcc_unused Error &error) + gcc_unused const ConfigBlock &block) { return new UpnpNeighborExplorer(listener); } diff --git a/test/run_neighbor_explorer.cxx b/test/run_neighbor_explorer.cxx index 0fbd434c0..22e7fd082 100644 --- a/test/run_neighbor_explorer.cxx +++ b/test/run_neighbor_explorer.cxx @@ -24,7 +24,6 @@ #include "neighbor/Glue.hxx" #include "fs/Path.hxx" #include "event/Loop.hxx" -#include "util/Error.hxx" #include "Log.hxx" #include @@ -56,8 +55,6 @@ try { /* read configuration file (mpd.conf) */ - Error error; - config_global_init(); ReadConfigFile(config_path); @@ -69,17 +66,15 @@ try { MyNeighborListener listener; NeighborGlue neighbor; - if (!neighbor.Init(loop, listener, error) || !neighbor.Open(error)) { - LogError(error); - return EXIT_FAILURE; - } + neighbor.Init(loop, listener); + neighbor.Open(); /* run */ loop.Run(); neighbor.Close(); return EXIT_SUCCESS; - } catch (const std::exception &e) { +} catch (const std::exception &e) { LogError(e); return EXIT_FAILURE; - } +}