From 23270725632bcdbc98b6eb7ee39101bcf65c2df2 Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Wed, 18 Dec 2019 14:42:35 +0800 Subject: [PATCH] [ncp] conform with c++11 constexpr and static_assert (#4416) C++11 constexpr doesn't support local variables and loop. This commit uses recursive way to assert handlers are sorted. C++11 static_assert requires a message. This commit adds the missing messages to static_assert calls. --- src/ncp/ncp_base.hpp | 2 +- src/ncp/ncp_base_dispatcher.cpp | 22 ++++++++-------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/ncp/ncp_base.hpp b/src/ncp/ncp_base.hpp index b2fe2f95a..c37c8297d 100644 --- a/src/ncp/ncp_base.hpp +++ b/src/ncp/ncp_base.hpp @@ -213,7 +213,7 @@ protected: otError HandleCommand(uint8_t aHeader); #if __cplusplus >= 201103L - static constexpr bool IsHandlerEntriesSorted(const HandlerEntry *aHandlerEntries, size_t aSize); + static constexpr bool AreHandlerEntriesSorted(const HandlerEntry *aHandlerEntries, size_t aSize); #endif static PropertyHandler FindPropertyHandler(const HandlerEntry *aHandlerEntries, diff --git a/src/ncp/ncp_base_dispatcher.cpp b/src/ncp/ncp_base_dispatcher.cpp index c3a33222d..00547cc9d 100644 --- a/src/ncp/ncp_base_dispatcher.cpp +++ b/src/ncp/ncp_base_dispatcher.cpp @@ -36,19 +36,11 @@ namespace ot { namespace Ncp { #if __cplusplus >= 201103L -constexpr bool NcpBase::IsHandlerEntriesSorted(const HandlerEntry *aHandlerEntries, size_t aSize) +constexpr bool NcpBase::AreHandlerEntriesSorted(const HandlerEntry *aHandlerEntries, size_t aSize) { - bool rval = true; - - for (size_t i = 1; i < aSize; ++i) - { - if (aHandlerEntries[i].mKey <= aHandlerEntries[i - 1].mKey) - { - rval = false; - } - } - - return rval; + return aSize < 2 ? true + : ((aHandlerEntries[aSize - 1].mKey > aHandlerEntries[aSize - 2].mKey) && + AreHandlerEntriesSorted(aHandlerEntries, aSize - 1)); } #endif @@ -352,7 +344,8 @@ NcpBase::PropertyHandler NcpBase::FindGetPropertyHandler(spinel_prop_key_t aKey) }; #if __cplusplus >= 201103L - static_assert(IsHandlerEntriesSorted(sHandlerEntries, OT_ARRAY_LENGTH(sHandlerEntries))); + static_assert(AreHandlerEntriesSorted(sHandlerEntries, OT_ARRAY_LENGTH(sHandlerEntries)), + "NCP property getter entries not sorted!"); #endif return FindPropertyHandler(sHandlerEntries, OT_ARRAY_LENGTH(sHandlerEntries), aKey); @@ -568,7 +561,8 @@ NcpBase::PropertyHandler NcpBase::FindSetPropertyHandler(spinel_prop_key_t aKey) }; #if __cplusplus >= 201103L - static_assert(IsHandlerEntriesSorted(sHandlerEntries, OT_ARRAY_LENGTH(sHandlerEntries))); + static_assert(AreHandlerEntriesSorted(sHandlerEntries, OT_ARRAY_LENGTH(sHandlerEntries)), + "NCP property setter entries not sorted!"); #endif return FindPropertyHandler(sHandlerEntries, OT_ARRAY_LENGTH(sHandlerEntries), aKey);