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

This commit is contained in:
Max Kellermann
2016-10-28 21:29:01 +02:00
parent aead221184
commit 0c343cb1c3
15 changed files with 145 additions and 257 deletions

View File

@@ -24,6 +24,7 @@
#include "config/ConfigError.hxx"
#include "util/NumberParser.hxx"
#include "util/ReusableArray.hxx"
#include "util/RuntimeError.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
@@ -58,7 +59,7 @@ class PreparedLameEncoder final : public PreparedEncoder {
int bitrate;
public:
bool Configure(const ConfigBlock &block, Error &error);
PreparedLameEncoder(const ConfigBlock &block);
/* virtual methods from class PreparedEncoder */
Encoder *Open(AudioFormat &audio_format, Error &) override;
@@ -70,8 +71,7 @@ public:
static constexpr Domain lame_encoder_domain("lame_encoder");
bool
PreparedLameEncoder::Configure(const ConfigBlock &block, Error &error)
PreparedLameEncoder::PreparedLameEncoder(const ConfigBlock &block)
{
const char *value;
char *endptr;
@@ -82,55 +82,32 @@ PreparedLameEncoder::Configure(const ConfigBlock &block, Error &error)
quality = ParseDouble(value, &endptr);
if (*endptr != '\0' || quality < -1.0 || quality > 10.0) {
error.Format(config_domain,
"quality \"%s\" is not a number in the "
"range -1 to 10",
value);
return false;
}
if (*endptr != '\0' || quality < -1.0 || quality > 10.0)
throw FormatRuntimeError("quality \"%s\" is not a number in the "
"range -1 to 10",
value);
if (block.GetBlockValue("bitrate") != nullptr) {
error.Set(config_domain,
"quality and bitrate are both defined");
return false;
}
if (block.GetBlockValue("bitrate") != nullptr)
throw std::runtime_error("quality and bitrate are both defined");
} else {
/* a bit rate was configured */
value = block.GetBlockValue("bitrate");
if (value == nullptr) {
error.Set(config_domain,
"neither bitrate nor quality defined");
return false;
}
if (value == nullptr)
throw std::runtime_error("neither bitrate nor quality defined");
quality = -2.0;
bitrate = ParseInt(value, &endptr);
if (*endptr != '\0' || bitrate <= 0) {
error.Set(config_domain,
"bitrate should be a positive integer");
return false;
}
if (*endptr != '\0' || bitrate <= 0)
throw std::runtime_error("bitrate should be a positive integer");
}
return true;
}
static PreparedEncoder *
lame_encoder_init(const ConfigBlock &block, Error &error)
lame_encoder_init(const ConfigBlock &block)
{
auto *encoder = new PreparedLameEncoder();
/* load configuration from "block" */
if (!encoder->Configure(block, error)) {
/* configuration has failed, roll back and return error */
delete encoder;
return nullptr;
}
return encoder;
return new PreparedLameEncoder(block);
}
static bool