diff --git a/lib/include/srslte/adt/adt_utils.h b/lib/include/srslte/adt/adt_utils.h index aefd52f90..853c91be1 100644 --- a/lib/include/srslte/adt/adt_utils.h +++ b/lib/include/srslte/adt/adt_utils.h @@ -22,9 +22,14 @@ #ifndef SRSLTE_ADT_UTILS_H #define SRSLTE_ADT_UTILS_H +#ifdef __EXCEPTIONS + +#include + +#define EXCEPTIONS_ENABLED 1 + namespace srslte { -#if defined(__cpp_exceptions) && (1 == __cpp_exceptions) class bad_type_access : public std::runtime_error { public: @@ -32,13 +37,25 @@ public: explicit bad_type_access(const char* what_arg) : runtime_error(what_arg) {} }; -#define THROW_BAD_ACCESS(msg) throw bad_type_access{msg}; +#define THROW_BAD_ACCESS(msg) throw bad_type_access(msg) + +} // namespace srslte + #else + +#define EXCEPTIONS_ENABLED 0 + +#include +#include + +namespace srslte { + #define THROW_BAD_ACCESS(msg) \ - fprintf(stderr, "ERROR: exception thrown with %s", msg); \ + std::fprintf(stderr, "ERROR: exception thrown with %s", msg); \ std::abort() -#endif } // namespace srslte +#endif + #endif // SRSLTE_ADT_UTILS_H diff --git a/lib/include/srslte/adt/bounded_bitset.h b/lib/include/srslte/adt/bounded_bitset.h index 9a88acb06..74cb5f086 100644 --- a/lib/include/srslte/adt/bounded_bitset.h +++ b/lib/include/srslte/adt/bounded_bitset.h @@ -49,7 +49,7 @@ public: size_t size() const noexcept { return cur_size; } - void resize(size_t new_size) noexcept + void resize(size_t new_size) { if (new_size > max_size()) { std::string msg = @@ -194,7 +194,7 @@ public: bool operator!=(const bounded_bitset& other) const noexcept { return not(*this == other); } - bounded_bitset& operator|=(const bounded_bitset& other) noexcept + bounded_bitset& operator|=(const bounded_bitset& other) { if (other.size() != size()) { std::string msg = "operator|= called for bitsets of different sizes (" + std::to_string(size()) + @@ -207,7 +207,7 @@ public: return *this; } - bounded_bitset& operator&=(const bounded_bitset& other) noexcept + bounded_bitset& operator&=(const bounded_bitset& other) { if (other.size() != size()) { std::string msg = "operator&= called for bitsets of different sizes (" + std::to_string(size()) + @@ -247,7 +247,7 @@ public: return s; } - uint64_t to_uint64() const noexcept + uint64_t to_uint64() const { if (nof_words_() > 1) { std::string msg = "ERROR: cannot convert bitset of size=" + std::to_string(size()) + " to uint64_t"; diff --git a/lib/test/adt/bounded_bitset_test.cc b/lib/test/adt/bounded_bitset_test.cc index a63ed4a5c..8d094936a 100644 --- a/lib/test/adt/bounded_bitset_test.cc +++ b/lib/test/adt/bounded_bitset_test.cc @@ -98,6 +98,18 @@ int test_bitset_bitwise_oper() TESTASSERT(mask != mask2); TESTASSERT(mask2.test(10) and not mask2.test(11)); +#if EXCEPTIONS_ENABLED + bool caught = false; + mask2.resize(24); + try { + mask2 |= mask; + } catch (srslte::bad_type_access& c) { + printf("Received exception \"%s\"\n", c.what()); + caught = true; + } + TESTASSERT(caught); +#endif + return SRSLTE_SUCCESS; }