From 375c25bd1ee445ddf97c5eb3a40f6db753cc2e23 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sun, 6 Nov 2022 17:57:07 +0800 Subject: [PATCH] upd: [imp] improve interface of system --- include/libimp/system.h | 2 +- src/libimp/platform/posix/system.h | 17 +++++++++++------ test/test_imp_system.cpp | 4 +++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/include/libimp/system.h b/include/libimp/system.h index 0222b2a..dbff599 100644 --- a/include/libimp/system.h +++ b/include/libimp/system.h @@ -61,7 +61,7 @@ public: enum class info : std::int32_t { page_size, }; -LIBIMP_EXPORT std::int64_t conf(info) noexcept; +LIBIMP_EXPORT result conf(info) noexcept; } // namespace sys LIBIMP_NAMESPACE_END_ diff --git a/src/libimp/platform/posix/system.h b/src/libimp/platform/posix/system.h index 3f5f33d..f0e285b 100644 --- a/src/libimp/platform/posix/system.h +++ b/src/libimp/platform/posix/system.h @@ -14,13 +14,17 @@ LIBIMP_NAMESPACE_BEG_ namespace sys { +#ifndef ENOERR +#define ENOERR (0) +#endif + /** * @brief Get the system error code * https://man7.org/linux/man-pages/man3/errno.3.html */ result_code error_code() noexcept { auto err = errno; - if (err == 0) return {true}; + if (err == ENOERR) return {ENOERR}; return {false, std::uint64_t(err)}; } @@ -29,7 +33,7 @@ result_code error_code() noexcept { * https://man7.org/linux/man-pages/man3/errno.3.html */ void error_code(result_code code) noexcept { - errno = code ? 0 : (int)code.value(); + errno = code ? ENOERR : (int)code.value(); } /** @@ -56,7 +60,7 @@ std::string error_str(result_code code) noexcept { * https://man7.org/linux/man-pages/man2/getpagesize.2.html * https://man7.org/linux/man-pages/man3/sysconf.3.html */ -std::int64_t conf(info r) noexcept { +result conf(info r) noexcept { LIBIMP_LOG_(); switch (r) { case info::page_size: { @@ -66,10 +70,11 @@ std::int64_t conf(info r) noexcept { } default: log.error("invalid info = {}", enum_cast(r)); - return -1; + return {}; } - log.error("info = {}, error = {}", enum_cast(r), error_msg(error_code())); - return -1; + auto err = sys::error(); + log.error("info = {}, error = {}", enum_cast(r), err); + return {false, (int)err.value()}; } } // namespace sys diff --git a/test/test_imp_system.cpp b/test/test_imp_system.cpp index ff8832e..d4128ed 100644 --- a/test/test_imp_system.cpp +++ b/test/test_imp_system.cpp @@ -67,5 +67,7 @@ TEST(system, error_str) { } TEST(system, conf) { - EXPECT_EQ(imp::sys::conf(imp::sys::info::page_size), 4096); + auto ret = imp::sys::conf(imp::sys::info::page_size); + EXPECT_TRUE(ret); + EXPECT_EQ(ret.value(), 4096); } \ No newline at end of file