From d46c03b588a5fc403adc8f8340cd01ff9c28d335 Mon Sep 17 00:00:00 2001 From: Simon Lin Date: Wed, 29 Jul 2020 00:09:52 +0800 Subject: [PATCH] [bit-vector] refactor methods (#5302) This commit refactors BitVector to use one method for setting or clearing masks. --- src/core/common/bit_vector.hpp | 33 +++++++++++---------------------- src/core/common/message.cpp | 4 ++-- src/core/thread/topology.cpp | 23 ++++------------------- 3 files changed, 17 insertions(+), 43 deletions(-) diff --git a/src/core/common/bit_vector.hpp b/src/core/common/bit_vector.hpp index edddb3bd3..52b225a88 100644 --- a/src/core/common/bit_vector.hpp +++ b/src/core/common/bit_vector.hpp @@ -59,7 +59,7 @@ namespace ot { * @tparam N Specifies the number of bits. * */ -template class BitVector : public Equatable> +template class BitVector : public Equatable>, public Clearable> { public: /** @@ -81,34 +81,23 @@ public: * This method sets the mask of a given index. * * @param[in] aIndex The index. + * @param[in] aValue TRUE to set the mask, or FALSE to clear the mask. * */ - void Set(uint16_t aIndex) + void Set(uint16_t aIndex, bool aValue) { OT_ASSERT(aIndex < N); - mMask[aIndex / 8] |= 0x80 >> (aIndex % 8); + if (aValue) + { + mMask[aIndex / 8] |= 0x80 >> (aIndex % 8); + } + else + { + mMask[aIndex / 8] &= ~(0x80 >> (aIndex % 8)); + }; } - /** - * This method clears the mask of a given index. - * - * @param[in] aIndex The index. - * - */ - void Clear(uint16_t aIndex) - { - OT_ASSERT(aIndex < N); - - mMask[aIndex / 8] &= ~(0x80 >> (aIndex % 8)); - } - - /** - * This method clears all indexes. - * - */ - void ClearAll(void) { memset(mMask, 0, sizeof(mMask)); } - /** * This method returns if any mask is set. * diff --git a/src/core/common/message.cpp b/src/core/common/message.cpp index 01ba1a4c2..aecf1ea61 100644 --- a/src/core/common/message.cpp +++ b/src/core/common/message.cpp @@ -642,12 +642,12 @@ bool Message::GetChildMask(uint16_t aChildIndex) const void Message::ClearChildMask(uint16_t aChildIndex) { - GetMetadata().mChildMask.Clear(aChildIndex); + GetMetadata().mChildMask.Set(aChildIndex, false); } void Message::SetChildMask(uint16_t aChildIndex) { - GetMetadata().mChildMask.Set(aChildIndex); + GetMetadata().mChildMask.Set(aChildIndex, true); } bool Message::IsChildPending(void) const diff --git a/src/core/thread/topology.cpp b/src/core/thread/topology.cpp index 70c714480..e8791cefe 100644 --- a/src/core/thread/topology.cpp +++ b/src/core/thread/topology.cpp @@ -157,8 +157,8 @@ void Child::ClearIp6Addresses(void) mMeshLocalIid.Clear(); memset(mIp6Address, 0, sizeof(mIp6Address)); #if OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE - mMlrToRegisterMask.ClearAll(); - mMlrRegisteredMask.ClearAll(); + mMlrToRegisterMask.Clear(); + mMlrRegisteredMask.Clear(); #endif } @@ -318,23 +318,8 @@ void Child::SetAddressMlrState(const Ip6::Address &aAddress, MlrState aState) addressIndex = static_cast(&aAddress - mIp6Address); - if (aState == kMlrStateToRegister) - { - mMlrToRegisterMask.Set(addressIndex); - } - else - { - mMlrToRegisterMask.Clear(addressIndex); - } - - if (aState == kMlrStateRegistered) - { - mMlrRegisteredMask.Set(addressIndex); - } - else - { - mMlrRegisteredMask.Clear(addressIndex); - } + mMlrToRegisterMask.Set(addressIndex, aState == kMlrStateToRegister); + mMlrRegisteredMask.Set(addressIndex, aState == kMlrStateRegistered); } #endif // OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE