Skip to content

Commit ec91cb5

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 f55e55d commit ec91cb5

68 files changed

Lines changed: 384 additions & 548 deletions

File tree

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;

lib-artnet/.settings/language.settings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
2222
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
2323
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuildCommandParser" id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser" keep-relative-paths="false" name="CDT GCC Build Output Parser" parameter="([^/\\\\]*)((g?cc)|([gc]\+\+)|(clang))" prefer-non-shared="true"/>
24-
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="1429925378381954929" id="org.eclipse.embedcdt.managedbuild.cross.arm.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Arm Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
24+
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="163286230884080209" id="org.eclipse.embedcdt.managedbuild.cross.arm.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Arm Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
2525
<language-scope id="org.eclipse.cdt.core.gcc"/>
2626
<language-scope id="org.eclipse.cdt.core.g++"/>
2727
</provider>

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/.settings/language.settings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
66
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
77
<provider copy-of="extension" id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser"/>
8-
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="-1613161325270985438" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
8+
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="-1165176944216149198" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
99
<language-scope id="org.eclipse.cdt.core.gcc"/>
1010
<language-scope id="org.eclipse.cdt.core.g++"/>
1111
</provider>
@@ -17,7 +17,7 @@
1717
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
1818
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
1919
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuildCommandParser" id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser" keep-relative-paths="false" name="CDT GCC Build Output Parser" parameter="([^/\\\\]*)((g?cc)|([gc]\+\+)|(clang))" prefer-non-shared="true"/>
20-
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="1448885608685315623" id="org.eclipse.embedcdt.managedbuild.cross.arm.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Arm Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
20+
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="182246461187440903" id="org.eclipse.embedcdt.managedbuild.cross.arm.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Arm Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
2121
<language-scope id="org.eclipse.cdt.core.gcc"/>
2222
<language-scope id="org.eclipse.cdt.core.g++"/>
2323
</provider>

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

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @file dmxmonitorparams.cpp
33
*/
4-
/* Copyright (C) 2025 by Arjan van Vught mailto:info@gd32-dmx.org
4+
/* Copyright (C) 2025-2026 by Arjan van Vught mailto:info@gd32-dmx.org
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -23,79 +23,67 @@
2323
*/
2424

2525
#include <cstring>
26+
#include <utility>
2627

27-
#include "common/utils/utils_enum.h"
2828
#include "dmxmonitor.h"
2929
#include "configstore.h"
3030
#include "json/dmxmonitorparams.h"
3131
#include "json/dmxmonitorparamsconst.h"
3232
#include "json/json_parsehelper.h"
3333
#include "json/json_parser.h"
3434

