Skip to content

Commit 6827b50

Browse files
committed
Raise common code to C++23
Update shared utility headers for C++23 formatting and syntax cleanup, and switch the common build flags from C++20 to C++23. Also tighten a few UTC offset calculations and simplify inline helpers in `global.h`.
1 parent af84177 commit 6827b50

8 files changed

Lines changed: 47 additions & 72 deletions

File tree

common/include/common/utils/utils_enum.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828

2929
#include <type_traits>
3030

31-
namespace common
32-
{
31+
namespace common {
3332
// Converts an enum class value to its underlying integer type.
3433
template <typename Enum>
3534
constexpr auto ToValue(Enum e) noexcept -> std::underlying_type_t<Enum> {

common/include/common/utils/utils_flags.h

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,80 +31,64 @@
3131

3232
#include "common/utils/utils_enum.h" // Ensure this provides ToValue and FromValue
3333

34-
namespace common
35-
{
36-
34+
namespace common {
3735
template <typename E>
3836
requires std::is_enum_v<E>
39-
constexpr E operator|(E lhs, E rhs)
40-
{
37+
constexpr E operator|(E lhs, E rhs) {
4138
return static_cast<E>(ToValue(lhs) | ToValue(rhs));
4239
}
4340

4441
template <typename E>
4542
requires std::is_enum_v<E>
46-
constexpr E operator&(E lhs, E rhs)
47-
{
43+
constexpr E operator&(E lhs, E rhs) {
4844
return static_cast<E>(ToValue(lhs) & ToValue(rhs));
4945
}
5046

5147
template <typename E>
5248
requires std::is_enum_v<E>
53-
constexpr E operator~(E e)
54-
{
49+
constexpr E operator~(E e) {
5550
return static_cast<E>(~ToValue(e));
5651
}
5752

5853
template <typename E>
5954
requires std::is_enum_v<E>
60-
constexpr E& operator|=(E& lhs, E rhs)
61-
{
55+
constexpr E& operator|=(E& lhs, E rhs) {
6256
lhs = lhs | rhs;
6357
return lhs;
6458
}
6559

6660
template <typename E>
6761
requires std::is_enum_v<E>
68-
constexpr E& operator&=(E& lhs, E rhs)
69-
{
62+
constexpr E& operator&=(E& lhs, E rhs) {
7063
lhs = lhs & rhs;
7164
return lhs;
7265
}
7366

7467
template <typename E>
7568
requires std::is_enum_v<E>
76-
constexpr void SetFlag(uint32_t& flags, E bit, bool enable)
77-
{
78-
if (enable)
79-
{
69+
constexpr void SetFlag(uint32_t& flags, E bit, bool enable) {
70+
if (enable) {
8071
flags |= ToValue(bit);
81-
}
82-
else
83-
{
72+
} else {
8473
flags &= ~ToValue(bit);
8574
}
8675
}
8776

8877
template <typename E>
8978
requires std::is_enum_v<E>
90-
constexpr uint32_t SetFlagValue(uint32_t flags, E bit, bool enable)
91-
{
92-
if (enable)
93-
{
79+
constexpr uint32_t SetFlagValue(uint32_t flags, E bit, bool enable) {
80+
if (enable) {
9481
return flags | ToValue(bit);
9582
}
96-
else
97-
{
98-
return flags & ~ToValue(bit);
99-
}
83+
84+
return flags & ~ToValue(bit);
10085
}
10186

10287
template <typename E>
103-
requires std::is_enum_v<E>
88+
requires std::is_enum_v<E>
10489
constexpr bool IsFlagSet(uint32_t flags, E bit) {
10590
return (flags & ToValue(bit)) != 0;
10691
}
107-
10892
} // namespace common
10993

110-
#endif // COMMON_UTILS_UTILS_FLAGS_H_
94+
#endif // COMMON_UTILS_UTILS_FLAGS_H_

common/include/common/utils/utils_hash.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,23 @@
2929
#include <cstdint>
3030

3131
// Compile-time FNV-1a 32-bit hash
32-
consteval uint32_t Fnv1a32(const char* str, uint8_t length)
33-
{
32+
consteval uint32_t Fnv1a32(const char* str, uint8_t length) {
3433
uint32_t hash = 0x811c9dc5u;
35-
for (uint8_t i = 0; i < length; ++i)
36-
{
34+
for (uint8_t i = 0; i < length; ++i) {
3735
hash ^= static_cast<uint8_t>(str[i]);
3836
hash *= 0x01000193u;
3937
}
4038
return hash;
4139
}
4240

4341
// Runtime version for raw filenames
44-
inline uint32_t Fnv1a32Runtime(const char* str, uint32_t length)
45-
{
42+
inline uint32_t Fnv1a32Runtime(const char* str, uint32_t length) {
4643
uint32_t hash = 0x811c9dc5u;
47-
for (uint32_t i = 0; i < length; ++i)
48-
{
44+
for (uint32_t i = 0; i < length; ++i) {
4945
hash ^= static_cast<uint8_t>(str[i]);
5046
hash *= 0x01000193u;
5147
}
5248
return hash;
5349
}
5450

55-
#endif // COMMON_UTILS_UTILS_HASH_H_
51+
#endif // COMMON_UTILS_UTILS_HASH_H_

common/include/common/utils/utils_port.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,17 @@
2828

2929
#include <cstdint>
3030

31-
namespace common
32-
{
33-
template <class S> void PortSet(uint32_t port_index, S s, uint16_t& n)
34-
{
31+
namespace common {
32+
template <class S>
33+
void PortSet(uint32_t port_index, S s, uint16_t& n) {
3534
uint16_t value = n; // Create a local copy
3635
value &= static_cast<uint16_t>(~(0x3 << (port_index * 2)));
3736
value |= static_cast<uint16_t>((static_cast<uint32_t>(s) & 0x3) << (port_index * 2));
3837
n = value; // Write back to the original field
3938
}
4039

41-
template <class S> S PortGet(uint32_t port_index, uint16_t n)
42-
{
40+
template <class S>
41+
S PortGet(uint32_t port_index, uint16_t n) {
4342
return static_cast<S>((n >> (port_index * 2)) & 0x3);
4443
}
4544
} // namespace common

common/include/common/utils/utils_string.h

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

2929
#include <cstdint>
3030

31-
namespace common
32-
{
33-
constexpr uint32_t ConstStrLen(const char* s)
34-
{
31+
namespace common {
32+
constexpr uint32_t ConstStrLen(const char* str) {
3533
uint32_t len = 0;
36-
while (s[len] != '\0')
37-
{
34+
while (str[len] != '\0') {
3835
++len;
3936
}
4037
return len;

common/include/global.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@
1616

1717
#include "utc.h"
1818

19-
/**
20-
* @namespace global
21-
* @brief Contains global variables related to time configuration.
22-
*/
23-
namespace global {
24-
extern int32_t g_utc_offset;
25-
}
26-
2719
/**
2820
* @class Global
2921
* @brief Singleton class for managing and validating UTC offsets.
@@ -53,14 +45,14 @@ class Global {
5345
* @param[out] hours Signed hour component.
5446
* @param[out] minutes Unsigned minute component.
5547
*/
56-
inline void GetUtcOffset(int32_t& hours, uint32_t& minutes) { utc::SplitOffset(global::g_utc_offset, hours, minutes); }
48+
void GetUtcOffset(int32_t& hours, uint32_t& minutes) { utc::SplitOffset(global::g_utc_offset, hours, minutes); }
5749

5850
/**
5951
* @brief Sets the global UTC offset if the value is valid.
6052
* @param utc_offset_seconds Offset in seconds
6153
* @return true if successfully set; false otherwise
6254
*/
63-
inline bool SetUtcOffsetIfValid(int32_t utc_offset_seconds) {
55+
bool SetUtcOffsetIfValid(int32_t utc_offset_seconds) {
6456
if (utc::IsValidOffset(utc_offset_seconds)) {
6557
::global::g_utc_offset = utc_offset_seconds;
6658
return true;
@@ -74,7 +66,7 @@ class Global {
7466
* @param minutes Unsigned minute component
7567
* @return true if valid and set; false otherwise
7668
*/
77-
inline bool SetUtcOffsetIfValid(int32_t hours, uint32_t minutes) {
69+
bool SetUtcOffsetIfValid(int32_t hours, uint32_t minutes) {
7870
int32_t offset_seconds;
7971
if (utc::ValidateOffset(hours, minutes, offset_seconds)) {
8072
return SetUtcOffsetIfValid(offset_seconds);

common/include/utc.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ inline bool ValidateOffset(int32_t hours, uint32_t minutes, int32_t& utc_offset_
7373
}
7474
for (const auto& offset : kValidOffsets) {
7575
if (offset.hours == hours && offset.minutes == minutes) {
76-
utc_offset_seconds = (hours >= 0) ? (hours * 3600 + static_cast<int32_t>(minutes) * 60) : (hours * 3600 - static_cast<int32_t>(minutes) * 60);
76+
utc_offset_seconds = (hours >= 0) ? ((hours * 3600) + static_cast<int32_t>(minutes) * 60) : (hours * 3600 - static_cast<int32_t>(minutes) * 60);
7777
return true;
7878
}
7979
}
@@ -87,7 +87,9 @@ inline bool ValidateOffset(int32_t hours, uint32_t minutes, int32_t& utc_offset_
8787
* @return true if offset is valid; false otherwise
8888
*/
8989
inline bool IsValidOffset(int32_t utc_offset_seconds) {
90-
if (utc_offset_seconds == 0) return true;
90+
if (utc_offset_seconds == 0) {
91+
return true;
92+
}
9193
int32_t hours = utc_offset_seconds / 3600;
9294
uint32_t minutes = (utc_offset_seconds >= 0) ? static_cast<uint32_t>(utc_offset_seconds - hours * 3600) / 60 : static_cast<uint32_t>((hours * 3600 - utc_offset_seconds)) / 60;
9395

@@ -96,8 +98,10 @@ inline bool IsValidOffset(int32_t utc_offset_seconds) {
9698
}
9799

98100
for (const auto& offset : kValidOffsets) {
99-
int32_t offset_seconds = (offset.hours >= 0) ? offset.hours * 3600 + static_cast<int32_t>(offset.minutes * 60) : offset.hours * 3600 - static_cast<int32_t>(offset.minutes * 60);
100-
if (utc_offset_seconds == offset_seconds) return true;
101+
int32_t offset_seconds = (offset.hours >= 0) ? (offset.hours * 3600) + static_cast<int32_t>(offset.minutes * 60) : offset.hours * 3600 - static_cast<int32_t>(offset.minutes * 60);
102+
if (utc_offset_seconds == offset_seconds) {
103+
return true;
104+
}
101105
}
102106
return false;
103107
}
@@ -149,9 +153,13 @@ inline bool ParseOffset(const char* buffer, uint32_t buffer_length, int32_t& hou
149153
if (buffer[5] < '0' || buffer[5] > '9') return false;
150154

151155
int32_t h = (buffer[1] - '0') * 10 + (buffer[2] - '0');
152-
if (h > 14) return false;
156+
if (h > 14) {
157+
return false;
158+
}
153159
uint32_t m = static_cast<uint32_t>((buffer[4] - '0') * 10 + (buffer[5] - '0'));
154-
if (m >= 60) return false;
160+
if (m >= 60) {
161+
return false;
162+
}
155163

156164
hours = negative ? -h : h;
157165
minutes = m;

common/make/CppOps.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$(info "CppOpts.mk")
22

3-
CPPOPS=-std=c++20
3+
CPPOPS=-std=c++23
44
CPPOPS+=-Wnon-virtual-dtor -Woverloaded-virtual -Wnull-dereference -fno-rtti -fno-exceptions -fno-unwind-tables
55
CPPOPS+=-Wuseless-cast -Wold-style-cast
66
CPPOPS+=-fno-threadsafe-statics -fno-use-cxa-atexit

0 commit comments

Comments
 (0)