From f00e9540d56952713c3250b31ce485bcbc0e6228 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sat, 2 Dec 2023 20:01:30 -0500 Subject: [PATCH] LibGfx/TIFF: Add an intrinsic way to get the exportable name of Enums The `TIFFType` enum is exported with a different name to C++. This change of behavior was handled by manually setting the parameter of a function. However, we will soon need the exported name in more places, so let's make it a property of the Enum itself. --- Userland/Libraries/LibGfx/TIFFGenerator.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibGfx/TIFFGenerator.py b/Userland/Libraries/LibGfx/TIFFGenerator.py index 7e0e476c2a..6224423b44 100755 --- a/Userland/Libraries/LibGfx/TIFFGenerator.py +++ b/Userland/Libraries/LibGfx/TIFFGenerator.py @@ -12,7 +12,16 @@ from pathlib import Path from typing import List, Optional, Type -class TIFFType(Enum): +class EnumWithExportName(Enum): + @classmethod + def export_name(cls) -> str: + return cls.__name__ + + +class TIFFType(EnumWithExportName): + @classmethod + def export_name(cls) -> str: + return "Type" Byte = 1 ASCII = 2 UnsignedShort = 3 @@ -26,12 +35,12 @@ class TIFFType(Enum): UTF8 = 129 -class Predictor(Enum): +class Predictor(EnumWithExportName): NoPrediction = 1 HorizontalDifferencing = 2 -class Compression(Enum): +class Compression(EnumWithExportName): NoCompression = 1 CCITT = 2 Group3Fax = 3 @@ -75,8 +84,8 @@ LICENSE = R"""/* */""" -def export_enum_to_cpp(e: Type[Enum], special_name: Optional[str] = None) -> str: - output = f'enum class {special_name if special_name else e.__name__} {{\n' +def export_enum_to_cpp(e: Type[EnumWithExportName], special_name: Optional[str] = None) -> str: + output = f'enum class {e.export_name()} {{\n' for entry in e: output += f' {entry.name} = {entry.value},\n' @@ -238,7 +247,7 @@ class Metadata; namespace TIFF {{ -{export_enum_to_cpp(TIFFType, 'Type')} +{export_enum_to_cpp(TIFFType)} template x32> struct Rational {{