From e51a0a7a523cea1942bc925f9b03a94a45c78f8b Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Thu, 11 Apr 2019 22:12:26 +1000 Subject: [PATCH] AP_Common: make Bitmask a template on number of bits --- libraries/AP_Common/Bitmask.cpp | 24 ---------------------- libraries/AP_Common/Bitmask.h | 13 ++++++------ libraries/AP_Common/tests/test_bitmask.cpp | 8 ++++---- 3 files changed, 11 insertions(+), 34 deletions(-) delete mode 100644 libraries/AP_Common/Bitmask.cpp diff --git a/libraries/AP_Common/Bitmask.cpp b/libraries/AP_Common/Bitmask.cpp deleted file mode 100644 index 9af9a0c3ca..0000000000 --- a/libraries/AP_Common/Bitmask.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "Bitmask.h" - -#include - -Bitmask &Bitmask::operator=(const Bitmask&other) -{ - if (other.numwords != numwords || other.numbits != numbits) { -#if CONFIG_HAL_BOARD == HAL_BOARD_SITL - // we really should not be assigning from a bitmask of a - // different number of bits! - AP_HAL::panic("attempt to copy from bitmask of different size"); -#endif - // ... but try to cope if it happens in real life: - if (numwords != other.numwords) { - delete bits; - bits = new uint32_t[numwords]; - numwords = other.numwords; - } - numbits = other.numbits; - } - memcpy(bits, other.bits, sizeof(bits[0])*other.numwords); - - return *this; -} diff --git a/libraries/AP_Common/Bitmask.h b/libraries/AP_Common/Bitmask.h index 70870c6271..dfe179fbe9 100644 --- a/libraries/AP_Common/Bitmask.h +++ b/libraries/AP_Common/Bitmask.h @@ -21,19 +21,20 @@ #include #include +template class Bitmask { public: - Bitmask(uint16_t num_bits) : + Bitmask() : numbits(num_bits), numwords((num_bits+31)/32) { - bits = new uint32_t[numwords]; clearall(); } - ~Bitmask(void) { - delete[] bits; + + Bitmask &operator=(const Bitmask&other) { + memcpy(bits, other.bits, sizeof(bits[0])*other.numwords); + return *this; } - Bitmask &operator=(const Bitmask&other); Bitmask(const Bitmask &other) = delete; // set given bitnumber @@ -129,5 +130,5 @@ public: private: uint16_t numbits; uint16_t numwords; - uint32_t *bits; + uint32_t bits[(num_bits+31)/32]; }; diff --git a/libraries/AP_Common/tests/test_bitmask.cpp b/libraries/AP_Common/tests/test_bitmask.cpp index c4906d6563..971af68d52 100644 --- a/libraries/AP_Common/tests/test_bitmask.cpp +++ b/libraries/AP_Common/tests/test_bitmask.cpp @@ -4,7 +4,7 @@ TEST(Bitmask, Tests) { - Bitmask x{49}; + Bitmask<49> x; EXPECT_EQ(-1, x.first_set()); x.set(5); @@ -40,7 +40,7 @@ TEST(Bitmask, Tests) TEST(Bitmask, SetAll) { - Bitmask x{49}; + Bitmask<49> x; EXPECT_EQ(-1, x.first_set()); EXPECT_EQ(false, x.get(45)); x.setall(); @@ -59,13 +59,13 @@ TEST(Bitmask, SetAll) TEST(Bitmask, Assignment) { - Bitmask x{49}; + Bitmask<49> x; x.set(0); x.set(5); x.set(6); x.set(48); - Bitmask y{49}; + Bitmask<49> y; y = x; x.clear(0); EXPECT_EQ(true, y.get(0));