Skip to content

Commit c921701

Browse files
committedMar 11, 2025·
Optimize size of code generated from ConfigFromString and ConfigToString classes
1 parent a3d2ad7 commit c921701

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed
 

‎Source/Config/ConfigFromString.h

+22-6
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,49 @@ class ConfigFromString {
4949

5050
void boolean(const char8_t* key, auto&& valueSetter, auto&& /* valueGetter */) noexcept
5151
{
52+
if (bool value; parseBool(key, value))
53+
valueSetter(value);
54+
}
55+
56+
void uint(const char8_t* key, auto&& valueSetter, auto&& /* valueGetter */) noexcept
57+
{
58+
if (std::uint64_t value; parseUint(key, value))
59+
valueSetter(value);
60+
}
61+
62+
private:
63+
[[nodiscard]] bool parseBool(const char8_t* key, bool& value) noexcept
64+
{
65+
bool parsed = false;
5266
if (shouldReadMe()) {
5367
const auto previousReadIndex = readIndex;
54-
if (bool parsedBool{}; readUntilStartOfValue(key) && parseBool(parsedBool)) {
55-
valueSetter(parsedBool);
68+
if (readUntilStartOfValue(key) && parseBool(value)) {
69+
parsed = true;
5670
markElementReadAtCurrentNestingLevel();
5771
} else {
5872
readIndex = previousReadIndex;
5973
}
6074
}
6175
increaseIndexInNestingLevel();
76+
return parsed;
6277
}
6378

64-
void uint(const char8_t* key, auto&& valueSetter, auto&& /* valueGetter */) noexcept
79+
[[nodiscard]] bool parseUint(const char8_t* key, std::uint64_t& value) noexcept
6580
{
81+
bool parsed = false;
6682
if (shouldReadMe()) {
6783
const auto previousReadIndex = readIndex;
68-
if (std::uint64_t parsedUint{}; readUntilStartOfValue(key) && parseUint(parsedUint)) {
69-
valueSetter(parsedUint);
84+
if (readUntilStartOfValue(key) && parseUint(value)) {
85+
parsed = true;
7086
markElementReadAtCurrentNestingLevel();
7187
} else {
7288
readIndex = previousReadIndex;
7389
}
7490
}
7591
increaseIndexInNestingLevel();
92+
return parsed;
7693
}
7794

78-
private:
7995
void markConversionComplete() noexcept
8096
{
8197
assert(conversionState.nestingLevel == 1);

‎Source/Config/ConfigToString.h

+15-5
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,40 @@ class ConfigToString {
5757
}
5858

5959
void boolean(const char8_t* key, auto&& /* valueSetter */, auto&& valueGetter) noexcept
60+
{
61+
writeBool(key, valueGetter());
62+
}
63+
64+
void uint(const char8_t* key, auto&& /* valueSetter */, auto&& valueGetter) noexcept
65+
{
66+
writeUint(key, valueGetter());
67+
}
68+
69+
private:
70+
void writeBool(const char8_t* key, bool value) noexcept
6071
{
6172
if (shouldWriteMe()) {
6273
const auto previousWriteIndex = writeIndex;
63-
if (writeCommaAfterPreviousElement() && writeKey(key) && writeBool(valueGetter()))
74+
if (writeCommaAfterPreviousElement() && writeKey(key) && writeBool(value))
6475
increaseConversionIndexInNestingLevel();
6576
else
6677
writeIndex = previousWriteIndex;
6778
}
6879
increaseIndexInNestingLevel();
6980
}
7081

71-
void uint(const char8_t* key, auto&& /* valueSetter */, auto&& valueGetter) noexcept
82+
void writeUint(const char8_t* key, std::uint64_t value) noexcept
7283
{
7384
if (shouldWriteMe()) {
7485
const auto previousWriteIndex = writeIndex;
75-
if (writeCommaAfterPreviousElement() && writeKey(key) && writeUint(valueGetter()))
86+
if (writeCommaAfterPreviousElement() && writeKey(key) && writeUint(value))
7687
increaseConversionIndexInNestingLevel();
7788
else
7889
writeIndex = previousWriteIndex;
7990
}
8091
increaseIndexInNestingLevel();
8192
}
8293

83-
private:
8494
[[nodiscard]] bool writeKey(const char8_t* key) noexcept
8595
{
8696
return writeChar(u8'"') && writeString(key) && writeString(u8"\":");
@@ -125,7 +135,7 @@ class ConfigToString {
125135
++indexInNestingLevel[nestingLevel];
126136
}
127137

128-
void increaseConversionIndexInNestingLevel()const noexcept
138+
void increaseConversionIndexInNestingLevel() const noexcept
129139
{
130140
assert(conversionState.indexInNestingLevel[conversionState.nestingLevel] < config_params::kMaxObjectIndex);
131141
++conversionState.indexInNestingLevel[conversionState.nestingLevel];

0 commit comments

Comments
 (0)
Please sign in to comment.