Generate indexes for message registries
Being able to index into the message registry constexpr arrays will be
useful in the future, so update the parse_registries script to generate
an Index enum class, that allows directly indexing into the constexpr
table when necessary.
These indexes are used in the patchset here:
https://gerrit.openbmc-project.xyz/c/openbmc/bmcweb/+/50950
to avoid a binary search lookup for each entry.
Tested: No-op change, code inspection only.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I345cc26a2b17b5bcd8cfb0055642f4ae443caad4
diff --git a/scripts/parse_registries.py b/scripts/parse_registries.py
index d64e540..8334eae 100755
--- a/scripts/parse_registries.py
+++ b/scripts/parse_registries.py
@@ -91,7 +91,9 @@
registry.write(
"constexpr std::array<MessageEntry, {}> registry = {{".format(
len(json_dict["Messages"])))
- for messageId, message in sorted(json_dict["Messages"].items()):
+
+ messages_sorted = sorted(json_dict["Messages"].items())
+ for messageId, message in messages_sorted:
registry.write("MessageEntry{")
registry.write("\"{}\",".format(messageId))
registry.write("{")
@@ -108,7 +110,17 @@
registry.write("},")
registry.write("\"{}\",".format(message["Resolution"]))
registry.write("}},")
- registry.write("};}\n")
+
+ registry.write("\n};")
+
+ registry.write("\n\nenum class Index {\n")
+ for index, (messageId, message) in enumerate(messages_sorted):
+ messageId = messageId[0].lower() + messageId[1:]
+ registry.write(
+ "{} = {},\n".format(messageId, index))
+ registry.write("};")
+ registry.write("}")
+
clang_format(file)