35-
namespace json
36-
{
37-
DmxMonitorParams::DmxMonitorParams()
38-
{
35+
namespace json {
36+
DmxMonitorParams::DmxMonitorParams() {
3937
ConfigStore::Instance().Copy(&store_dmxmonitor, &ConfigurationStore::dmx_monitor);
4038
}
4139

42-
void DmxMonitorParams::SetDmxStartAddress(const char* val, uint32_t len)
43-
{
40+
void DmxMonitorParams::SetDmxStartAddress(const char* val, uint32_t len) {
4441
auto v = ParseValue<uint16_t>(val, len);
4542
store_dmxmonitor.dmx_start_address = v;
4643
}
4744

48-
void DmxMonitorParams::SetDmxMaxChannels(const char* val, uint32_t len)
49-
{
45+
void DmxMonitorParams::SetDmxMaxChannels(const char* val, uint32_t len) {
5046
auto v = ParseValue<uint16_t>(val, len);
5147
store_dmxmonitor.dmx_max_channels = v;
5248
}
5349

54-
void DmxMonitorParams::SetFormat(const char* val, uint32_t len)
55-
{
56-
if (len != 3) return;
57-
58-
if (memcmp(val, "pct", 3) == 0)
59-
{
60-
store_dmxmonitor.format = common::ToValue(dmxmonitor::Format::kPct);
61-
}
62-
else if (memcmp(val, "dec", 3) == 0)
63-
{
64-
store_dmxmonitor.format = common::ToValue(dmxmonitor::Format::kDec);
65-
}
66-
else
67-
{
68-
store_dmxmonitor.format = common::ToValue(dmxmonitor::Format::kHex);
50+
void DmxMonitorParams::SetFormat(const char* val, uint32_t len) {
51+
if (len != 3) return;
52+
53+
if (memcmp(val, "pct", 3) == 0) {
54+
store_dmxmonitor.format = std::to_underlying(dmxmonitor::Format::kPct);
55+
} else if (memcmp(val, "dec", 3) == 0) {
56+
store_dmxmonitor.format = std::to_underlying(dmxmonitor::Format::kDec);
57+
} else {
58+
store_dmxmonitor.format = std::to_underlying(dmxmonitor::Format::kHex);
6959
}
7060
}
7161

72-
void DmxMonitorParams::Store(const char* buffer, uint32_t buffer_size)
73-
{
62+
void DmxMonitorParams::Store(const char* buffer, uint32_t buffer_size) {
7463
ParseJsonWithTable(buffer, buffer_size, kDmxMonitorKeys);
7564
ConfigStore::Instance().Store(&store_dmxmonitor, &ConfigurationStore::dmx_monitor);
7665
}
7766

78-
void DmxMonitorParams::Set()
79-
{
67+
void DmxMonitorParams::Set() {
8068
auto& dmxmonitor = DmxMonitor::Instance();
8169

82-
dmxmonitor.SetDmxStartAddress(store_dmxmonitor.dmx_start_address);
83-
dmxmonitor.SetFormat(static_cast<dmxmonitor::Format>(store_dmxmonitor.format));
70+
dmxmonitor.SetDmxStartAddress(store_dmxmonitor.dmx_start_address);
71+
dmxmonitor.SetFormat(static_cast<dmxmonitor::Format>(store_dmxmonitor.format));
8472
#if defined(__linux__) || defined(__APPLE__)
85-
dmxmonitor.SetMaxDmxChannels(store_dmxmonitor.dmx_max_channels);
73+
dmxmonitor.SetMaxDmxChannels(store_dmxmonitor.dmx_max_channels);
8674
#endif
8775

8876
#ifndef NDEBUG
8977
Dump();
9078
#endif
9179
}
9280

93-
void DmxMonitorParams::Dump()
94-
{
81+
void DmxMonitorParams::Dump() {
9582
printf("%s::%s \'%s\':\n", __FILE__, __FUNCTION__, json::DmxMonitorParamsConst::kFileName);
9683

97-
printf(" %s=%u\n", DmxMonitorParamsConst::kDmxStartAddress.name, store_dmxmonitor.dmx_start_address);
84+
printf(" %s=%u\n", DmxMonitorParamsConst::kDmxStartAddress.name, store_dmxmonitor.dmx_start_address);
9885
printf(" %s=%u\n", DmxMonitorParamsConst::kDmxMaxChannels.name, store_dmxmonitor.dmx_max_channels);
99-
printf(" %s=%s [%u]\n", DmxMonitorParamsConst::kFormat.name, store_dmxmonitor.format == common::ToValue(dmxmonitor::Format::kPct) ? "pct" : (store_dmxmonitor.format == static_cast<uint8_t>(dmxmonitor::Format::kDec) ? "dec" : "hex"), static_cast<int>(store_dmxmonitor.format));
86+
printf(" %s=%s [%u]\n", DmxMonitorParamsConst::kFormat.name, store_dmxmonitor.format == std::to_underlying(dmxmonitor::Format::kPct) ? "pct" : (store_dmxmonitor.format == static_cast<uint8_t>(dmxmonitor::Format::kDec) ? "dec" : "hex"),
87+
static_cast<int>(store_dmxmonitor.format));
10088
}
10189
} // namespace json

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) {

0 commit comments

Comments
 (0)