diff --git a/include/sigfn.hpp b/include/sigfn.hpp index 98f7506..fd1eee6 100644 --- a/include/sigfn.hpp +++ b/include/sigfn.hpp @@ -31,6 +31,7 @@ #include "sigfn.h" +#include #include #include #include @@ -47,32 +48,6 @@ namespace sigfn const std::string INVALID_SIGNUM = "sigfn: invalid signal code"; const std::string INVALID_HANDLER = "sigfn: invalid signal handler"; - /** - * @brief sigfn exception - * - * @class exception allows sigfn error codes to be thrown as exceptions - */ - class DLL_EXPORT exception final : public std::exception - { - private: - std::string _error_message; - - public: - /** - * @brief create an exception based on a status code - * - * @param status error code that maps to an error message - */ - exception(int status); - - /** - * @brief what override for std::exception - * - * @returns null-terminated char buffer containing error message - */ - const char *what() const noexcept override; - }; - /** * @brief attach handler to specific signal using copy semantics * diff --git a/src/sigfn.cpp b/src/sigfn.cpp index 9c4b1a4..6bc389c 100644 --- a/src/sigfn.cpp +++ b/src/sigfn.cpp @@ -22,26 +22,15 @@ #include "sigfn.hpp" -sigfn::exception::exception(int status) +static std::string error_message(int status) { - if (status == SIGFN_INVALID_SIGNUM) - { - _error_message = sigfn::INVALID_SIGNUM; - } - else if (status == SIGFN_INVALID_HANDLER) - { - _error_message = sigfn::INVALID_HANDLER; - } - else - { - _error_message = ""; - } + const static std::unordered_map error_messages = { + {SIGFN_INVALID_SIGNUM, sigfn::INVALID_SIGNUM}, + {SIGFN_INVALID_HANDLER, sigfn::INVALID_HANDLER} + }; + return error_messages.at(status); } -char const* sigfn::exception::what() const noexcept -{ - return _error_message.c_str(); -} template static void sigfn_wrapped_call(F &&function, Arg... args) @@ -53,7 +42,7 @@ static void sigfn_wrapped_call(F &&function, Arg... args) if (status != SIGFN_SUCCESS) { - throw sigfn::exception(status); + throw std::runtime_error(error_message(status)); } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e51a0e5..43a44b3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -26,5 +26,4 @@ add_test(NAME test_c_ignore COMMAND unit IGNORE_C) add_test(NAME test_c_reset COMMAND unit RESET_C) add_test(NAME test_cpp_handle COMMAND unit HANDLE_CPP) add_test(NAME test_cpp_ignore COMMAND unit IGNORE_CPP) -add_test(NAME test_cpp_reset COMMAND unit RESET_CPP) -add_test(NAME test_cpp_exception COMMAND unit EXCEPTION_CPP) \ No newline at end of file +add_test(NAME test_cpp_reset COMMAND unit RESET_CPP) \ No newline at end of file diff --git a/tests/unit.cpp b/tests/unit.cpp index 44dd809..60c4dbb 100644 --- a/tests/unit.cpp +++ b/tests/unit.cpp @@ -56,7 +56,6 @@ static void test_c_reset(); static void test_cpp_handle(); static void test_cpp_ignore(); static void test_cpp_reset(); -static void test_cpp_exception(); static void echo_signum(int signum, void *userdata); // GCOV_EXCL_START @@ -69,7 +68,6 @@ int main(int argc, const char **argv) {"HANDLE_CPP", test_cpp_handle}, {"IGNORE_CPP", test_cpp_ignore}, {"RESET_CPP", test_cpp_reset}, - {"EXCEPTION_CPP", test_cpp_exception}, }; int result; result = PASS; @@ -78,7 +76,7 @@ int main(int argc, const char **argv) try { const std::function &unit_test = unit_tests.at(argv[1]); - unit_test(); + unit_test(); } catch (const std::exception &exception) { @@ -179,7 +177,7 @@ void test_cpp_handle() } catch (const std::exception &e) { - flag = -1; + flag = (e.what() != nullptr) ? -1 : flag; } TEST_ASSERT(flag == -1); @@ -206,9 +204,9 @@ void test_cpp_ignore() { sigfn::ignore(signum); } - catch (const std::exception &exception) + catch (const std::exception &e) { - has_error = true; + has_error = (e.what() != nullptr); } TEST_ASSERT(expect_error == has_error); }); @@ -230,9 +228,9 @@ void test_cpp_reset() { sigfn::reset(signum); } - catch (const std::exception &exception) + catch (const std::exception &e) { - has_error = true; + has_error = (e.what() != nullptr); } TEST_ASSERT(expect_error == has_error); }); @@ -240,29 +238,6 @@ void test_cpp_reset() try_catch_assert(SIGINT, false); } -void test_cpp_exception() -{ - const std::function try_catch_assert( - []( - int status, - const std::string &expected_message) - { - std::string message("replaced by error messgae"); - try - { - throw sigfn::exception(status); - } - catch (const std::exception &exception) - { - message = exception.what(); - } - TEST_ASSERT(expected_message == message); - }); - try_catch_assert(SIGFN_SUCCESS, ""); - try_catch_assert(SIGFN_INVALID_SIGNUM, sigfn::INVALID_SIGNUM); - try_catch_assert(SIGFN_INVALID_HANDLER, sigfn::INVALID_HANDLER); -} - void echo_signum(int signum, void *userdata) { *(int *)userdata = signum;