diff options
author | Chris Robinson <[email protected]> | 2020-04-10 15:11:40 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2020-04-10 15:12:57 -0700 |
commit | f7380a44d4c5c6345602db8630ae7bbd971cac1d (patch) | |
tree | 9034ad41ed0275498c7c712b9fe4cff8f5c725ee | |
parent | 611a0155cd4bbe588918b07a219cc830c77691c3 (diff) |
Use a common base for a couple exceptions
-rw-r--r-- | al/filter.cpp | 32 | ||||
-rw-r--r-- | alc/backends/base.h | 19 | ||||
-rw-r--r-- | common/alexcpt.cpp | 6 | ||||
-rw-r--r-- | common/alexcpt.h | 18 |
4 files changed, 36 insertions, 39 deletions
diff --git a/al/filter.cpp b/al/filter.cpp index ecbdc7f6..a861c8cd 100644 --- a/al/filter.cpp +++ b/al/filter.cpp @@ -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 */ diff --git a/alc/backends/base.h b/alc/backends/base.h index 07200e0e..db9b2296 100644 --- a/alc/backends/base.h +++ b/alc/backends/base.h @@ -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 */ diff --git a/common/alexcpt.cpp b/common/alexcpt.cpp index 5055e34f..111258a0 100644 --- a/common/alexcpt.cpp +++ b/common/alexcpt.cpp @@ -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 diff --git a/common/alexcpt.h b/common/alexcpt.h index ff09bab2..b399e8f7 100644 --- a/common/alexcpt.h +++ b/common/alexcpt.h @@ -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; } }; |