Duckstation/src/common/error.h

76 lines
1.9 KiB
C
Raw Normal View History

2023-08-19 13:40:36 +00:00
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
2021-03-18 15:51:39 +00:00
#pragma once
2023-08-19 13:40:36 +00:00
2021-03-18 15:51:39 +00:00
#include "types.h"
2023-08-19 13:40:36 +00:00
#include <string>
2021-03-18 15:51:39 +00:00
class Error
{
public:
Error();
Error(const Error& e);
2023-08-19 13:40:36 +00:00
Error(Error&& e);
2021-03-18 15:51:39 +00:00
~Error();
enum class Type
{
2023-08-19 13:40:36 +00:00
None = 0,
Errno = 1,
Socket = 2,
User = 3,
Win32 = 4,
HResult = 5,
2021-03-18 15:51:39 +00:00
};
ALWAYS_INLINE Type GetType() const { return m_type; }
2023-08-19 13:40:36 +00:00
ALWAYS_INLINE const std::string& GetDescription() const { return m_description; }
2021-03-18 15:51:39 +00:00
void Clear();
2023-08-19 13:40:36 +00:00
/// Error that is set by system functions, such as open().
2021-03-18 15:51:39 +00:00
void SetErrno(int err);
2023-08-19 13:40:36 +00:00
/// Error that is set by socket functions, such as socket(). On Unix this is the same as errno.
2021-03-18 15:51:39 +00:00
void SetSocket(int err);
2023-08-19 13:40:36 +00:00
/// Set both description and message.
void SetString(std::string description);
2021-03-18 15:51:39 +00:00
#ifdef _WIN32
2023-08-19 13:40:36 +00:00
/// Error that is returned by some Win32 functions, such as RegOpenKeyEx. Also used by other APIs through
/// GetLastError().
2021-03-18 15:51:39 +00:00
void SetWin32(unsigned long err);
2023-08-19 13:40:36 +00:00
/// Error that is returned by Win32 COM methods, e.g. S_OK.
2021-03-18 15:51:39 +00:00
void SetHResult(long err);
#endif
static Error CreateNone();
static Error CreateErrno(int err);
static Error CreateSocket(int err);
2023-08-19 13:40:36 +00:00
static Error CreateString(std::string description);
2021-03-18 15:51:39 +00:00
#ifdef _WIN32
static Error CreateWin32(unsigned long err);
static Error CreateHResult(long err);
#endif
2023-08-19 13:40:36 +00:00
// helpers for setting
static void SetErrno(Error* errptr, int err);
static void SetSocket(Error* errptr, int err);
static void SetString(Error* errptr, std::string description);
static void SetWin32(Error* errptr, unsigned long err);
static void SetHResult(Error* errptr, long err);
2021-03-18 15:51:39 +00:00
Error& operator=(const Error& e);
2023-08-19 13:40:36 +00:00
Error& operator=(Error&& e);
2021-03-18 15:51:39 +00:00
bool operator==(const Error& e) const;
bool operator!=(const Error& e) const;
private:
Type m_type = Type::None;
2023-08-19 13:40:36 +00:00
std::string m_description;
2021-03-18 15:51:39 +00:00
};