From 6c512c19736c81900200155290926d0bc43e2be6 Mon Sep 17 00:00:00 2001 From: Jonathan Li Date: Fri, 14 Apr 2017 02:11:34 +0100 Subject: [PATCH] spu2-x:xaudio2: Simplify XAudio2Error exception Instead of creating a separate message function, just reuse what() and pass the string to the std::runtime_error constructor instead. Also catch a reference to a std::runtime_error instead, since it'll allow for simpler cleanup. --- plugins/spu2-x/src/Windows/SndOut_XAudio2.inl | 45 +++++++++---------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/plugins/spu2-x/src/Windows/SndOut_XAudio2.inl b/plugins/spu2-x/src/Windows/SndOut_XAudio2.inl index 3d2ad5825f..70818c93ad 100644 --- a/plugins/spu2-x/src/Windows/SndOut_XAudio2.inl +++ b/plugins/spu2-x/src/Windows/SndOut_XAudio2.inl @@ -16,42 +16,37 @@ */ #include +#include +#include +#include namespace Exception { class XAudio2Error : public std::runtime_error { -protected: - static const char *SomeKindaErrorString(HRESULT hr) +private: + static std::string CreateErrorMessage(const HRESULT result, const std::string &msg) { - switch (hr) { + std::stringstream ss; + ss << " (code 0x" << std::hex << result << ")\n\n"; + switch (result) { case XAUDIO2_E_INVALID_CALL: - return "Invalid call for the XA2 object state."; - + ss << "Invalid call for the XA2 object state."; + break; case XAUDIO2_E_DEVICE_INVALIDATED: - return "Device is unavailable, unplugged, unsupported, or has been consumed by The Nothing."; + ss << "Device is unavailable, unplugged, unsupported, or has been consumed by The Nothing."; + break; + default: + ss << "Unknown error code!"; + break; } - return "Unknown error code!"; + return msg + ss.str(); } public: - const HRESULT ErrorCode; - std::string m_Message; - - const char *CMessage() const + explicit XAudio2Error(const HRESULT result, const std::string &msg) + : std::runtime_error(CreateErrorMessage(result, msg)) { - return m_Message.c_str(); - } - - virtual ~XAudio2Error() throw() {} - XAudio2Error(const HRESULT result, const std::string &msg) - : runtime_error(msg) - , ErrorCode(result) - , m_Message() - { - char omg[1024]; - sprintf_s(omg, "%s (code 0x%x)\n\n%s", what(), ErrorCode, SomeKindaErrorString(ErrorCode)); - m_Message = omg; } }; } @@ -368,8 +363,8 @@ public: } voiceContext->Init(pXAudio2); - } catch (Exception::XAudio2Error &ex) { - SysMessage(ex.CMessage()); + } catch (std::runtime_error &ex) { + SysMessage(ex.what()); pXAudio2.Release(); CoUninitialize(); return -1;