Common: Create HRWrap

HRWrap now allows HRESULT to be formatted, giving useful information beyond "it failed" or a hex code that isn't obvious to most users.  This commit does not add any uses of it, though.
This commit is contained in:
Pokechu22 2021-12-12 12:04:13 -08:00
parent c296c34e00
commit 140c8217f6
4 changed files with 57 additions and 1 deletions

View file

@ -164,7 +164,7 @@ elseif(WIN32)
winmm.lib
)
if (_M_X86_64)
target_link_libraries(common PRIVATE opengl32.lib)
target_link_libraries(common PRIVATE opengl32.lib)
endif()
elseif (ANDROID)
target_link_libraries(common
@ -286,6 +286,10 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_libraries(common PUBLIC dl rt)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
target_sources(common PUBLIC HRWrap.h HRWrap.cpp)
endif()
if(USE_UPNP)
target_link_libraries(common PRIVATE Miniupnpc::miniupnpc)
endif()

View file

@ -0,0 +1,17 @@
// Copyright 2021 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "HRWrap.h"
#include <comdef.h>
#include "Common/StringUtil.h"
namespace Common
{
std::string GetHResultMessage(HRESULT hr)
{
// See https://stackoverflow.com/a/7008111
_com_error err(hr);
return TStrToUTF8(err.ErrorMessage());
}
} // namespace Common

View file

@ -0,0 +1,33 @@
// Copyright 2021 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <fmt/format.h>
#include <string>
#include <winerror.h>
namespace Common
{
std::string GetHResultMessage(HRESULT hr);
// Wrapper for HRESULT to be used with fmt. Note that we can't create a fmt::formatter directly
// for HRESULT as HRESULT is simply a typedef on long and not a distinct type.
struct HRWrap
{
constexpr explicit HRWrap(HRESULT hr) : m_hr(hr) {}
const HRESULT m_hr;
};
} // namespace Common
template <>
struct fmt::formatter<Common::HRWrap>
{
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const Common::HRWrap& hr, FormatContext& ctx)
{
return fmt::format_to(ctx.out(), "{} ({:#010x})", Common::GetHResultMessage(hr.m_hr),
static_cast<u32>(hr.m_hr));
}
};

View file

@ -107,6 +107,7 @@
<ClInclude Include="Common\GL\GLUtil.h" />
<ClInclude Include="Common\GL\GLX11Window.h" />
<ClInclude Include="Common\Hash.h" />
<ClInclude Include="Common\HRWrap.h" />
<ClInclude Include="Common\HttpRequest.h" />
<ClInclude Include="Common\Image.h" />
<ClInclude Include="Common\ImageC.h" />
@ -707,6 +708,7 @@
<ClCompile Include="Common\GL\GLInterface\WGL.cpp" />
<ClCompile Include="Common\GL\GLUtil.cpp" />
<ClCompile Include="Common\Hash.cpp" />
<ClCompile Include="Common\HRWrap.cpp" />
<ClCompile Include="Common\HttpRequest.cpp" />
<ClCompile Include="Common\Image.cpp" />
<ClCompile Include="Common\ImageC.c">