Skip to content

Commit c9f0af6

Browse files
committed
Refactor JSON SimpleKey constants with helper
Add `json::MakeSimpleKey()` in `json_key.h` to compute key length and FNV-1a hash from string literals at compile time, and migrate multiple params-const headers to use it instead of manual `SimpleKey` initializers. This removes duplicated hash includes and avoids hardcoded length/hash mismatches. The change also marks `Key` getters as `[[nodiscard]]` and includes minor formatting/header cleanup in touched JSON DMX files.
1 parent 266495b commit c9f0af6

18 files changed

Lines changed: 165 additions & 110 deletions

File tree

common/include/json/json_key.h

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

2929
#include <cstdint>
3030

31+
#include "common/utils/utils_hash.h"
32+
3133
namespace json {
3234
struct SimpleKey {
3335
const char* name;
@@ -54,11 +56,11 @@ struct Key {
5456

5557
enum { kSimple, kKeyed } type;
5658

57-
constexpr const char* GetName() const noexcept { return type == kSimple ? simple_key->name : port_key->name; }
59+
[[nodiscard]] constexpr const char* GetName() const noexcept { return type == kSimple ? simple_key->name : port_key->name; }
5860

59-
constexpr uint8_t GetLength() const noexcept { return type == kSimple ? simple_key->length : port_key->length; }
61+
[[nodiscard]] constexpr uint8_t GetLength() const noexcept { return type == kSimple ? simple_key->length : port_key->length; }
6062

61-
constexpr uint32_t GetHash() const noexcept { return type == kSimple ? simple_key->hash : port_key->hash; }
63+
[[nodiscard]] constexpr uint32_t GetHash() const noexcept { return type == kSimple ? simple_key->hash : port_key->hash; }
6264
};
6365

6466
constexpr Key MakeKey(void (*set)(const char*, uint32_t), const SimpleKey& simple) noexcept {
@@ -68,6 +70,18 @@ constexpr Key MakeKey(void (*set)(const char*, uint32_t), const SimpleKey& simpl
6870
constexpr Key MakeKey(void (*set)(const char*, uint32_t, const char*, uint32_t), const PortKey& port) noexcept {
6971
return Key{.port_key = &port, .set_keyed = set, .type = Key::kKeyed};
7072
}
73+
74+
template <std::size_t N>
75+
constexpr SimpleKey MakeSimpleKey(const char (&name)[N]) noexcept {
76+
static_assert(N > 1);
77+
static_assert(N - 1 <= UINT8_MAX);
78+
79+
return {
80+
.name = name,
81+
.length = static_cast<uint8_t>(N - 1),
82+
.hash = Fnv1a32(name, N - 1)
83+
};
84+
}
7185
} // namespace json
7286

7387
#endif // JSON_JSON_KEY_H_

lib-artnet/include/json/artnetparamsconst.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ struct ArtNetParamsConst {
7070
};
7171

7272
// Art-Net 4
73-
static constexpr SimpleKey kEnableRdm{"enable_rdm", 10, Fnv1a32("enable_rdm", 10)};
73+
static constexpr auto kEnableRdm = json::MakeSimpleKey("enable_rdm");
7474

75-
static constexpr SimpleKey kMapUniverse0{"map_universe0", 13, Fnv1a32("map_universe0", 13)};
75+
static constexpr auto kMapUniverse0 = json::MakeSimpleKey("map_universe0");
7676

7777
static constexpr json::PortKey kProtocolPortA{"protocol_port_a", 15, Fnv1a32("protocol_port_a", 15)};
7878
#if (MAX_ARRAY_SIZE > 1)

lib-displayudf/include/json/displayudfparamsconst.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ namespace json {
4242
struct DisplayUdfParamsConst {
4343
static constexpr char kFileName[] = "display.json";
4444

45-
static constexpr json::SimpleKey kIntensity{"intensity", 9, Fnv1a32("intensity", 9)};
46-
static constexpr json::SimpleKey kSleepTimeout{"sleep_timeout", 13, Fnv1a32("sleep_timeout", 13)};
47-
static constexpr json::SimpleKey kFlipVertically{"flip_vertically", 15, Fnv1a32("flip_vertically", 15)};
45+
static constexpr auto kIntensity = json::MakeSimpleKey("intensity");
46+
static constexpr auto kSleepTimeout = json::MakeSimpleKey("sleep_timeout");
47+
static constexpr auto kFlipVertically = json::MakeSimpleKey("flip_vertically");
4848
static constexpr json::PortKey kTitle{"title", 5, Fnv1a32("title", 5)};
4949
static constexpr json::PortKey kBoardName{"board_name", 10, Fnv1a32("board_name", 10)};
5050
static constexpr json::PortKey kVersion{"version", 7, Fnv1a32("version", 7)};

lib-dmx/include/json/dmxsendparams.h

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
/**
22
* @file dmxsendparams.h
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
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
524

625
#ifndef JSON_DMXSENDPARAMS_H_
726
#define JSON_DMXSENDPARAMS_H_
@@ -11,10 +30,8 @@
1130
#include "json/json_key.h"
1231
#include "json/json_params_base.h"
1332

14-
namespace json
15-
{
16-
class DmxSendParams : public JsonParamsBase<DmxSendParams>
17-
{
33+
namespace json {
34+
class DmxSendParams : public JsonParamsBase<DmxSendParams> {
1835
public:
1936
DmxSendParams();
2037

@@ -40,14 +57,13 @@ class DmxSendParams : public JsonParamsBase<DmxSendParams>
4057
static constexpr json::Key kDmxSendKeys[] = {
4158
MakeKey(SetBreakTime, DmxSendParamsConst::kBreakTime),
4259
MakeKey(SetMabTime, DmxSendParamsConst::kMabTime),
43-
MakeKey(SetRefreshRate, DmxSendParamsConst::kRefreshRate),
44-
MakeKey(SetSlotsCount, DmxSendParamsConst::kSlotsCount)
45-
};
60+
MakeKey(SetRefreshRate, DmxSendParamsConst::kRefreshRate),
61+
MakeKey(SetSlotsCount, DmxSendParamsConst::kSlotsCount)};
4662

4763
inline static common::store::DmxSend store_dmx_send;
4864

4965
friend class JsonParamsBase<DmxSendParams>;
5066
};
5167
} // namespace json
5268

53-
#endif // JSON_DMXSENDPARAMS_H_
69+
#endif // JSON_DMXSENDPARAMS_H_

lib-dmx/include/json/dmxsendparamsconst.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
/**
22
* @file dmxsendparamsconst.h
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
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
524

625
#ifndef JSON_DMXSENDPARAMSCONST_H_
726
#define JSON_DMXSENDPARAMSCONST_H_
827

928
#include "json/json_key.h"
10-
#include "common/utils/utils_hash.h"
1129

1230
namespace json {
1331
struct DmxSendParamsConst {
1432
static constexpr char kFileName[] = "dmxsend.json";
1533

16-
static constexpr SimpleKey kBreakTime{"break_time", 10, Fnv1a32("break_time", 10)};
17-
static constexpr SimpleKey kMabTime{"mab_time", 8, Fnv1a32("mab_time", 8)};
18-
static constexpr SimpleKey kRefreshRate{"refresh_rate", 12, Fnv1a32("refresh_rate", 12)};
19-
static constexpr SimpleKey kSlotsCount{"slots_count", 11, Fnv1a32("slots_count", 11)};
34+
static constexpr auto kBreakTime = json::MakeSimpleKey("break_time");
35+
static constexpr auto kMabTime = json::MakeSimpleKey("mab_time");
36+
static constexpr auto kRefreshRate = json::MakeSimpleKey("refresh_rate");
37+
static constexpr auto kSlotsCount = json::MakeSimpleKey("slots_count");
2038
};
2139
} // namespace json
2240

lib-dmx/src/json/json_config_dmxsend.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,29 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
* THE SOFTWARE.
2323
*/
24-
24+
2525
#include <cstdint>
2626

2727
#include "json/dmxsendparams.h"
2828
#include "json/dmxsendparamsconst.h"
2929
#include "json/json_helpers.h"
3030
#include "dmx.h"
3131

32-
namespace json::config
33-
{
34-
uint32_t GetDmxSend(char* buffer, uint32_t length)
35-
{
36-
auto& dmx = *Dmx::Get();
37-
const auto kPeriod = dmx.TransmitPeriodTime();
32+
namespace json::config {
33+
uint32_t GetDmxSend(char* buffer, uint32_t length) {
34+
auto& dmx = *Dmx::Get();
35+
const auto kPeriod = dmx.TransmitPeriodTime();
3836

39-
return json::helpers::Serialize(buffer, length, [&](JsonDoc& doc) {
40-
doc[DmxSendParamsConst::kBreakTime.name] = dmx.TransmitBreakTime();
41-
doc[DmxSendParamsConst::kMabTime.name] = dmx.TransmitMabTime();
42-
doc[DmxSendParamsConst::kRefreshRate.name] = 1000000U / kPeriod;
43-
doc[DmxSendParamsConst::kSlotsCount.name] = dmx.TransmitSlots();
37+
return json::helpers::Serialize(buffer, length, [&](JsonDoc& doc) {
38+
doc[DmxSendParamsConst::kBreakTime.name] = dmx.TransmitBreakTime();
39+
doc[DmxSendParamsConst::kMabTime.name] = dmx.TransmitMabTime();
40+
doc[DmxSendParamsConst::kRefreshRate.name] = 1000000U / kPeriod;
41+
doc[DmxSendParamsConst::kSlotsCount.name] = dmx.TransmitSlots();
4442
});
4543
}
4644

47-
void SetDmxSend(const char* buffer, uint32_t buffer_size)
48-
{
49-
::json::DmxSendParams dmx_send_params;
45+
void SetDmxSend(const char* buffer, uint32_t buffer_size) {
46+
::json::DmxSendParams dmx_send_params;
5047
dmx_send_params.Store(buffer, buffer_size);
5148
dmx_send_params.Set();
5249
}

lib-dmx/src/json/json_status_dmx.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
/**
22
* @file json_status_dmx.cpp
33
*/
4-
/* Copyright (C) 2025-2026 by Arjan van Vught mailto:info@gd32-dmx.org */
4+
/* Copyright (C) 2025-206 by Arjan van Vught mailto:info@gd32-dmx.org
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
524

625
#include <cstdint>
726
#include <cstdio>

lib-dmxled/include/json/dmxledparamsconst.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,23 @@
2626
#define JSON_DMXLEDPARAMSCONST_H_
2727

2828
#include "json/json_key.h"
29-
#include "common/utils/utils_hash.h"
3029

3130
namespace json {
3231
struct DmxLedParamsConst {
3332
static constexpr char kFileName[] = "dmxled.json";
3433

35-
static constexpr json::SimpleKey kType{"type", 4, Fnv1a32("type", 4)};
36-
static constexpr json::SimpleKey kMap{"map", 3, Fnv1a32("map", 3)};
37-
static constexpr json::SimpleKey kCount{"count", 5, Fnv1a32("count", 5)};
38-
static constexpr json::SimpleKey kGroupingCount{"group_count", 11, Fnv1a32("group_count", 11)};
39-
static constexpr json::SimpleKey kT0H{"t0h", 3, Fnv1a32("t0h", 3)};
40-
41-
static constexpr json::SimpleKey kT1H{"t1h", 3, Fnv1a32("t1h", 3)};
42-
static constexpr json::SimpleKey kActiveOutputPorts{"active_out", 10, Fnv1a32("active_out", 10)};
43-
static constexpr json::SimpleKey kTestPattern{"test_pattern", 12, Fnv1a32("test_pattern", 12)};
44-
static constexpr json::SimpleKey kSpiSpeedHz{"clock_speed_hz", 14, Fnv1a32("clock_speed_hz", 14)};
45-
static constexpr json::SimpleKey kGlobalBrightness{"global_brightness", 17, Fnv1a32("global_brightness", 17)};
46-
static constexpr json::SimpleKey kGammaCorrection{"gamma_correction", 16, Fnv1a32("gamma_correction", 16)};
47-
static constexpr json::SimpleKey kGammaValue{"gamma_value", 11, Fnv1a32("gamma_value", 11)};
34+
static constexpr auto kType = json::MakeSimpleKey("type");
35+
static constexpr auto kMap = json::MakeSimpleKey("map");
36+
static constexpr auto kCount = json::MakeSimpleKey("count");
37+
static constexpr auto kGroupingCount = json::MakeSimpleKey("group_count");
38+
static constexpr auto kT0H = json::MakeSimpleKey("t0h");
39+
static constexpr auto kT1H = json::MakeSimpleKey("t1h");
40+
static constexpr auto kActiveOutputPorts = json::MakeSimpleKey("active_out");
41+
static constexpr auto kTestPattern = json::MakeSimpleKey("test_pattern");
42+
static constexpr auto kSpiSpeedHz = json::MakeSimpleKey("clock_speed_hz");
43+
static constexpr auto kGlobalBrightness = json::MakeSimpleKey("global_brightness");
44+
static constexpr auto kGammaCorrection = json::MakeSimpleKey("gamma_correction");
45+
static constexpr auto kGammaValue = json::MakeSimpleKey("gamma_value");
4846
};
4947
} // namespace json
5048

lib-network/include/json/networkparamsconst.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,18 @@
2626
#define JSON_NETWORKPARAMSCONST_H_
2727

2828
#include "json/json_key.h"
29-
#include "common/utils/utils_hash.h"
3029

3130
namespace json {
3231
struct NetworkParamsConst {
3332
static constexpr char kFileName[] = "network.json";
3433

35-
static constexpr json::SimpleKey kSecondaryIp{"secondary_ip", 12, Fnv1a32("secondary_ip", 12)};
36-
static constexpr json::SimpleKey kUseStaticIp{"use_static_ip", 13, Fnv1a32("use_static_ip", 13)};
37-
static constexpr json::SimpleKey kIpAddress{"ip_address", 10, Fnv1a32("ip_address", 10)};
38-
static constexpr json::SimpleKey kNetMask{"net_mask", 8, Fnv1a32("net_mask", 8)};
39-
static constexpr json::SimpleKey kDefaultGateway{"default_gateway", 15, Fnv1a32("default_gateway", 15)};
40-
static constexpr json::SimpleKey kHostname{"hostname", 8, Fnv1a32("hostname", 8)};
41-
static constexpr json::SimpleKey kNtpServer{"ntp_server", 10, Fnv1a32("ntp_server", 10)};
34+
static constexpr auto kSecondaryIp = json::MakeSimpleKey("secondary_ip");
35+
static constexpr auto kUseStaticIp = json::MakeSimpleKey("use_static_ip");
36+
static constexpr auto kIpAddress = json::MakeSimpleKey("ip_address");
37+
static constexpr auto kNetMask = json::MakeSimpleKey("net_mask");
38+
static constexpr auto kDefaultGateway = json::MakeSimpleKey("default_gateway");
39+
static constexpr auto kHostname = json::MakeSimpleKey("hostname");
40+
static constexpr auto kNtpServer = json::MakeSimpleKey("ntp_server");
4241
};
4342
} // namespace json
4443

lib-osc/include/json/oscclientparamsconst.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ namespace json {
3333
struct OscClientParamsConst {
3434
static constexpr char kFileName[] = "oscclient.json";
3535

36-
static constexpr json::SimpleKey kServerIp{"server_ip", 9, Fnv1a32("server_ip", 9)};
37-
static constexpr json::SimpleKey kPingDisable{"ping_disable", 12, Fnv1a32("ping_disable", 12)};
38-
static constexpr json::SimpleKey kPingDelay{"ping_delay", 10, Fnv1a32("ping_delay", 10)};
36+
static constexpr auto kServerIp = json::MakeSimpleKey("server_ip");
37+
static constexpr auto kPingDisable = json::MakeSimpleKey("ping_disable");
38+
static constexpr auto kPingDelay = json::MakeSimpleKey("ping_delay");
3939

4040
static constexpr json::PortKey kCmd0{"cmd0", 4, Fnv1a32("cmd0", 4)};
4141
static constexpr json::PortKey kCmd1{"cmd1", 4, Fnv1a32("cmd1", 4)};

0 commit comments

Comments
 (0)