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`.
This commit is contained in:
Lucas CHOLLET 2023-12-02 20:18:52 -05:00 committed by Andreas Kling
parent f00e9540d5
commit 6e282538cc
Notes: sideshowbarker 2024-07-18 04:38:32 +09:00

View File

@ -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<E, {e.export_name()}>) {{
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<Enum E>
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<ByteBuffer, String, u32, Rational<u32>, i32, Rational<i32>
{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};
}}