diff options
author | Max Kellermann <max@musicpd.org> | 2016-11-05 15:24:10 +0100 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2016-11-05 15:24:10 +0100 |
commit | b45ea66175d599b430c7bb501859e8efad31a21e (patch) | |
tree | 7ef15bfae8ed8a65c1500912b0f5b701f7fdfe3c | |
parent | 543c5034afbec1ee3c46db2cb2d54966c55c7321 (diff) |
output/pipe: migrate from class Error to C++ exceptions
-rw-r--r-- | src/output/plugins/PipeOutputPlugin.cxx | 53 |
1 files changed, 16 insertions, 37 deletions
diff --git a/src/output/plugins/PipeOutputPlugin.cxx b/src/output/plugins/PipeOutputPlugin.cxx index 4f3e72c4c..476e5ebb0 100644 --- a/src/output/plugins/PipeOutputPlugin.cxx +++ b/src/output/plugins/PipeOutputPlugin.cxx @@ -21,10 +21,10 @@ #include "PipeOutputPlugin.hxx" #include "../OutputAPI.hxx" #include "../Wrapper.hxx" -#include "config/ConfigError.hxx" -#include "util/Error.hxx" +#include "system/Error.hxx" #include <string> +#include <stdexcept> #include <stdio.h> @@ -33,13 +33,10 @@ class PipeOutput { AudioOutput base; - std::string cmd; + const std::string cmd; FILE *fh; - PipeOutput() - :base(pipe_output_plugin) {} - - bool Configure(const ConfigBlock &block, Error &error); + PipeOutput(const ConfigBlock &block); public: static PipeOutput *Create(const ConfigBlock &block, Error &error); @@ -53,54 +50,36 @@ public: size_t Play(const void *chunk, size_t size, Error &error); }; -inline bool -PipeOutput::Configure(const ConfigBlock &block, Error &error) +PipeOutput::PipeOutput(const ConfigBlock &block) + :base(pipe_output_plugin, block), + cmd(block.GetBlockValue("command", "")) { - if (!base.Configure(block, error)) - return false; - - cmd = block.GetBlockValue("command", ""); - if (cmd.empty()) { - error.Set(config_domain, - "No \"command\" parameter specified"); - return false; - } - - return true; + if (cmd.empty()) + throw std::runtime_error("No \"command\" parameter specified"); } inline PipeOutput * -PipeOutput::Create(const ConfigBlock &block, Error &error) +PipeOutput::Create(const ConfigBlock &block, Error &) { - PipeOutput *po = new PipeOutput(); - - if (!po->Configure(block, error)) { - delete po; - return nullptr; - } - - return po; + return new PipeOutput(block); } inline bool -PipeOutput::Open(gcc_unused AudioFormat &audio_format, Error &error) +PipeOutput::Open(gcc_unused AudioFormat &audio_format, Error &) { fh = popen(cmd.c_str(), "w"); - if (fh == nullptr) { - error.FormatErrno("Error opening pipe \"%s\"", - cmd.c_str()); - return false; - } + if (fh == nullptr) + throw FormatErrno("Error opening pipe \"%s\"", cmd.c_str()); return true; } inline size_t -PipeOutput::Play(const void *chunk, size_t size, Error &error) +PipeOutput::Play(const void *chunk, size_t size, Error &) { size_t nbytes = fwrite(chunk, 1, size, fh); if (nbytes == 0) - error.SetErrno("Write error on pipe"); + throw MakeErrno("Write error on pipe"); return nbytes; } |