#pragma once #if !defined(MIJIN_UTIL_WINERR_HPP_INCLUDED) #define MIJIN_UTIL_WINERR_HPP_INCLUDED 1 #include #include #include "./winundef.hpp" #include "./exception.hpp" namespace mijin { // // public types // class HRException : public Exception { private: HRESULT hResult_; public: explicit inline HRException(HRESULT hResult); inline HRException(std::string_view message, HRESULT hResult); }; // // public functions // [[nodiscard]] inline std::string getHResultMessage(HRESULT hResult) { if (hResult == S_OK) { return "Success (0x0)"; } return std::format("Unknown error (0x{:x}", hResult); } inline HRESULT ensureHR(HRESULT hResult, std::string_view message = {}) { if (!SUCCEEDED(hResult)) { if (message.empty()) { throw HRException(hResult); } throw HRException(message, hResult); } return hResult; } HRException::HRException(HRESULT hResult) : Exception(getHResultMessage(hResult)), hResult_(hResult) { } HRException::HRException(std::string_view message, HRESULT hResult) : Exception(std::format("{} {}", message, getHResultMessage(hResult))), hResult_(hResult) { } } #endif // !defined(MIJIN_UTIL_WINERR_HPP_INCLUDED)