Skip to content

Commit

Permalink
[style] use constexpr instead of anonymous enum for constants (op…
Browse files Browse the repository at this point in the history
…enthread#11161)

This commit updates a few remaining places where anonymous enums were
used to declare constants, replacing them with `static constexpr`.
  • Loading branch information
abtink authored Jan 15, 2025
1 parent b8c4545 commit 0e1c9a7
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 39 deletions.
11 changes: 4 additions & 7 deletions src/cli/cli_br.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,11 @@ class Br : private Utils
otError Process(Arg aArgs[]);

private:
using Command = CommandEntry<Br>;

using Command = CommandEntry<Br>;
using PrefixType = uint8_t;
enum : PrefixType
{
kPrefixTypeLocal = 1u << 0,
kPrefixTypeFavored = 1u << 1,
};

static constexpr PrefixType kPrefixTypeLocal = 1u << 0;
static constexpr PrefixType kPrefixTypeFavored = 1u << 1;

enum RouterOutputMode : uint8_t
{
Expand Down
13 changes: 5 additions & 8 deletions src/core/net/tcp6.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,11 @@ class Tcp : public InstanceLocator, private NonCopyable
friend void ::tcplp_sys_set_timer(struct tcpcb *aTcb, uint8_t aTimerFlag, uint32_t aDelay);
friend void ::tcplp_sys_stop_timer(struct tcpcb *aTcb, uint8_t aTimerFlag);

enum : uint8_t
{
kTimerDelack = 0,
kTimerRexmtPersist = 1,
kTimerKeep = 2,
kTimer2Msl = 3,
kNumTimers = 4,
};
static constexpr uint8_t kTimerDelack = 0;
static constexpr uint8_t kTimerRexmtPersist = 1;
static constexpr uint8_t kTimerKeep = 2;
static constexpr uint8_t kTimer2Msl = 3;
static constexpr uint8_t kNumTimers = 4;

static uint8_t TimerFlagToIndex(uint8_t aTimerFlag);

Expand Down
5 changes: 1 addition & 4 deletions src/core/thread/mle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4094,10 +4094,7 @@ void Mle::Log(MessageAction aAction, MessageType aType, const Ip6::Address &aAdd

void Mle::Log(MessageAction aAction, MessageType aType, const Ip6::Address &aAddress, uint16_t aRloc)
{
enum : uint8_t
{
kRlocStringSize = 17,
};
static constexpr uint16_t kRlocStringSize = 17;

String<kRlocStringSize> rlocString;

Expand Down
9 changes: 3 additions & 6 deletions src/core/utils/parse_cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,14 @@ Error ParseAsUint32(const char *aString, uint32_t &aUint32) { return ParseUint<u

Error ParseAsUint64(const char *aString, uint64_t &aUint64)
{
static constexpr uint64_t kMaxHexBeforeOverflow = (0xffffffffffffffffULL / 16);
static constexpr uint64_t kMaxDecBeforeOverflow = (0xffffffffffffffffULL / 10);

Error error = kErrorNone;
uint64_t value = 0;
const char *cur = aString;
bool isHex = false;

enum : uint64_t
{
kMaxHexBeforeOverflow = (0xffffffffffffffffULL / 16),
kMaxDecBeforeOverflow = (0xffffffffffffffffULL / 10),
};

VerifyOrExit(aString != nullptr, error = kErrorInvalidArgs);

if (cur[0] == '0' && (cur[1] == 'x' || cur[1] == 'X'))
Expand Down
5 changes: 1 addition & 4 deletions src/ncp/ncp_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,10 +802,7 @@ class NcpBase
uint32_t mDroppedInboundIpFrameCounter; // Number of dropped inbound data/IP frames.

#if OPENTHREAD_CONFIG_SRP_CLIENT_ENABLE
enum : uint8_t
{
kSrpClientMaxHostAddresses = OPENTHREAD_CONFIG_SRP_CLIENT_BUFFERS_MAX_HOST_ADDRESSES,
};
static constexpr uint8_t kSrpClientMaxHostAddresses = OPENTHREAD_CONFIG_SRP_CLIENT_BUFFERS_MAX_HOST_ADDRESSES;

otError EncodeSrpClientHostInfo(const otSrpClientHostInfo &aHostInfo);
otError EncodeSrpClientServices(const otSrpClientService *aServices);
Expand Down
9 changes: 3 additions & 6 deletions tests/unit/test_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ namespace ot {

void TestMessage(void)
{
enum : uint16_t
{
kMaxSize = (kBufferSize * 3 + 24),
kOffsetStep = 101,
kLengthStep = 21,
};
static constexpr uint16_t kMaxSize = (kBufferSize * 3 + 24);
static constexpr uint16_t kOffsetStep = 101;
static constexpr uint16_t kLengthStep = 21;

Instance *instance;
MessagePool *messagePool;
Expand Down
5 changes: 1 addition & 4 deletions tests/unit/test_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ struct Entry : public EntryBase, LinkedListEntry<Entry>
bool mInitWithInstance;
};

enum : uint16_t
{
kPoolSize = 11,
};
constexpr uint16_t kPoolSize = 11;

typedef Pool<Entry, kPoolSize> EntryPool;

Expand Down

0 comments on commit 0e1c9a7

Please sign in to comment.