mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-13 11:42:38 +03:00
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:
parent
f00e9540d5
commit
6e282538cc
Notes:
sideshowbarker
2024-07-18 04:38:32 +09:00
Author: https://github.com/LucasChollet Commit: https://github.com/SerenityOS/serenity/commit/6e282538cc Pull-request: https://github.com/SerenityOS/serenity/pull/22139 Reviewed-by: https://github.com/nico ✅
@ -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};
|
||||
|
||||
}}
|
||||
|
Loading…
Reference in New Issue
Block a user