mirror of
https://github.com/WinampDesktop/winamp.git
synced 2025-06-21 03:45:39 -04:00
GPUDevice: Use Error class for initialization errors
This commit is contained in:
@ -28,21 +28,26 @@ void Error::Clear()
|
||||
}
|
||||
|
||||
void Error::SetErrno(int err)
|
||||
{
|
||||
SetErrno(std::string_view(), err);
|
||||
}
|
||||
|
||||
void Error::SetErrno(std::string_view prefix, int err)
|
||||
{
|
||||
m_type = Type::Errno;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
char buf[128];
|
||||
if (strerror_s(buf, sizeof(buf), err) == 0)
|
||||
m_description = fmt::format("errno {}: {}", err, buf);
|
||||
m_description = fmt::format("{}errno {}: {}", prefix, err, buf);
|
||||
else
|
||||
m_description = fmt::format("errno {}: <Could not get error message>", err);
|
||||
m_description = fmt::format("{}errno {}: <Could not get error message>", prefix, err);
|
||||
#else
|
||||
const char* buf = std::strerror(err);
|
||||
if (buf)
|
||||
m_description = fmt::format("errno {}: {}", err, buf);
|
||||
m_description = fmt::format("{}errno {}: {}", prefix, err, buf);
|
||||
else
|
||||
m_description = fmt::format("errno {}: <Could not get error message>", err);
|
||||
m_description = fmt::format("{}errno {}: <Could not get error message>", prefix, err);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -52,20 +57,44 @@ void Error::SetErrno(Error* errptr, int err)
|
||||
errptr->SetErrno(err);
|
||||
}
|
||||
|
||||
void Error::SetErrno(Error* errptr, std::string_view prefix, int err)
|
||||
{
|
||||
if (errptr)
|
||||
errptr->SetErrno(prefix, err);
|
||||
}
|
||||
|
||||
void Error::SetString(std::string description)
|
||||
{
|
||||
m_type = Type::User;
|
||||
m_description = std::move(description);
|
||||
}
|
||||
|
||||
void Error::SetStringView(std::string_view description)
|
||||
{
|
||||
m_type = Type::User;
|
||||
m_description = std::string(description);
|
||||
}
|
||||
|
||||
void Error::SetString(Error* errptr, std::string description)
|
||||
{
|
||||
if (errptr)
|
||||
errptr->SetString(std::move(description));
|
||||
}
|
||||
|
||||
void Error::SetStringView(Error* errptr, std::string_view description)
|
||||
{
|
||||
if (errptr)
|
||||
errptr->SetStringView(std::move(description));
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
void Error::SetWin32(unsigned long err)
|
||||
{
|
||||
SetWin32(std::string_view(), err);
|
||||
}
|
||||
|
||||
void Error::SetWin32(std::string_view prefix, unsigned long err)
|
||||
{
|
||||
m_type = Type::Win32;
|
||||
|
||||
@ -75,11 +104,11 @@ void Error::SetWin32(unsigned long err)
|
||||
if (r > 0)
|
||||
{
|
||||
m_description =
|
||||
fmt::format("Win32 Error {}: {}", err, StringUtil::WideStringToUTF8String(std::wstring_view(buf, r)));
|
||||
fmt::format("{}Win32 Error {}: {}", prefix, err, StringUtil::WideStringToUTF8String(std::wstring_view(buf, r)));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_description = fmt::format("Win32 Error {}: <Could not resolve system error ID>", err);
|
||||
m_description = fmt::format("{}Win32 Error {}: <Could not resolve system error ID>", prefix, err);
|
||||
}
|
||||
}
|
||||
|
||||
@ -89,7 +118,18 @@ void Error::SetWin32(Error* errptr, unsigned long err)
|
||||
errptr->SetWin32(err);
|
||||
}
|
||||
|
||||
void Error::SetWin32(Error* errptr, std::string_view prefix, unsigned long err)
|
||||
{
|
||||
if (errptr)
|
||||
errptr->SetWin32(prefix, err);
|
||||
}
|
||||
|
||||
void Error::SetHResult(long err)
|
||||
{
|
||||
SetHResult(std::string_view(), err);
|
||||
}
|
||||
|
||||
void Error::SetHResult(std::string_view prefix, long err)
|
||||
{
|
||||
m_type = Type::HResult;
|
||||
|
||||
@ -99,11 +139,11 @@ void Error::SetHResult(long err)
|
||||
if (r > 0)
|
||||
{
|
||||
m_description =
|
||||
fmt::format("HRESULT {:08X}: {}", err, StringUtil::WideStringToUTF8String(std::wstring_view(buf, r)));
|
||||
fmt::format("{}HRESULT {:08X}: {}", prefix, err, StringUtil::WideStringToUTF8String(std::wstring_view(buf, r)));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_description = fmt::format("HRESULT {:08X}: <Could not resolve system error ID>", err);
|
||||
m_description = fmt::format("{}HRESULT {:08X}: <Could not resolve system error ID>", prefix, err);
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,15 +153,26 @@ void Error::SetHResult(Error* errptr, long err)
|
||||
errptr->SetHResult(err);
|
||||
}
|
||||
|
||||
void Error::SetHResult(Error* errptr, std::string_view prefix, long err)
|
||||
{
|
||||
if (errptr)
|
||||
errptr->SetHResult(prefix, err);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void Error::SetSocket(int err)
|
||||
{
|
||||
SetSocket(std::string_view(), err);
|
||||
}
|
||||
|
||||
void Error::SetSocket(std::string_view prefix, int err)
|
||||
{
|
||||
// Socket errors are win32 errors on windows
|
||||
#ifdef _WIN32
|
||||
SetWin32(err);
|
||||
SetWin32(prefix, err);
|
||||
#else
|
||||
SetErrno(err);
|
||||
SetErrno(prefix, err);
|
||||
#endif
|
||||
m_type = Type::Socket;
|
||||
}
|
||||
@ -132,6 +183,12 @@ void Error::SetSocket(Error* errptr, int err)
|
||||
errptr->SetSocket(err);
|
||||
}
|
||||
|
||||
void Error::SetSocket(Error* errptr, std::string_view prefix, int err)
|
||||
{
|
||||
if (errptr)
|
||||
errptr->SetSocket(prefix, err);
|
||||
}
|
||||
|
||||
Error Error::CreateNone()
|
||||
{
|
||||
return Error();
|
||||
|
@ -1,11 +1,14 @@
|
||||
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#include "fmt/core.h"
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
class Error
|
||||
{
|
||||
@ -26,26 +29,32 @@ public:
|
||||
};
|
||||
|
||||
ALWAYS_INLINE Type GetType() const { return m_type; }
|
||||
ALWAYS_INLINE bool IsValid() const { return (m_type != Type::None); }
|
||||
ALWAYS_INLINE const std::string& GetDescription() const { return m_description; }
|
||||
|
||||
void Clear();
|
||||
|
||||
/// Error that is set by system functions, such as open().
|
||||
void SetErrno(int err);
|
||||
void SetErrno(std::string_view prefix, int err);
|
||||
|
||||
/// Error that is set by socket functions, such as socket(). On Unix this is the same as errno.
|
||||
void SetSocket(int err);
|
||||
void SetSocket(std::string_view prefix, int err);
|
||||
|
||||
/// Set both description and message.
|
||||
void SetString(std::string description);
|
||||
void SetStringView(std::string_view description);
|
||||
|
||||
#ifdef _WIN32
|
||||
/// Error that is returned by some Win32 functions, such as RegOpenKeyEx. Also used by other APIs through
|
||||
/// GetLastError().
|
||||
void SetWin32(unsigned long err);
|
||||
void SetWin32(std::string_view prefix, unsigned long err);
|
||||
|
||||
/// Error that is returned by Win32 COM methods, e.g. S_OK.
|
||||
void SetHResult(long err);
|
||||
void SetHResult(std::string_view prefix, long err);
|
||||
#endif
|
||||
|
||||
static Error CreateNone();
|
||||
@ -59,10 +68,26 @@ public:
|
||||
|
||||
// helpers for setting
|
||||
static void SetErrno(Error* errptr, int err);
|
||||
static void SetErrno(Error* errptr, std::string_view prefix, int err);
|
||||
static void SetSocket(Error* errptr, int err);
|
||||
static void SetSocket(Error* errptr, std::string_view prefix, int err);
|
||||
static void SetString(Error* errptr, std::string description);
|
||||
static void SetStringView(Error* errptr, std::string_view description);
|
||||
|
||||
#ifdef _WIN32
|
||||
static void SetWin32(Error* errptr, unsigned long err);
|
||||
static void SetWin32(Error* errptr, std::string_view prefix, unsigned long err);
|
||||
static void SetHResult(Error* errptr, long err);
|
||||
static void SetHResult(Error* errptr, std::string_view prefix, long err);
|
||||
#endif
|
||||
|
||||
/// Sets a formatted message.
|
||||
template<typename... T>
|
||||
static void SetStringFmt(Error* errptr, fmt::format_string<T...> fmt, T&&... args)
|
||||
{
|
||||
if (errptr)
|
||||
Error::SetString(errptr, fmt::vformat(fmt, fmt::make_format_args(args...)));
|
||||
}
|
||||
|
||||
Error& operator=(const Error& e);
|
||||
Error& operator=(Error&& e);
|
||||
|
Reference in New Issue
Block a user