use macro to disable throw

This commit is contained in:
Francisco Paisana 2020-04-27 12:33:13 +01:00 committed by Francisco Paisana
parent 0fdb5f0114
commit e1ac6d7cfe
4 changed files with 27 additions and 21 deletions

View File

@ -519,7 +519,7 @@ protected:
if (is_success()) {
return result;
}
throw bad_type_access{"in proc_fsm_t::get_result"};
THROW_BAD_ACCESS("in proc_fsm_t::get_result");
}
private:

View File

@ -26,6 +26,14 @@
#include <functional>
#include <type_traits>
#if defined(__cpp_exceptions) && (1 == __cpp_exceptions)
#define THROW_BAD_FUNCTION_CALL(const char* cause) throw std::bad_function_call{};
#else
#define THROW_BAD_FUNCTION_CALL(cause) \
fprintf(stderr, "ERROR: exception thrown due to bad function call (cause: %s)\n", cause); \
std::abort()
#endif
namespace srslte {
//! Size of the buffer used by "move_callback<R(Args...)>" to store functors without calling "new"
@ -54,7 +62,7 @@ class empty_table_t : public oper_table_t<R, Args...>
{
public:
constexpr empty_table_t() = default;
R call(void* src, Args&&... args) const final { throw std::bad_function_call(); }
R call(void* src, Args&&... args) const final { THROW_BAD_FUNCTION_CALL("function ptr is empty"); }
void move(void* src, void* dest) const final {}
void dtor(void* src) const final {}
bool is_in_small_buffer() const final { return true; }

View File

@ -30,6 +30,21 @@
namespace srslte {
#if defined(__cpp_exceptions) && (1 == __cpp_exceptions)
class bad_type_access : public std::runtime_error
{
public:
explicit bad_type_access(const std::string& what_arg) : runtime_error(what_arg) {}
explicit bad_type_access(const char* what_arg) : runtime_error(what_arg) {}
};
#define THROW_BAD_ACCESS(msg) throw bad_type_access{msg};
#else
#define THROW_BAD_ACCESS(msg) \
fprintf(stderr, "ERROR: exception thrown at %s", msg); \
std::abort()
#endif
//! Helper to print the name of a type for logging
/**
* @brief Helper function that returns a type name string
@ -156,13 +171,6 @@ struct visit_impl<F, TypeList, First> {
} // namespace type_utils_details
class bad_type_access : public std::runtime_error
{
public:
explicit bad_type_access(const std::string& what_arg) : runtime_error(what_arg) {}
explicit bad_type_access(const char* what_arg) : runtime_error(what_arg) {}
};
//! Get index of T in Types...
template <typename T, typename... Types>
constexpr size_t get_type_index()
@ -201,7 +209,7 @@ T& get(TypeContainer& c)
if (c.template is<T>()) {
return c.template get_unchecked<T>();
}
throw bad_type_access{"in get<T>"};
THROW_BAD_ACCESS("in get<T>");
}
template <typename T, typename TypeContainer>
@ -210,7 +218,7 @@ const T& get(const TypeContainer& c)
if (c.template is<T>()) {
return c.template get_unchecked<T>();
}
throw bad_type_access{"in get<T>"};
THROW_BAD_ACCESS("in get<T>");
}
template <size_t I,

View File

@ -104,7 +104,6 @@ int test_tagged_union()
int test_choice()
{
using srslte::bad_type_access;
using srslte::choice_t;
TESTASSERT(C::counter == 0);
@ -123,15 +122,6 @@ int test_choice()
// TEST: Invalid member access. get<>() should throw
TESTASSERT(srslte::get_if<char>(c2) == nullptr);
bool catched = false;
try {
char n = '1';
n = srslte::get<char>(c2);
TESTASSERT(n == '1');
} catch (bad_type_access& e) {
catched = true;
}
TESTASSERT(catched);
// TEST: simple emplace after construction
c2 = 'c';