Use a common base for a couple exceptions

This commit is contained in:
Chris Robinson
2020-04-10 15:11:40 -07:00
parent 611a0155cd
commit f7380a44d4
4 changed files with 36 additions and 39 deletions
+8 -24
View File
@@ -47,33 +47,17 @@
namespace {
class filter_exception final : public std::exception {
std::string mMessage;
ALenum mErrorCode;
class filter_exception final : public al::base_exception {
public:
filter_exception(ALenum code, const char *msg, ...) ALEXCPT_FORMAT(printf, 3,4);
const char *what() const noexcept override { return mMessage.c_str(); }
ALenum errorCode() const noexcept { return mErrorCode; }
};
filter_exception::filter_exception(ALenum code, const char *msg, ...) : mErrorCode{code}
{
std::va_list args, args2;
va_start(args, msg);
va_copy(args2, args);
int msglen{std::vsnprintf(nullptr, 0, msg, args)};
if LIKELY(msglen > 0)
[[gnu::format(printf, 3, 4)]]
filter_exception(ALenum code, const char *msg, ...) : base_exception{code}
{
mMessage.resize(static_cast<size_t>(msglen)+1);
std::vsnprintf(&mMessage[0], mMessage.length(), msg, args2);
mMessage.pop_back();
va_list args;
va_start(args, msg);
setMessage(msg, args);
va_end(args);
}
va_end(args2);
va_end(args);
}
};
#define FILTER_MIN_GAIN 0.0f
#define FILTER_MAX_GAIN 4.0f /* +12dB */
+18 -1
View File
@@ -8,8 +8,9 @@
#include "AL/alc.h"
#include "alcmain.h"
#include "albyte.h"
#include "alcmain.h"
#include "alexcpt.h"
struct ClockLatency {
@@ -79,4 +80,20 @@ protected:
virtual ~BackendFactory() = default;
};
namespace al {
class backend_exception final : public base_exception {
public:
[[gnu::format(printf, 3, 4)]]
backend_exception(ALCenum code, const char *msg, ...) : base_exception{code}
{
std::va_list args;
va_start(args, msg);
setMessage(msg, args);
va_end(args);
}
};
} // namespace al
#endif /* ALC_BACKENDS_BASE_H */
+2 -4
View File
@@ -11,10 +11,9 @@
namespace al {
backend_exception::backend_exception(ALCenum code, const char *msg, ...) : mErrorCode{code}
void base_exception::setMessage(const char* msg, std::va_list args)
{
va_list args, args2;
va_start(args, msg);
std::va_list args2;
va_copy(args2, args);
int msglen{std::vsnprintf(nullptr, 0, msg, args)};
if LIKELY(msglen > 0)
@@ -24,7 +23,6 @@ backend_exception::backend_exception(ALCenum code, const char *msg, ...) : mErro
mMessage.pop_back();
}
va_end(args2);
va_end(args);
}
} // namespace al
+8 -10
View File
@@ -1,28 +1,26 @@
#ifndef ALEXCPT_H
#define ALEXCPT_H
#include <cstdarg>
#include <exception>
#include <string>
#include <utility>
#include "AL/alc.h"
#ifdef __GNUC__
#define ALEXCPT_FORMAT(x, y, z) __attribute__((format(x, (y), (z))))
#else
#define ALEXCPT_FORMAT(x, y, z)
#endif
namespace al {
class backend_exception final : public std::exception {
class base_exception : public std::exception {
std::string mMessage;
ALCenum mErrorCode;
public:
backend_exception(ALCenum code, const char *msg, ...) ALEXCPT_FORMAT(printf, 3,4);
protected:
base_exception(ALCenum code) : mErrorCode{code} { }
void setMessage(const char *msg, std::va_list args);
public:
const char *what() const noexcept override { return mMessage.c_str(); }
ALCenum errorCode() const noexcept { return mErrorCode; }
};