pcsx2/common/Error.h

106 lines
3.0 KiB
C
Raw Permalink Normal View History

2024-04-01 10:48:39 +00:00
// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team
// SPDX-License-Identifier: GPL-3.0+
2022-12-28 11:13:26 +00:00
#pragma once
2024-04-01 10:48:39 +00:00
2022-12-28 11:13:26 +00:00
#include "Pcsx2Defs.h"
2024-04-01 10:48:39 +00:00
#include "fmt/core.h"
2022-12-28 11:13:26 +00:00
#include <string>
class Error
{
public:
Error();
Error(const Error& e);
Error(Error&& e);
~Error();
enum class Type
{
None = 0,
Errno = 1,
Socket = 2,
User = 3,
Win32 = 4,
HResult = 5,
};
__fi Type GetType() const { return m_type; }
2024-04-01 10:48:39 +00:00
__fi bool IsValid() const { return (m_type != Type::None); }
2022-12-28 11:13:26 +00:00
__fi const std::string& GetDescription() const { return m_description; }
void Clear();
/// Error that is set by system functions, such as open().
void SetErrno(int err);
2024-04-01 10:48:39 +00:00
void SetErrno(std::string_view prefix, int err);
2022-12-28 11:13:26 +00:00
/// Error that is set by socket functions, such as socket(). On Unix this is the same as errno.
void SetSocket(int err);
2024-04-01 10:48:39 +00:00
void SetSocket(std::string_view prefix, int err);
2022-12-28 11:13:26 +00:00
/// Set both description and message.
void SetString(std::string description);
2024-04-01 10:48:39 +00:00
void SetStringView(std::string_view description);
2022-12-28 11:13:26 +00:00
#ifdef _WIN32
2024-04-01 10:48:39 +00:00
/// Error that is returned by some Win32 functions, such as RegOpenKeyEx. Also used by other APIs through
/// GetLastError().
2022-12-28 11:13:26 +00:00
void SetWin32(unsigned long err);
2024-04-01 10:48:39 +00:00
void SetWin32(std::string_view prefix, unsigned long err);
2022-12-28 11:13:26 +00:00
/// Error that is returned by Win32 COM methods, e.g. S_OK.
void SetHResult(long err);
2024-04-01 10:48:39 +00:00
void SetHResult(std::string_view prefix, long err);
2022-12-28 11:13:26 +00:00
#endif
static Error CreateNone();
static Error CreateErrno(int err);
static Error CreateSocket(int err);
static Error CreateString(std::string description);
#ifdef _WIN32
static Error CreateWin32(unsigned long err);
static Error CreateHResult(long err);
#endif
// helpers for setting
static void Clear(Error* errptr);
2022-12-28 11:13:26 +00:00
static void SetErrno(Error* errptr, int err);
2024-04-01 10:48:39 +00:00
static void SetErrno(Error* errptr, std::string_view prefix, int err);
2022-12-28 11:13:26 +00:00
static void SetSocket(Error* errptr, int err);
2024-04-01 10:48:39 +00:00
static void SetSocket(Error* errptr, std::string_view prefix, int err);
2022-12-28 11:13:26 +00:00
static void SetString(Error* errptr, std::string description);
2024-04-01 10:48:39 +00:00
static void SetStringView(Error* errptr, std::string_view description);
#ifdef _WIN32
2022-12-28 11:13:26 +00:00
static void SetWin32(Error* errptr, unsigned long err);
2024-04-01 10:48:39 +00:00
static void SetWin32(Error* errptr, std::string_view prefix, unsigned long err);
2022-12-28 11:13:26 +00:00
static void SetHResult(Error* errptr, long err);
2024-04-01 10:48:39 +00:00
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...)));
}
void AddPrefix(std::string_view prefix);
void AddSuffix(std::string_view suffix);
static void AddPrefix(Error* errptr, std::string_view prefix);
static void AddSuffix(Error* errptr, std::string_view prefix);
2022-12-28 11:13:26 +00:00
Error& operator=(const Error& e);
Error& operator=(Error&& e);
bool operator==(const Error& e) const;
bool operator!=(const Error& e) const;
private:
Type m_type = Type::None;
std::string m_description;
};