upd: [imp] modify interface of the system header file

This commit is contained in:
mutouyun 2022-12-18 18:16:08 +08:00
parent e60f856295
commit fc6f28a886
7 changed files with 49 additions and 87 deletions

View File

@ -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<std::int64_t> 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_

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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

View File

@ -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)) {

View File

@ -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";
}