From fc6f28a8868cb0c921046cf961535d9e5e360ac4 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sun, 18 Dec 2022 18:16:08 +0800 Subject: [PATCH] upd: [imp] modify interface of the system header file --- include/libimp/system.h | 41 ++++++--------------- src/libimp/platform/posix/system.h | 8 ++--- src/libimp/platform/win/codecvt.h | 4 +-- src/libimp/platform/win/system.h | 8 ++--- src/libimp/system.cpp | 57 +++++++++++------------------- src/libipc/platform/win/get_sa.h | 3 +- test/imp/test_imp_system.cpp | 15 ++++---- 7 files changed, 49 insertions(+), 87 deletions(-) diff --git a/include/libimp/system.h b/include/libimp/system.h index fd3edcf..469b520 100644 --- a/include/libimp/system.h +++ b/include/libimp/system.h @@ -12,17 +12,16 @@ #include "libimp/def.h" #include "libimp/export.h" -#include "libimp/result.h" -#include "libimp/fmt_cpo.h" +#include "libimp/error.h" LIBIMP_NAMESPACE_BEG_ namespace sys { /** - * @brief Get/Set the system error code + * @brief Get/Set the system error number */ -LIBIMP_EXPORT result_code error_code() noexcept; -LIBIMP_EXPORT void error_code(result_code) noexcept; +LIBIMP_EXPORT result_code error_no() noexcept; +LIBIMP_EXPORT void error_no(result_code) noexcept; /** * @brief Gets a text description of the system error @@ -30,29 +29,16 @@ LIBIMP_EXPORT void error_code(result_code) noexcept; LIBIMP_EXPORT std::string error_str(result_code) noexcept; /** - * @brief A text description string with an error number attached + * @brief Identifies the operating system error category. + * @see https://en.cppreference.com/w/cpp/error/system_category */ -LIBIMP_EXPORT std::string error_msg(result_code) noexcept; +LIBIMP_EXPORT error_category const &category() noexcept; /** - * @brief The system error object. + * @brief A platform-dependent error code. + * @see https://en.cppreference.com/w/cpp/error/error_code */ -class LIBIMP_EXPORT error { - result_code r_code_; - -public: - explicit error() noexcept; - explicit error(result_code rc) noexcept; - - result_code code() const noexcept; - std::uint64_t value() const noexcept; - explicit operator bool() const noexcept; - - std::string str() const noexcept; - - friend LIBIMP_EXPORT bool operator==(error const &lhs, error const &rhs) noexcept; - friend LIBIMP_EXPORT bool operator!=(error const &lhs, error const &rhs) noexcept; -}; +LIBIMP_EXPORT error_code error() noexcept; /** * @brief Get system configuration information at run time @@ -62,12 +48,5 @@ enum class info : std::int32_t { }; LIBIMP_EXPORT result conf(info) noexcept; -/** - * @brief @brief Custom defined fmt_to method for imp::fmt - */ -inline bool tag_invoke(decltype(::LIBIMP::fmt_to), fmt_context &ctx, error r) noexcept { - return fmt_to(ctx, error_msg(r.code())); -} - } // namespace sys LIBIMP_NAMESPACE_END_ diff --git a/src/libimp/platform/posix/system.h b/src/libimp/platform/posix/system.h index 25e449d..2666ba7 100644 --- a/src/libimp/platform/posix/system.h +++ b/src/libimp/platform/posix/system.h @@ -19,20 +19,20 @@ namespace sys { #endif /** - * @brief Get the system error code + * @brief Get the system error number * https://man7.org/linux/man-pages/man3/errno.3.html */ -result_code error_code() noexcept { +result_code error_no() noexcept { auto err = errno; if (err == ENOERR) return {ENOERR}; return {false, std::uint64_t(err)}; } /** - * @brief Set the system error code + * @brief Set the system error number * https://man7.org/linux/man-pages/man3/errno.3.html */ -void error_code(result_code code) noexcept { +void error_no(result_code code) noexcept { errno = code ? ENOERR : (int)code.value(); } diff --git a/src/libimp/platform/win/codecvt.h b/src/libimp/platform/win/codecvt.h index edcb785..4a8dd85 100644 --- a/src/libimp/platform/win/codecvt.h +++ b/src/libimp/platform/win/codecvt.h @@ -36,7 +36,7 @@ std::size_t cvt_cstr(char const *src, std::size_t slen, wchar_t *des, std::size_ int cch_wc = (des == nullptr) ? 0 : (int)dlen; int size_needed = ::MultiByteToWideChar(CP_ACP, 0, src, (int)slen, des, cch_wc); if (size_needed <= 0) { - log.error("failed: MultiByteToWideChar(CP_ACP). error = ", sys::error_code()); + log.error("failed: MultiByteToWideChar(CP_ACP). error = ", sys::error_no()); } return size_needed; } @@ -51,7 +51,7 @@ std::size_t cvt_cstr(wchar_t const *src, std::size_t slen, char *des, std::size_ int cb_mb = (des == nullptr) ? 0 : (int)dlen; int size_needed = ::WideCharToMultiByte(CP_ACP, 0, src, (int)slen, des, cb_mb, NULL, NULL); if (size_needed <= 0) { - log.error("failed: WideCharToMultiByte(CP_ACP). error = ", sys::error_code()); + log.error("failed: WideCharToMultiByte(CP_ACP). error = ", sys::error_no()); } return size_needed; } diff --git a/src/libimp/platform/win/system.h b/src/libimp/platform/win/system.h index a24763a..19d5861 100644 --- a/src/libimp/platform/win/system.h +++ b/src/libimp/platform/win/system.h @@ -21,10 +21,10 @@ LIBIMP_NAMESPACE_BEG_ namespace sys { /** - * @brief Get the system error code + * @brief Get the system error number * https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror */ -result_code error_code() noexcept { +result_code error_no() noexcept { auto err = ::GetLastError(); if (err == ERROR_SUCCESS) { return {ERROR_SUCCESS}; @@ -33,10 +33,10 @@ result_code error_code() noexcept { } /** - * @brief Set the system error code + * @brief Set the system error number * https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-setlasterror */ -void error_code(result_code code) noexcept { +void error_no(result_code code) noexcept { DWORD err = code ? ERROR_SUCCESS : (DWORD)code.value(); ::SetLastError(err); } diff --git a/src/libimp/system.cpp b/src/libimp/system.cpp index 8dfb15d..d71167b 100644 --- a/src/libimp/system.cpp +++ b/src/libimp/system.cpp @@ -1,4 +1,6 @@ +#include "libimp/system.h" +#include "libimp/fmt.h" #include "libimp/detect_plat.h" #if defined(LIBIMP_OS_WIN) # include "libimp/platform/win/system.h" @@ -6,47 +8,30 @@ # include "libimp/platform/posix/system.h" #endif +namespace { + +class system_error_category : public ::LIBIMP::error_category { +public: + std::string name() const { + return "system"; + } + std::string message(::LIBIMP::result_code r) const { + return ::LIBIMP::fmt(r.value(), ::LIBIMP::sys::error_str(r)); + } +}; + +} // namespace + LIBIMP_NAMESPACE_BEG_ namespace sys { -std::string error_msg(result_code code) noexcept { - LIBIMP_TRY { - return fmt("[", code.value(), ", \"", error_str(code), "\"]"); - } LIBIMP_CATCH(...) { - return error_str(code); - } +error_category const &category() noexcept { + static system_error_category ec; + return ec; } -/// @brief The system error object. - -error::error() noexcept - : error(error_code()) {} - -error::error(result_code rc) noexcept - : r_code_(rc) {} - -result_code error::code() const noexcept { - return r_code_; -} - -std::uint64_t error::value() const noexcept { - return r_code_.value(); -} - -error::operator bool() const noexcept { - return !!r_code_; -} - -std::string error::str() const noexcept { - return error_str(r_code_); -} - -bool operator==(error const &lhs, error const &rhs) noexcept { - return lhs.code() == rhs.code(); -} - -bool operator!=(error const &lhs, error const &rhs) noexcept { - return lhs.code() != rhs.code(); +error_code error() noexcept { + return error_code{error_no(), category()}; } } // namespace sys diff --git a/src/libipc/platform/win/get_sa.h b/src/libipc/platform/win/get_sa.h index 3964188..60a64f8 100644 --- a/src/libipc/platform/win/get_sa.h +++ b/src/libipc/platform/win/get_sa.h @@ -30,7 +30,8 @@ inline LPSECURITY_ATTRIBUTES get_sa() { using namespace ::LIBIMP; log::grip log {"get_sa"}; if (!::InitializeSecurityDescriptor(&sd_, SECURITY_DESCRIPTOR_REVISION)) { - log.error("failed: InitializeSecurityDescriptor(SECURITY_DESCRIPTOR_REVISION). error = ", sys::error()); + log.error("failed: InitializeSecurityDescriptor(SECURITY_DESCRIPTOR_REVISION). " + "error = ", sys::error()); return; } if (!::SetSecurityDescriptorDacl(&sd_, TRUE, NULL, FALSE)) { diff --git a/test/imp/test_imp_system.cpp b/test/imp/test_imp_system.cpp index 81a6f6d..d543596 100644 --- a/test/imp/test_imp_system.cpp +++ b/test/imp/test_imp_system.cpp @@ -14,19 +14,16 @@ #else #endif -TEST(system, error_code) { - imp::sys::error_code({false, 111}); - auto err = imp::sys::error_code(); +TEST(system, error_no) { + imp::sys::error_no({false, 111}); + auto err = imp::sys::error_no(); EXPECT_FALSE(err); EXPECT_EQ(err.value(), 111); - imp::sys::error_code({}); - EXPECT_TRUE(imp::sys::error_code()); + imp::sys::error_no({}); + EXPECT_TRUE(imp::sys::error_no()); - imp::sys::error e_obj {err}; - EXPECT_EQ(err.value(), e_obj.value()); - auto e_msg = imp::fmt(imp::sys::error_msg(imp::sys::error_code())); - EXPECT_EQ(e_msg, imp::fmt(imp::sys::error())); + auto e_msg = imp::sys::error_str(imp::sys::error_no()); std::cout << e_msg << "\n"; }