diff options
Diffstat (limited to 'zen/com_error.h')
-rw-r--r-- | zen/com_error.h | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/zen/com_error.h b/zen/com_error.h index 2ba76c0f..4546bd8a 100644 --- a/zen/com_error.h +++ b/zen/com_error.h @@ -16,6 +16,29 @@ namespace zen std::wstring generateErrorMsg(const std::wstring& input, HRESULT hr); std::wstring formatWin32Msg(DWORD dwMessageId); //return empty string on error +class ComError +{ +public: + explicit ComError(const std::wstring& msg, HRESULT hr = S_OK) : msg_(hr == S_OK ? msg : generateErrorMsg(msg, hr)) {} + const std::wstring& toString() const { return msg_; } + +private: + std::wstring msg_; +}; + +#define ZEN_CHECK_COM(func) ZEN_CHECK_COM_ERROR(func, #func) //throw ComError +/*Convenience Macro checking for COM errors: + +Example: ZEN_CHECK_COM(backupComp->InitializeForBackup()); + +Equivalent to: +{ + HRESULT hrInternal = backupComp->InitializeForBackup(); + if (FAILED(hrInternal)) + throw ComError(L"Error calling \"backupComp->InitializeForBackup()\".", hrInternal); +} +*/ + @@ -202,5 +225,18 @@ std::wstring generateErrorMsg(const std::wstring& input, HRESULT hr) } return output; } + + +#define ZEN_CHECK_COM_ERROR(func, txt) \ + { \ + HRESULT hrInternal = func; \ + if (FAILED(hrInternal)) \ + throw ComError(L"Error calling \"" ## ZEN_CONCAT_SUB(L, txt) ## L"\".", hrInternal); \ + } + +#ifndef ZEN_CONCAT //redeclare those macros: avoid dependency to scope_guard.h +#define ZEN_CONCAT_SUB(X, Y) X ## Y +#define ZEN_CONCAT(X, Y) ZEN_CONCAT_SUB(X, Y) +#endif } #endif //COM_ERROR_HEADER |