fix macro checking for exceptions, added missing includes, fixed bounded bitset methods' noexcept specifiers

This commit is contained in:
Francisco Paisana 2020-08-11 19:49:10 +01:00
parent cceeff10de
commit 857cc141c7
3 changed files with 40 additions and 11 deletions

View File

@ -22,9 +22,14 @@
#ifndef SRSLTE_ADT_UTILS_H
#define SRSLTE_ADT_UTILS_H
#ifdef __EXCEPTIONS
#include <stdexcept>
#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};
#else
#define THROW_BAD_ACCESS(msg) \
fprintf(stderr, "ERROR: exception thrown with %s", msg); \
std::abort()
#endif
#define THROW_BAD_ACCESS(msg) throw bad_type_access(msg)
} // namespace srslte
#else
#define EXCEPTIONS_ENABLED 0
#include <cstdio>
#include <cstdlib>
namespace srslte {
#define THROW_BAD_ACCESS(msg) \
std::fprintf(stderr, "ERROR: exception thrown with %s", msg); \
std::abort()
} // namespace srslte
#endif
#endif // SRSLTE_ADT_UTILS_H

View File

@ -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<N, reversed>& other) const noexcept { return not(*this == other); }
bounded_bitset<N, reversed>& operator|=(const bounded_bitset<N, reversed>& other) noexcept
bounded_bitset<N, reversed>& operator|=(const bounded_bitset<N, reversed>& 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<N, reversed>& operator&=(const bounded_bitset<N, reversed>& other) noexcept
bounded_bitset<N, reversed>& operator&=(const bounded_bitset<N, reversed>& 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";

View File

@ -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;
}