From fe60c52c70705b721bee7fbd6e3aae076a3b99c9 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 18 Dec 2015 01:06:12 +0100 Subject: system/Error: add MakeErrno(), MakeLastError() --- src/system/Error.hxx | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'src/system/Error.hxx') diff --git a/src/system/Error.hxx b/src/system/Error.hxx index 948bb47e5..5a10b6844 100644 --- a/src/system/Error.hxx +++ b/src/system/Error.hxx @@ -49,6 +49,19 @@ FormatSystemError(std::error_code code, const char *fmt, Args&&... args) #include +static inline std::system_error +MakeLastError(DWORD code, const char *msg) +{ + return std::system_error(std::error_code(code, std::system_category()), + msg); +} + +static inline std::system_error +MakeLastError(const char *msg) +{ + return MakeLastError(GetLastError(), msg); +} + template static inline std::system_error FormatLastError(DWORD code, const char *fmt, Args&&... args) @@ -64,8 +77,7 @@ FormatLastError(DWORD code, const char *fmt, Args&&... args) FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, code, 0, p, end - p, nullptr); - return std::system_error(std::error_code(code, std::system_category()), - buffer); + return MakeLastError(code, buffer); } template @@ -81,6 +93,19 @@ FormatLastError(const char *fmt, Args&&... args) #include #include +static inline std::system_error +MakeErrno(int code, const char *msg) +{ + return std::system_error(std::error_code(code, std::system_category()), + msg); +} + +static inline std::system_error +MakeErrno(const char *msg) +{ + return MakeErrno(errno, msg); +} + template static inline std::system_error FormatErrno(int code, const char *fmt, Args&&... args) @@ -94,8 +119,7 @@ FormatErrno(int code, const char *fmt, Args&&... args) *p++ = ' '; CopyString(p, strerror(code), end - p); - return std::system_error(std::error_code(code, std::system_category()), - buffer); + return MakeErrno(code, buffer); } template -- cgit v1.2.3