Skip to content

Commit b85c94b

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 c6078d3 commit b85c94b

3 files changed

Lines changed: 25 additions & 64 deletions

File tree

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;

0 commit comments

Comments
 (0)