From 49b741c94f5db6ec653f902f5410eace53c5ba6d Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Sun, 1 Oct 2023 22:56:45 +0200 Subject: [PATCH] Replace errno_error with std::system_error Signed-off-by: Simon Rozman --- UnitTests/pch.hpp | 3 +- include/stdex/errno.hpp | 77 ------------------------------------- include/stdex/exception.hpp | 3 +- include/stdex/stream.hpp | 40 ++++++++++--------- include/stdex/system.hpp | 23 ++++++++--- include/stdex/unicode.hpp | 12 +++--- 6 files changed, 48 insertions(+), 110 deletions(-) delete mode 100644 include/stdex/errno.hpp diff --git a/UnitTests/pch.hpp b/UnitTests/pch.hpp index 5051ccf9f..b674b4bfd 100644 --- a/UnitTests/pch.hpp +++ b/UnitTests/pch.hpp @@ -1,4 +1,4 @@ -/* +/* SPDX-License-Identifier: MIT Copyright © 2023 Amebis */ @@ -7,7 +7,6 @@ #include #include -#include #include #include #include diff --git a/include/stdex/errno.hpp b/include/stdex/errno.hpp deleted file mode 100644 index 2bab3b136..000000000 --- a/include/stdex/errno.hpp +++ /dev/null @@ -1,77 +0,0 @@ -/* - SPDX-License-Identifier: MIT - Copyright © 2023 Amebis -*/ - -#pragma once - -#include "compat.hpp" -#include -#include - -namespace stdex -{ - /// - /// Standard C runtime library error - /// - class errno_error : public std::runtime_error - { - public: - /// - /// Constructs an exception - /// - /// \param[in] num Numeric error code - /// \param[in] msg Error message - /// - errno_error(_In_ errno_t num, _In_ const std::string& msg) : - m_num(num), - runtime_error(msg) - { - } - - /// - /// Constructs an exception - /// - /// \param[in] num Numeric error code - /// \param[in] msg Error message - /// - errno_error(_In_ errno_t num, _In_opt_z_ const char *msg = nullptr) : - m_num(num), - runtime_error(msg) - { - } - - /// - /// Constructs an exception using `GetLastError()` - /// - /// \param[in] msg Error message - /// - errno_error(_In_ const std::string& msg) : - m_num(errno), - runtime_error(msg) - { - } - - /// - /// Constructs an exception using `GetLastError()` - /// - /// \param[in] msg Error message - /// - errno_error(_In_opt_z_ const char *msg = nullptr) : - m_num(errno), - runtime_error(msg) - { - } - - /// - /// Returns the error number - /// - errno_t number() const - { - return m_num; - } - - protected: - errno_t m_num; ///< Numeric error code - }; -} \ No newline at end of file diff --git a/include/stdex/exception.hpp b/include/stdex/exception.hpp index aab40c38b..c5afa04ed 100644 --- a/include/stdex/exception.hpp +++ b/include/stdex/exception.hpp @@ -21,7 +21,6 @@ namespace stdex /// /// \param[in] msg Error message /// - user_cancelled(_In_opt_z_ const char* msg = "operation cancelled") : runtime_error(msg) - {} + user_cancelled(_In_opt_z_ const char* msg = "operation cancelled") : runtime_error(msg) {} }; } diff --git a/include/stdex/stream.hpp b/include/stdex/stream.hpp index 0315dff16..b05bfc303 100644 --- a/include/stdex/stream.hpp +++ b/include/stdex/stream.hpp @@ -210,7 +210,7 @@ namespace stdex uint8_t byte; if (read_array(&byte, sizeof(byte), 1) == 1) return byte; - throw std::runtime_error("failed to read"); + throw std::system_error(sys_error(), std::system_category(), "failed to read"); } /// @@ -846,10 +846,10 @@ namespace stdex if (!sa) throw std::runtime_error("SafeArrayCreateVector failed"); safearray_accessor a(sa.get()); - if (seek(0) != 0) - throw std::runtime_error("failed to seek"); + if (seek(0) != 0) _Unlikely_ + throw std::system_error(sys_error(), std::system_category(), "failed to seek"); if (read_array(a.data(), 1, length) != length) - throw std::runtime_error("failed to read"); + throw std::system_error(sys_error(), std::system_category(), "failed to read"); return sa.release(); } #endif @@ -861,29 +861,29 @@ namespace stdex /// charset_id read_charset(_In_ charset_id default_charset = charset_id::system) { - if (seek(0) != 0) - throw std::runtime_error("failed to seek"); + if (seek(0) != 0) _Unlikely_ + throw std::system_error(sys_error(), std::system_category(), "failed to seek"); char32_t id_utf32; read_array(&id_utf32, sizeof(char32_t), 1); if (ok() && id_utf32 == utf32_bom) return charset_id::utf32; - if (seek(0) != 0) - throw std::runtime_error("failed to seek"); + if (seek(0) != 0) _Unlikely_ + throw std::system_error(sys_error(), std::system_category(), "failed to seek"); char16_t id_utf16; read_array(&id_utf16, sizeof(char16_t), 1); if (ok() && id_utf16 == utf16_bom) return charset_id::utf16; - if (seek(0) != 0) - throw std::runtime_error("failed to seek"); + if (seek(0) != 0) _Unlikely_ + throw std::system_error(sys_error(), std::system_category(), "failed to seek"); char id_utf8[3] = { 0 }; read_array(id_utf8, sizeof(id_utf8), 1); if (ok() && strncmp(id_utf8, _countof(id_utf8), utf8_bom, _countof(utf8_bom)) == 0) return charset_id::utf8; - if (seek(0) != 0) - throw std::runtime_error("failed to seek"); + if (seek(0) != 0) _Unlikely_ + throw std::system_error(sys_error(), std::system_category(), "failed to seek"); return default_charset; } }; @@ -1716,7 +1716,7 @@ namespace stdex if (m_source) { flush_cache(); if (!ok()) _Unlikely_ - throw std::runtime_error("cache flush failed"); // Data loss occured + throw std::system_error(sys_error(), std::system_category(), "failed to flush cache"); // Data loss occured m_source->seek(m_offset); m_source = nullptr; } @@ -1739,7 +1739,7 @@ namespace stdex if (m_source) { flush_cache(); if (!ok()) _Unlikely_ - throw std::runtime_error("cache flush failed"); // Data loss occured + throw std::system_error(sys_error(), std::system_category(), "failed to flush cache"); // Data loss occured m_source->seek(m_offset); } } @@ -1863,7 +1863,7 @@ namespace stdex { invalidate_cache(); if (!ok()) _Unlikely_ - throw std::runtime_error("cache flush failed"); // Data loss occured + throw std::system_error(sys_error(), std::system_category(), "failed to flush cache"); // Data loss occured m_source->close(); m_state = m_source->state(); } @@ -2741,8 +2741,10 @@ namespace stdex tp2ft(date, ft); if (SetFileTime(m_h, &ft, nullptr, nullptr)) return; + throw std::system_error(GetLastError(), std::system_category(), "SetFileTime failed"); +#else + throw std::runtime_error("not supported"); #endif - throw std::runtime_error("failed to set file ctime"); } virtual void set_atime(time_point date) @@ -2753,6 +2755,7 @@ namespace stdex tp2ft(date, ft); if (SetFileTime(m_h, nullptr, &ft, nullptr)) return; + throw std::system_error(GetLastError(), std::system_category(), "SetFileTime failed"); #else struct timespec ts[2] = { { date.time_since_epoch().count(), 0 }, @@ -2760,8 +2763,8 @@ namespace stdex }; if (futimens(m_h, ts) >= 0) return; + throw std::system_error(errno, std::system_category(), "futimens failed"); #endif - throw std::runtime_error("failed to set file atime"); } virtual void set_mtime(time_point date) @@ -2771,6 +2774,7 @@ namespace stdex tp2ft(date, ft); if (SetFileTime(m_h, nullptr, nullptr, &ft)) return; + throw std::system_error(GetLastError(), std::system_category(), "SetFileTime failed"); #else struct timespec ts[2] = { { 0, UTIME_OMIT }, @@ -2778,8 +2782,8 @@ namespace stdex }; if (futimens(m_h, ts) >= 0) return; + throw std::system_error(errno, std::system_category(), "futimens failed"); #endif - throw std::runtime_error("failed to set file mtime"); } /// diff --git a/include/stdex/system.hpp b/include/stdex/system.hpp index 0e24e0853..d30387f16 100644 --- a/include/stdex/system.hpp +++ b/include/stdex/system.hpp @@ -55,6 +55,15 @@ namespace stdex const sys_handle invalid_handle = (sys_handle)-1; #endif + /// + /// Last operation error + /// +#if defined(_WIN32) + inline DWORD sys_error() { return GetLastError(); } +#else + inline int sys_error() { return errno; } +#endif + /// /// Character type for system functions /// @@ -153,11 +162,13 @@ namespace stdex { #ifdef _WIN32 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE) + return; + throw std::system_error(GetLastError(), std::system_category(), "CloseHandle failed"); #else if (::close(h) >= 0 || errno == EBADF) -#endif return; - throw std::runtime_error("failed to close handle"); + throw std::system_error(errno, std::system_category(), "close failed"); +#endif } /// @@ -169,12 +180,14 @@ namespace stdex #ifdef _WIN32 HANDLE process = GetCurrentProcess(); if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS)) + return h_new; + throw std::system_error(GetLastError(), std::system_category(), "DuplicateHandle failed"); #else _Unreferenced_(inherit); if ((h_new = dup(h)) >= 0) -#endif return h_new; - throw std::runtime_error("failed to duplicate handle"); + throw std::system_error(errno, std::system_category(), "dup failed"); +#endif } protected: @@ -190,7 +203,7 @@ namespace stdex { HRESULT hr = SafeArrayAccessData(sa, reinterpret_cast(&m_data)); if (FAILED(hr)) - throw std::invalid_argument("SafeArrayAccessData failed"); + throw std::system_error(hr, std::system_category(), "SafeArrayAccessData failed"); } ~safearray_accessor() diff --git a/include/stdex/unicode.hpp b/include/stdex/unicode.hpp index 9e9ded955..549287e83 100644 --- a/include/stdex/unicode.hpp +++ b/include/stdex/unicode.hpp @@ -69,7 +69,7 @@ namespace stdex #else m_handle = iconv_open(to_encoding(to), to_encoding(from)); if (m_handle == (iconv_t)-1) - throw std::runtime_error("iconv_open failed"); + throw std::system_error(errno, std::system_category(), "iconv_open failed"); #endif } @@ -131,7 +131,7 @@ namespace stdex dst.append(reinterpret_cast(szBuffer.get()), count_src != SIZE_MAX ? wcsnlen(szBuffer.get(), cch) : static_cast(cch) - 1); return; } - throw std::runtime_error("MultiByteToWideChar failed"); + throw std::system_error(GetLastError(), std::system_category(), "MultiByteToWideChar failed"); } if _Constexpr_ (sizeof(T_from) == sizeof(wchar_t) && sizeof(T_to) == sizeof(char)) { @@ -154,7 +154,7 @@ namespace stdex dst.append(reinterpret_cast(szBuffer.get()), count_src != SIZE_MAX ? strnlen(szBuffer.get(), cch) : static_cast(cch) - 1); return; } - throw std::runtime_error("WideCharToMultiByte failed"); + throw std::system_error(GetLastError(), std::system_category(), "WideCharToMultiByte failed"); } if _Constexpr_ (sizeof(T_from) == sizeof(char) && sizeof(T_to) == sizeof(char)) { @@ -186,7 +186,7 @@ namespace stdex dst.append(reinterpret_cast(szBufferWCMB.get()), strnlen(szBufferWCMB.get(), cch)); return; } - throw std::runtime_error("WideCharToMultiByte failed"); + throw std::system_error(GetLastError(), std::system_category(), "WideCharToMultiByte failed"); } if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Query the required output size. Allocate buffer. Then convert again. @@ -202,7 +202,7 @@ namespace stdex dst.append(reinterpret_cast(szBufferWCMB.get()), strnlen(szBufferWCMB.get(), cch)); return; } - throw std::runtime_error("MultiByteToWideChar failed"); + throw std::system_error(GetLastError(), std::system_category(), "MultiByteToWideChar failed"); } #else dst.reserve(dst.size() + count_src); @@ -218,7 +218,7 @@ namespace stdex break; if (errno == E2BIG) continue; - throw std::runtime_error("iconv failed"); + throw std::system_error(errno, std::system_category(), "iconv failed"); } #endif }