[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.
This commit is contained in:
Yakun Xu
2019-12-17 22:42:35 -08:00
committed by Jonathan Hui
parent 420479d899
commit 2327072563
2 changed files with 9 additions and 15 deletions
+1 -1
View File
@@ -213,7 +213,7 @@ protected:
otError HandleCommand(uint8_t aHeader); otError HandleCommand(uint8_t aHeader);
#if __cplusplus >= 201103L #if __cplusplus >= 201103L
static constexpr bool IsHandlerEntriesSorted(const HandlerEntry *aHandlerEntries, size_t aSize); static constexpr bool AreHandlerEntriesSorted(const HandlerEntry *aHandlerEntries, size_t aSize);
#endif #endif
static PropertyHandler FindPropertyHandler(const HandlerEntry *aHandlerEntries, static PropertyHandler FindPropertyHandler(const HandlerEntry *aHandlerEntries,
+8 -14
View File
@@ -36,19 +36,11 @@ namespace ot {
namespace Ncp { namespace Ncp {
#if __cplusplus >= 201103L #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; return aSize < 2 ? true
: ((aHandlerEntries[aSize - 1].mKey > aHandlerEntries[aSize - 2].mKey) &&
for (size_t i = 1; i < aSize; ++i) AreHandlerEntriesSorted(aHandlerEntries, aSize - 1));
{
if (aHandlerEntries[i].mKey <= aHandlerEntries[i - 1].mKey)
{
rval = false;
}
}
return rval;
} }
#endif #endif
@@ -352,7 +344,8 @@ NcpBase::PropertyHandler NcpBase::FindGetPropertyHandler(spinel_prop_key_t aKey)
}; };
#if __cplusplus >= 201103L #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 #endif
return FindPropertyHandler(sHandlerEntries, OT_ARRAY_LENGTH(sHandlerEntries), aKey); return FindPropertyHandler(sHandlerEntries, OT_ARRAY_LENGTH(sHandlerEntries), aKey);
@@ -568,7 +561,8 @@ NcpBase::PropertyHandler NcpBase::FindSetPropertyHandler(spinel_prop_key_t aKey)
}; };
#if __cplusplus >= 201103L #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 #endif
return FindPropertyHandler(sHandlerEntries, OT_ARRAY_LENGTH(sHandlerEntries), aKey); return FindPropertyHandler(sHandlerEntries, OT_ARRAY_LENGTH(sHandlerEntries), aKey);