30 #ifndef SYSTEM_ERROR_HXX
31 #define SYSTEM_ERROR_HXX
36 #include <system_error>
41 template<
typename... Args>
42 static inline std::system_error
46 snprintf(buffer,
sizeof(buffer), fmt, std::forward<Args>(args)...);
47 return std::system_error(code, buffer);
54 static inline std::system_error
55 MakeLastError(DWORD code,
const char *msg)
57 return std::system_error(std::error_code(code, std::system_category()),
61 static inline std::system_error
62 MakeLastError(
const char *msg)
64 return MakeLastError(GetLastError(), msg);
67 template<
typename... Args>
68 static inline std::system_error
69 FormatLastError(DWORD code,
const char *
fmt, Args&&... args)
72 const auto end = buffer +
sizeof(buffer);
73 size_t length = snprintf(buffer,
sizeof(buffer) - 128,
74 fmt, std::forward<Args>(args)...);
75 char *p = buffer + length;
79 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
80 FORMAT_MESSAGE_IGNORE_INSERTS,
81 nullptr, code, 0, p, end - p,
nullptr);
82 return MakeLastError(code, buffer);
85 template<
typename... Args>
86 static inline std::system_error
87 FormatLastError(
const char *fmt, Args&&... args)
89 return FormatLastError(GetLastError(), fmt,
90 std::forward<Args>(args)...);
106 static inline const std::error_category &
112 return std::generic_category();
116 return std::system_category();
120 static inline std::system_error
123 return std::system_error(std::error_code(code,
ErrnoCategory()),
127 static inline std::system_error
133 template<
typename... Args>
134 static inline std::system_error
138 snprintf(buffer,
sizeof(buffer),
139 fmt, std::forward<Args>(args)...);
143 template<
typename... Args>
144 static inline std::system_error
147 return FormatErrno(errno, fmt, std::forward<Args>(args)...);
155 return e.code().category() == std::system_category() &&
156 e.code().value() == ERROR_FILE_NOT_FOUND;
159 e.code().value() == ENOENT;
168 return e.code().category() == std::system_category() &&
169 e.code().value() == ERROR_PATH_NOT_FOUND;
172 e.code().value() == ENOTDIR;
181 return e.code().category() == std::system_category() &&
182 e.code().value() == ERROR_ACCESS_DENIED;
185 e.code().value() == EACCES;
static std::system_error MakeErrno(int code, const char *msg)
static const std::error_category & ErrnoCategory()
Returns the error_category to be used to wrap errno values.
static gcc_pure bool IsAccessDenied(const std::system_error &e)
static gcc_pure bool IsPathNotFound(const std::system_error &e)
static std::system_error FormatErrno(int code, const char *fmt, Args &&...args)
static gcc_pure bool IsFileNotFound(const std::system_error &e)
static std::system_error FormatSystemError(std::error_code code, const char *fmt, Args &&...args)