Added header for converting HRESULTs to exceptions.
This commit is contained in:
parent
c36fc2d6ed
commit
85373e5293
69
source/mijin/util/winerr.hpp
Normal file
69
source/mijin/util/winerr.hpp
Normal file
@ -0,0 +1,69 @@
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(MIJIN_UTIL_WINERR_HPP_INCLUDED)
|
||||
#define MIJIN_UTIL_WINERR_HPP_INCLUDED 1
|
||||
|
||||
#include <format>
|
||||
|
||||
#include <windows.h>
|
||||
#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)
|
||||
Loading…
x
Reference in New Issue
Block a user