Skip to content

Commit 8df86d6

Browse files
committed
Replace utils_enum helpers with std::to_underlying
Remove the custom `utils_enum.h` header (which provided `common::ToValue` and `common::FromValue`) and replace all usages with C++23's `std::to_underlying` and `static_cast` respectively. Update `utils_flags.h` to use `std::to_underlying` directly. Also includes minor code style cleanups (whitespace, brace style) across affected files.
1 parent c9f0af6 commit 8df86d6

62 files changed

Lines changed: 782 additions & 2314 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

common/include/common/utils/utils_enum.h

Lines changed: 0 additions & 47 deletions
This file was deleted.

common/include/common/utils/utils_flags.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* @file utils_flags.h
3-
* Generic enum class bitmask helpers (C++20, freestanding-safe, Google Style)
3+
* Generic enum class bitmask helpers (C++23, freestanding-safe, Google Style)
44
*/
5-
/* Copyright (C) 2025 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2025-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -28,26 +28,25 @@
2828

2929
#include <cstdint>
3030
#include <type_traits>
31-
32-
#include "common/utils/utils_enum.h" // Ensure this provides ToValue and FromValue
31+
#include <utility>
3332

3433
namespace common {
3534
template <typename E>
3635
requires std::is_enum_v<E>
3736
constexpr E operator|(E lhs, E rhs) {
38-
return static_cast<E>(ToValue(lhs) | ToValue(rhs));
37+
return static_cast<E>(std::to_underlying(lhs) | std::to_underlying(rhs));
3938
}
4039

4140
template <typename E>
4241
requires std::is_enum_v<E>
4342
constexpr E operator&(E lhs, E rhs) {
44-
return static_cast<E>(ToValue(lhs) & ToValue(rhs));
43+
return static_cast<E>(std::to_underlying(lhs) & std::to_underlying(rhs));
4544
}
4645

4746
template <typename E>
4847
requires std::is_enum_v<E>
4948
constexpr E operator~(E e) {
50-
return static_cast<E>(~ToValue(e));
49+
return static_cast<E>(~std::to_underlying(e));
5150
}
5251

5352
template <typename E>
@@ -68,26 +67,26 @@ template <typename E>
6867
requires std::is_enum_v<E>
6968
constexpr void SetFlag(uint32_t& flags, E bit, bool enable) {
7069
if (enable) {
71-
flags |= ToValue(bit);
70+
flags |= std::to_underlying(bit);
7271
} else {
73-
flags &= ~ToValue(bit);
72+
flags &= ~std::to_underlying(bit);
7473
}
7574
}
7675

7776
template <typename E>
7877
requires std::is_enum_v<E>
7978
constexpr uint32_t SetFlagValue(uint32_t flags, E bit, bool enable) {
8079
if (enable) {
81-
return flags | ToValue(bit);
80+
return flags | std::to_underlying(bit);
8281
}
8382

84-
return flags & ~ToValue(bit);
83+
return flags & ~std::to_underlying(bit);
8584
}
8685

8786
template <typename E>
8887
requires std::is_enum_v<E>
8988
constexpr bool IsFlagSet(uint32_t flags, E bit) {
90-
return (flags & ToValue(bit)) != 0;
89+
return (flags & std::to_underlying(bit)) != 0;
9190
}
9291
} // namespace common
9392

common/include/firmware/debug/debug_stack.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ namespace debug::stack {
3939
inline static constexpr uint32_t kMagicWord = 0xABCDABCD;
4040

4141
inline void Print() {
42-
static uint32_t s_used_bytes_previous;
42+
static uint32_t s_used_bytes_previous;
4343
const auto* start = reinterpret_cast<uint32_t*>(&stack_low);
4444
const auto* end = reinterpret_cast<uint32_t*>(&_sp);
45-
assert(end > start);
45+
assert(end > start);
4646
const auto kSize = static_cast<uint32_t>(end - start);
4747

4848
const auto* ptr = start;
@@ -70,16 +70,25 @@ inline void Print() {
7070
}
7171

7272
#ifndef NDEBUG
73-
printf("Stack: Size %uKB, [%p:%p:%p], Used: %u, Free: %u [%u]", static_cast<unsigned>(kSize / (1024 / 4)), reinterpret_cast<const void *>(start), reinterpret_cast<const void *>(ptr), reinterpret_cast<const void *>(end), static_cast<unsigned>(kUsedBytes), static_cast<unsigned>(kFreeBytes), static_cast<unsigned>(kFreePct));
73+
printf("Stack: Size %uKB, [%p:%p:%p], Used: %u, Free: %u [%u]",
74+
static_cast<unsigned>(kSize / (1024 / 4)),
75+
reinterpret_cast<const void*>(start),
76+
reinterpret_cast<const void*>(ptr),
77+
reinterpret_cast<const void*>(end),
78+
static_cast<unsigned>(kUsedBytes),
79+
static_cast<unsigned>(kFreeBytes),
80+
static_cast<unsigned>(kFreePct));
7481
#else
75-
printf("Stack: Size %uKB, Used: %u, Free: %u", static_cast<unsigned>(kSize / (1024 / 4)), static_cast<unsigned>(kUsedBytes), static_cast<unsigned>(kFreeBytes));
82+
printf("Stack: Size %uKB, Used: %u, Free: %u", static_cast<unsigned>(kSize / (1024 / 4)),
83+
static_cast<unsigned>(kUsedBytes),
84+
static_cast<unsigned>(kFreeBytes));
7685
#endif
7786
printf("\x1b[39m\n");
7887
}
7988
}
8089

8190
inline void Run() {
82-
static uint32_t s_millis_previous;
91+
static uint32_t s_millis_previous;
8392
const auto kMillis = timing::Millis();
8493
if (kMillis - s_millis_previous >= 1000U) {
8594
s_millis_previous = kMillis;

gd32_emac_artnet_pixel_multi/firmware/main.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include "dmxnodemsgconst.h"
3636
#include "artnettriggerhandler.h"
3737
#include "firmware/pixeldmx/show.h"
38-
#include "common/utils/utils_enum.h"
3938
#include "pixeltestpattern.h"
4039
#include "json/pixeldmxparams.h"
4140
#include "pixeldmxmulti.h"
@@ -54,8 +53,7 @@ void RebootHandler() {
5453
}
5554
} // namespace board
5655

57-
int main() // NOLINT
58-
{
56+
int main() { // NOLINT
5957
board::Init();
6058
DisplayUdf display;
6159
ConfigStore config_store;

lib-artnet/src/node/artnetnode.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <cstdint>
3131
#include <cstdio>
3232
#include <cstring>
33+
#include <utility>
3334
#if !defined(DISABLE_RTC)
3435
#include <ctime>
3536
#endif
@@ -40,7 +41,6 @@
4041
#include "artnet.h"
4142
#include "artnetdisplay.h"
4243
#include "artnetstore.h"
43-
#include "common/utils/utils_enum.h"
4444
#if defined(ARTNET_HAVE_TRIGGER)
4545
#include "artnettrigger.h"
4646
#endif
@@ -87,7 +87,7 @@ ArtNetNode::ArtNetNode() {
8787

8888
memset(&art_poll_reply_, 0, sizeof(struct artnet::ArtPollReply));
8989
memcpy(art_poll_reply_.id, artnet::kNodeId, sizeof(art_poll_reply_.id));
90-
art_poll_reply_.op_code = common::ToValue(artnet::OpCodes::kOpPollreply);
90+
art_poll_reply_.op_code = std::to_underlying(artnet::OpCodes::kOpPollreply);
9191
art_poll_reply_.port = artnet::kUdpPort;
9292
art_poll_reply_.vers_info_h = ArtNetConst::kVersion[0];
9393
art_poll_reply_.vers_info_l = ArtNetConst::kVersion[1];
@@ -138,7 +138,7 @@ ArtNetNode::ArtNetNode() {
138138

139139
#if defined(ARTNET_HAVE_TIMECODE)
140140
memcpy(art_time_code_.id, artnet::kNodeId, sizeof(art_poll_reply_.id));
141-
art_time_code_.op_code = common::ToValue(artnet::OpCodes::kOpTimecode);
141+
art_time_code_.op_code = std::to_underlying(artnet::OpCodes::kOpTimecode);
142142
art_time_code_.prot_ver_hi = 0;
143143
art_time_code_.prot_ver_lo = artnet::kProtocolRevision;
144144
art_time_code_.filler1 = 0;
@@ -148,7 +148,7 @@ ArtNetNode::ArtNetNode() {
148148
#if defined(ARTNET_ENABLE_SENDDIAG)
149149
memset(&diag_data_, 0, sizeof(struct artnet::ArtDiagData));
150150
memcpy(diag_data_.id, artnet::kNodeId, sizeof(diag_data_.id));
151-
diag_data_.op_code = common::ToValue(artnet::OpCodes::kOpDiagdata);
151+
diag_data_.op_code = std::to_underlying(artnet::OpCodes::kOpDiagdata);
152152
diag_data_.prot_ver_lo = artnet::kProtocolRevision;
153153
#endif
154154

lib-displayudf/src/json/displayudfparams.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include "configurationstore.h"
3838
#include "common/utils/utils_flags.h"
3939
#include "common/utils/utils_array.h"
40-
#include "common/utils/utils_enum.h"
4140
#include "firmware/debug/debug_debug.h"
4241
#include "displayudf.h"
4342

@@ -117,9 +116,9 @@ void DisplayUdfParams::SetAndShow() {
117116
for (uint8_t i = 0; i < common::ArraySize(DisplayUdfParamsConst::kLabels); ++i) {
118117
const auto kLabelIndex = store_displayudf.label_index[i];
119118
if (kLabelIndex != 0) {
120-
displayudf.Set(kLabelIndex, common::FromValue<displayudf::Labels>(i));
119+
displayudf.Set(kLabelIndex, static_cast<displayudf::Labels>(i));
121120
} else {
122-
displayudf.Set(255, common::FromValue<displayudf::Labels>(i));
121+
displayudf.Set(255, static_cast<displayudf::Labels>(i));
123122
}
124123
}
125124

lib-dmxnode/src/json/dmxnodeparams.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <cstdint>
3030
#include <cstring>
31+
#include <utility>
3132

3233
#include "json/dmxnodeparams.h"
3334
#include "json/dmxnodeparamsconst.h"
@@ -39,7 +40,6 @@
3940
#include "configstore.h"
4041
#include "configurationstore.h"
4142
#include "common/utils/utils_flags.h"
42-
#include "common/utils/utils_enum.h"
4343
#include "firmware/debug/debug_debug.h"
4444

4545
using common::store::dmxnode::Flags;
@@ -59,7 +59,7 @@ void DmxNodeParams::SetNodeName(const char* val, uint32_t len) {
5959
}
6060

6161
void DmxNodeParams::SetFailsafe(const char* val, [[maybe_unused]] uint32_t len) {
62-
store_dmxnode.fail_safe = common::ToValue(dmxnode::GetFailsafe(val));
62+
store_dmxnode.fail_safe = std::to_underlying(dmxnode::GetFailsafe(val));
6363
}
6464

6565
void DmxNodeParams::SetDisableMergeTimeout(const char* val, [[maybe_unused]] uint32_t len) {

lib-pixel/include/pixelconfiguration.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828

2929
#include <cstdint>
3030
#include <cstdio>
31+
#include <utility>
3132
#include <cassert>
3233

3334
#include "pixeltype.h"
34-
#include "common/utils/utils_enum.h"
3535
#if defined(CONFIG_PIXELDMX_ENABLE_GAMMATABLE)
3636
#include "gamma/gamma_tables.h"
3737
#endif
@@ -122,7 +122,7 @@ class PixelConfiguration {
122122

123123
const auto& info = GetTypeInfo(type_);
124124

125-
leds_per_pixel_ = common::ToValue(info.led_count);
125+
leds_per_pixel_ = std::to_underlying(info.led_count);
126126
is_rtz_protocol_ = info.protocol_type == pixel::ProtocolType::kRtz;
127127

128128
if (map_ == pixel::LedMap::kUndefined) {

0 commit comments

Comments
 (0)