From e1ac6d7cfe2d611bac4d0a519b19eeb88bf60e5f Mon Sep 17 00:00:00 2001 From: Francisco Paisana Date: Mon, 27 Apr 2020 12:33:13 +0100 Subject: [PATCH] use macro to disable throw --- lib/include/srslte/common/fsm.h | 2 +- lib/include/srslte/common/move_callback.h | 10 ++++++++- lib/include/srslte/common/type_utils.h | 26 +++++++++++++++-------- lib/test/common/choice_type_test.cc | 10 --------- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/include/srslte/common/fsm.h b/lib/include/srslte/common/fsm.h index 13d54264c..77b7c5c40 100644 --- a/lib/include/srslte/common/fsm.h +++ b/lib/include/srslte/common/fsm.h @@ -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: diff --git a/lib/include/srslte/common/move_callback.h b/lib/include/srslte/common/move_callback.h index d7d06d520..9e522a0f3 100644 --- a/lib/include/srslte/common/move_callback.h +++ b/lib/include/srslte/common/move_callback.h @@ -26,6 +26,14 @@ #include #include +#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" to store functors without calling "new" @@ -54,7 +62,7 @@ class empty_table_t : public oper_table_t { 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; } diff --git a/lib/include/srslte/common/type_utils.h b/lib/include/srslte/common/type_utils.h index 3ce2d863e..b7e14c5bf 100644 --- a/lib/include/srslte/common/type_utils.h +++ b/lib/include/srslte/common/type_utils.h @@ -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 { } // 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 constexpr size_t get_type_index() @@ -201,7 +209,7 @@ T& get(TypeContainer& c) if (c.template is()) { return c.template get_unchecked(); } - throw bad_type_access{"in get"}; + THROW_BAD_ACCESS("in get"); } template @@ -210,7 +218,7 @@ const T& get(const TypeContainer& c) if (c.template is()) { return c.template get_unchecked(); } - throw bad_type_access{"in get"}; + THROW_BAD_ACCESS("in get"); } template () should throw TESTASSERT(srslte::get_if(c2) == nullptr); - bool catched = false; - try { - char n = '1'; - n = srslte::get(c2); - TESTASSERT(n == '1'); - } catch (bad_type_access& e) { - catched = true; - } - TESTASSERT(catched); // TEST: simple emplace after construction c2 = 'c';