Add system

This commit is contained in:
mutouyun 2025-01-08 19:33:51 +08:00 committed by 木头云
parent 453f964bdd
commit b70dea04c5
5 changed files with 187 additions and 0 deletions

View File

@ -0,0 +1,33 @@
/**
* \file libipc/system.h
* \author mutouyun (orz@orzz.org)
* \brief Isolation and encapsulation of system APIs.
*/
#pragma once
#include <string>
#include <ostream> // std::ostream
#include <cstdint>
#include "libipc/imp/export.h"
#include "libipc/imp/error.h"
#include "libipc/imp/result.h"
namespace ipc {
namespace sys {
/// \brief A platform-dependent error code.
LIBIPC_EXPORT std::error_code error() noexcept;
/// \enum The name of the `conf()` argument used to inquire about its value.
/// \brief Certain options are supported,
/// or what the value is of certain configurable constants or limits.
enum class info : std::int32_t {
page_size,
};
/// \brief Get system configuration information at run time.
LIBIPC_EXPORT result<std::int64_t> conf(info) noexcept;
} // namespace sys
} // namespace ipc

View File

@ -0,0 +1,7 @@
#include "libipc/imp/detect_plat.h"
#if defined(LIBIPC_OS_WIN)
# include "libipc/platform/win/system.h"
#else
# include "libipc/platform/posix/system.h"
#endif

View File

@ -0,0 +1,49 @@
/**
* \file libipc/platform/posix/system.h
* \author mutouyun (orz@orzz.org)
*/
#pragma once
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "libipc/imp/system.h"
#include "libipc/imp/log.h"
namespace ipc {
namespace sys {
/**
* \brief Get the system error number.
* \see https://en.cppreference.com/w/cpp/error/generic_category
* https://man7.org/linux/man-pages/man3/errno.3.html
*/
std::error_code error() noexcept {
return std::error_code(errno, std::generic_category());
}
/**
* \brief Gets configuration information at run time
* https://man7.org/linux/man-pages/man2/getpagesize.2.html
* https://man7.org/linux/man-pages/man3/sysconf.3.html
*/
result<std::int64_t> conf(info r) noexcept {
LIBIPC_LOG();
switch (r) {
case info::page_size: {
auto val = ::sysconf(_SC_PAGESIZE);
if (val >= 0) return static_cast<std::int64_t>(val);
break;
}
default:
log.error("invalid info = ", underlyof(r));
return std::make_error_code(std::errc::invalid_argument);
}
auto err = sys::error();
log.error("info = ", underlyof(r), ", error = ", err);
return err;
}
} // namespace sys
} // namespace ipc

View File

@ -0,0 +1,88 @@
/**
* \file libipc/platform/win/system.h
* \author mutouyun (orz@orzz.org)
*/
#pragma once
#include <exception>
#include <type_traits>
#include <Windows.h>
#include <tchar.h>
#include "libipc/imp/system.h"
#include "libipc/imp/log.h"
#include "libipc/imp/codecvt.h"
#include "libipc/imp/generic.h"
#include "libipc/imp/detect_plat.h"
#include "libipc/imp/scope_exit.h"
namespace ipc {
namespace sys {
/**
* \brief Gets a text description of the system error
* \see https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessage
*/
std::string error_string(DWORD code) noexcept {
LIBIPC_LOG();
LIBIPC_TRY {
LPTSTR lpErrText = NULL;
if (::FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpErrText,
0, NULL) == 0) {
log.error("failed: FormatMessage(dwMessageId = ", code, "). error = ", ::GetLastError());
return {};
}
LIBIPC_SCOPE_EXIT(finally) = [lpErrText] { ::LocalFree(lpErrText); };
std::size_t msg_len = ::_tcslen(lpErrText);
std::size_t len = cvt_cstr(lpErrText, msg_len, (char *)nullptr, 0);
if (len == 0) {
return {};
}
std::string ret(len, '\0');
cvt_cstr(lpErrText, msg_len, &ret[0], ret.size());
return ret;
} LIBIPC_CATCH(...) {
log.error("failed: FormatMessage(dwMessageId = ", code, ").",
"\n\texception: ", log::exception_string(std::current_exception()));
}
return {};
}
/**
* \brief Get the system error number.
* \see https://en.cppreference.com/w/cpp/error/system_category
* https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror
*/
std::error_code error() noexcept {
return std::error_code(::GetLastError(), std::system_category());
}
/**
* \brief Retrieves information about the current system.
* \see https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsysteminfo
* https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getnativesysteminfo
*/
result<std::int64_t> conf(info r) noexcept {
LIBIPC_LOG();
switch (r) {
case info::page_size: {
::SYSTEM_INFO info {};
::GetNativeSystemInfo(&info);
return (std::int64_t)info.dwPageSize;
}
default:
log.error("invalid info = ", underlyof(r));
return std::make_error_code(std::errc::invalid_argument);
}
}
} // namespace sys
} // namespace ipc

View File

@ -0,0 +1,10 @@
#include "test.h"
#include "libipc/imp/system.h"
TEST(system, conf) {
auto ret = ipc::sys::conf(ipc::sys::info::page_size);
EXPECT_TRUE(ret);
EXPECT_EQ(ret.value(), 4096);
}