Skip to content

Commit

Permalink
Fix: DLL Export Warning (#2)
Browse files Browse the repository at this point in the history
* replaced exception subclass with std::runtime_error
  • Loading branch information
johnpatek authored Nov 28, 2024
1 parent 020e512 commit 349a179
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 77 deletions.
27 changes: 1 addition & 26 deletions include/sigfn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "sigfn.h"

#include <stdexcept>
#include <functional>
#include <string>
#include <unordered_map>
Expand All @@ -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
*
Expand Down
25 changes: 7 additions & 18 deletions src/sigfn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int,std::string> 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 <class F, class... Arg>
static void sigfn_wrapped_call(F &&function, Arg... args)
Expand All @@ -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));
}
}

Expand Down
3 changes: 1 addition & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
add_test(NAME test_cpp_reset COMMAND unit RESET_CPP)
37 changes: 6 additions & 31 deletions tests/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -78,7 +76,7 @@ int main(int argc, const char **argv)
try
{
const std::function<void()> &unit_test = unit_tests.at(argv[1]);
unit_test();
unit_test();
}
catch (const std::exception &exception)
{
Expand Down Expand Up @@ -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);

Expand All @@ -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);
});
Expand All @@ -230,39 +228,16 @@ 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);
});
try_catch_assert(-1, true);
try_catch_assert(SIGINT, false);
}

void test_cpp_exception()
{
const std::function<void(int, const std::string &)> 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;
Expand Down

0 comments on commit 349a179

Please sign in to comment.