From 6e282538cc4f1dd66e0b697f3898813355131231 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sat, 2 Dec 2023 20:18:52 -0500 Subject: [PATCH] LibGfx/TIFF: Generate a C++ helper function to print enums For a given enum value, this function will return its corresponding name as a `StringView`. --- Userland/Libraries/LibGfx/TIFFGenerator.py | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Userland/Libraries/LibGfx/TIFFGenerator.py b/Userland/Libraries/LibGfx/TIFFGenerator.py index 6224423b445..38d8f61bfd2 100755 --- a/Userland/Libraries/LibGfx/TIFFGenerator.py +++ b/Userland/Libraries/LibGfx/TIFFGenerator.py @@ -94,6 +94,34 @@ def export_enum_to_cpp(e: Type[EnumWithExportName], special_name: Optional[str] return output +def export_enum_to_string_converter(enums: List[Type[EnumWithExportName]]) -> str: + stringifier_internals = [] + for e in enums: + single_stringifier = fR""" if constexpr (IsSame) {{ + switch (value) {{ + default: + return "Invalid value for {e.export_name()}"sv;""" + for entry in e: + single_stringifier += fR""" + case {e.export_name()}::{entry.name}: + return "{entry.name}"sv;""" + + single_stringifier += R""" + } + }""" + stringifier_internals.append(single_stringifier) + + stringifier_internals_str = '\n'.join(stringifier_internals) + + out = fR"""template +StringView name_for_enum_tag_value(E value) {{ +{stringifier_internals_str} + VERIFY_NOT_REACHED(); +}}""" + + return out + + def export_tag_related_enums(tags: List[Tag]) -> str: exported_enums = [] for tag in tags: @@ -263,6 +291,8 @@ using Value = Variant, i32, Rational {export_tag_related_enums(known_tags)} +{export_enum_to_string_converter([tag.associated_enum for tag in known_tags if tag.associated_enum] + [TIFFType])} + {HANDLE_TAG_SIGNATURE}; }}