From a5a6fba9224752de2949686aec43cfdb28ed3027 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Sat, 17 Jul 2021 19:40:20 -0700 Subject: [PATCH] [common] replace `enum` constants with `constexpr` (#6845) This commit replaces the `enum` constants with `constexpr` definitions in all the modules under `core/common`. It also adds type to the names `enum` definitions. --- src/core/common/crc16.hpp | 2 +- src/core/common/logging.cpp | 12 +++--------- src/core/common/message.hpp | 29 ++++++++++------------------- src/core/common/notifier.hpp | 16 +++++++++------- src/core/common/settings.hpp | 2 +- src/core/common/time.hpp | 5 +---- src/core/common/time_ticker.hpp | 7 ++----- src/core/common/tlvs.hpp | 17 ++++------------- src/core/common/trickle_timer.hpp | 15 ++++++--------- 9 files changed, 37 insertions(+), 68 deletions(-) diff --git a/src/core/common/crc16.hpp b/src/core/common/crc16.hpp index d2e20d1ae..60f556ab3 100644 --- a/src/core/common/crc16.hpp +++ b/src/core/common/crc16.hpp @@ -47,7 +47,7 @@ namespace ot { class Crc16 { public: - enum Polynomial + enum Polynomial : uint16_t { kCcitt = 0x1021, ///< CRC16_CCITT kAnsi = 0x8005, ///< CRC16-ANSI diff --git a/src/core/common/logging.cpp b/src/core/common/logging.cpp index e8ea85a51..5c4c757b0 100644 --- a/src/core/common/logging.cpp +++ b/src/core/common/logging.cpp @@ -226,11 +226,8 @@ exit: } #endif -enum : uint8_t -{ - kStringLineLength = 80, - kDumpBytesPerLine = 16, -}; +static constexpr uint8_t kStringLineLength = 80; +static constexpr uint8_t kDumpBytesPerLine = 16; static void DumpLine(otLogLevel aLogLevel, otLogRegion aLogRegion, const uint8_t *aBytes, const size_t aLength) { @@ -279,10 +276,7 @@ static void DumpLine(otLogLevel aLogLevel, otLogRegion aLogRegion, const uint8_t void otDump(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aId, const void *aBuf, const size_t aLength) { - enum : uint8_t - { - kWidth = 72, - }; + constexpr uint8_t kWidth = 72; size_t idLen = strlen(aId); ot::String string; diff --git a/src/core/common/message.hpp b/src/core/common/message.hpp index 8088b8fc9..c33d03a2d 100644 --- a/src/core/common/message.hpp +++ b/src/core/common/message.hpp @@ -134,11 +134,8 @@ class HmacSha256; } \ } while (false) -enum -{ - kNumBuffers = OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS, - kBufferSize = OPENTHREAD_CONFIG_MESSAGE_BUFFER_SIZE, -}; +constexpr uint16_t kNumBuffers = OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS; +constexpr uint16_t kBufferSize = OPENTHREAD_CONFIG_MESSAGE_BUFFER_SIZE; class Message; class MessagePool; @@ -285,11 +282,8 @@ private: */ const uint8_t *GetData(void) const { return mBuffer.mData; } - enum - { - kBufferDataSize = kBufferSize - sizeof(otMessageBuffer), - kHeadBufferDataSize = kBufferDataSize - sizeof(MessageMetadata), - }; + static constexpr uint16_t kBufferDataSize = kBufferSize - sizeof(otMessageBuffer); + static constexpr uint16_t kHeadBufferDataSize = kBufferDataSize - sizeof(MessageMetadata); protected: union @@ -321,7 +315,7 @@ public: * This enumeration represents the message type. * */ - enum Type + enum Type : uint8_t { kTypeIp6 = 0, ///< A full uncompressed IPv6 packet kType6lowpan = 1, ///< A 6lowpan frame @@ -334,7 +328,7 @@ public: * This enumeration represents the message sub-type. * */ - enum SubType + enum SubType : uint8_t { kSubTypeNone = 0, ///< None kSubTypeMleAnnounce = 1, ///< MLE Announce @@ -349,7 +343,7 @@ public: kSubTypeMleChildIdRequest = 10, ///< MLE Child ID Request }; - enum Priority + enum Priority : uint8_t { kPriorityLow = OT_MESSAGE_PRIORITY_LOW, ///< Low priority level. kPriorityNormal = OT_MESSAGE_PRIORITY_NORMAL, ///< Normal priority level. @@ -357,16 +351,13 @@ public: kPriorityNet = OT_MESSAGE_PRIORITY_HIGH + 1, ///< Network Control priority level. }; - enum - { - kNumPriorities = 4, ///< Number of priority levels. - }; + static constexpr uint8_t kNumPriorities = 4; ///< Number of priority levels. /** * This enumeration represents the link security mode (used by `Settings` constructor). * */ - enum LinkSecurityMode + enum LinkSecurityMode : uint8_t { kNoLinkSecurity, ///< Link security disabled (no link security). kWithLinkSecurity, ///< Link security enabled. @@ -1400,7 +1391,7 @@ public: * should be added in the queue. * */ - enum QueuePosition + enum QueuePosition : uint8_t { kQueuePositionHead, ///< Indicates the head (front) of the list. kQueuePositionTail, ///< Indicates the tail (end) of the list. diff --git a/src/core/common/notifier.hpp b/src/core/common/notifier.hpp index 308a3242f..96494ac1a 100644 --- a/src/core/common/notifier.hpp +++ b/src/core/common/notifier.hpp @@ -297,13 +297,15 @@ public: } private: - enum - { - kMaxExternalHandlers = OPENTHREAD_CONFIG_MAX_STATECHANGE_HANDLERS, - kFlagsStringLineLimit = 70, // Character limit to divide the log into multiple lines in `LogChangedFlags()`. - kMaxFlagNameLength = 25, // Max length for string representation of a flag by `FlagToString()`. - kFlagsStringBufferSize = kFlagsStringLineLimit + kMaxFlagNameLength, - }; + static constexpr uint16_t kMaxExternalHandlers = OPENTHREAD_CONFIG_MAX_STATECHANGE_HANDLERS; + + // Character limit to divide the log into multiple lines in `LogChangedFlags()`. + static constexpr uint16_t kFlagsStringLineLimit = 70; + + // Max length for string representation of a flag by `FlagToString()`. + static constexpr uint8_t kMaxFlagNameLength = 25; + + static constexpr uint16_t kFlagsStringBufferSize = kFlagsStringLineLimit + kMaxFlagNameLength; struct ExternalCallback { diff --git a/src/core/common/settings.hpp b/src/core/common/settings.hpp index c288868bc..19f90f497 100644 --- a/src/core/common/settings.hpp +++ b/src/core/common/settings.hpp @@ -1062,7 +1062,7 @@ public: } private: - enum IteratorType + enum IteratorType : uint8_t { kEndIterator, }; diff --git a/src/core/common/time.hpp b/src/core/common/time.hpp index 98c299bd6..9e8358764 100644 --- a/src/core/common/time.hpp +++ b/src/core/common/time.hpp @@ -249,10 +249,7 @@ public: static uint32_t MsecToSec(uint32_t aMilliseconds) { return aMilliseconds / 1000u; } private: - enum - { - kDistantFuture = (1UL << 31), - }; + static constexpr uint32_t kDistantFuture = (1UL << 31); uint32_t mValue; }; diff --git a/src/core/common/time_ticker.hpp b/src/core/common/time_ticker.hpp index 4d733d584..8f5d27201 100644 --- a/src/core/common/time_ticker.hpp +++ b/src/core/common/time_ticker.hpp @@ -109,11 +109,8 @@ public: bool IsReceiverRegistered(Receiver aReceiver) const { return (mReceivers & Mask(aReceiver)) != 0; } private: - enum : uint32_t - { - kTickInterval = 1000, // in msec. - kRestartJitter = 4, // in msec, jitter added when restarting the timer [-4,+4] ms. - }; + static constexpr uint32_t kTickInterval = 1000; // in msec. + static constexpr uint32_t kRestartJitter = 4; // in msec, jitter added when restarting the timer [-4,+4] ms. constexpr static uint32_t Mask(Receiver aReceiver) { return static_cast(1U) << aReceiver; } diff --git a/src/core/common/tlvs.hpp b/src/core/common/tlvs.hpp index b127e426d..7d07b34dc 100644 --- a/src/core/common/tlvs.hpp +++ b/src/core/common/tlvs.hpp @@ -58,13 +58,10 @@ class Tlv { public: /** - * Length values. + * The maximum length of the Base TLV format. * */ - enum - { - kBaseTlvMaxLength = OT_NETWORK_BASE_TLV_MAX_LENGTH, ///< The maximum length of the Base TLV format. - }; + static constexpr uint8_t kBaseTlvMaxLength = OT_NETWORK_BASE_TLV_MAX_LENGTH; /** * This method returns the Type value. @@ -431,10 +428,7 @@ public: } protected: - enum - { - kExtendedLength = 255, ///< Extended Length value - }; + static const uint8_t kExtendedLength = 255; // Extended Length value. private: static Error Find(const Message &aMessage, uint8_t aType, uint16_t *aOffset, uint16_t *aSize, bool *aIsExtendedTlv); @@ -483,10 +477,7 @@ private: template class TlvInfo { public: - enum : uint8_t - { - kType = kTlvTypeValue, ///< The TLV Type value. - }; + static constexpr uint8_t kType = kTlvTypeValue; ///< The TLV Type value. }; /** diff --git a/src/core/common/trickle_timer.hpp b/src/core/common/trickle_timer.hpp index 9aac25554..5255179bf 100644 --- a/src/core/common/trickle_timer.hpp +++ b/src/core/common/trickle_timer.hpp @@ -68,15 +68,12 @@ public: kModePlainTimer, ///< Operate as a plain periodic timer with random interval selected within min/max intervals. }; - enum : uint16_t - { - /** - * Special value for redundancy constant (aka `k`) to indicate infinity (when used, it disables trickle timer's - * suppression behavior, invoking the handler callback independent of number of "consistent" events). - * - */ - kInfiniteRedundancyConstant = NumericLimits::kMax, - }; + /** + * Special value for redundancy constant (aka `k`) to indicate infinity (when used, it disables trickle timer's + * suppression behavior, invoking the handler callback independent of number of "consistent" events). + * + */ + static constexpr uint16_t kInfiniteRedundancyConstant = NumericLimits::kMax; /** * This function pointer is called when the timer expires (i.e., transmission should happen).