mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
LibGfx/TIFF: Honor the default value for single count tags
Some tags have a default value, we should return this value in Metadata's getters when no value has been read from the input file. Note that we don't support default values for tags with a count bigger than one.
This commit is contained in:
parent
ef10a58522
commit
34e9059ae8
Notes:
sideshowbarker
2024-07-16 20:31:50 +09:00
Author: https://github.com/LucasChollet Commit: https://github.com/SerenityOS/serenity/commit/34e9059ae8 Pull-request: https://github.com/SerenityOS/serenity/pull/22637 Reviewed-by: https://github.com/nico ✅
1 changed files with 15 additions and 3 deletions
|
@ -9,7 +9,7 @@ import re
|
|||
from enum import Enum
|
||||
from collections import namedtuple
|
||||
from pathlib import Path
|
||||
from typing import List, Type
|
||||
from typing import Any, List, Type
|
||||
|
||||
|
||||
class EnumWithExportName(Enum):
|
||||
|
@ -251,6 +251,12 @@ def pascal_case_to_snake_case(name: str) -> str:
|
|||
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()
|
||||
|
||||
|
||||
def default_value_to_cpp(value: Any) -> str:
|
||||
if isinstance(value, EnumWithExportName):
|
||||
return f'TIFF::{value.export_name()}::{value.name}'
|
||||
return str(value)
|
||||
|
||||
|
||||
def generate_getter(tag: Tag) -> str:
|
||||
biggest_type = retrieve_biggest_type(tag.types)
|
||||
variant_inner_type = tiff_type_to_cpp(biggest_type)
|
||||
|
@ -262,7 +268,8 @@ def generate_getter(tag: Tag) -> str:
|
|||
tag_final_type = f"TIFF::{tag.associated_enum.__name__}"
|
||||
extracted_value_template = f"static_cast<{tag_final_type}>({extracted_value_template})"
|
||||
|
||||
if len(tag.counts) == 1 and tag.counts[0] == 1 or is_container(biggest_type):
|
||||
single_count = len(tag.counts) == 1 and tag.counts[0] == 1 or is_container(biggest_type)
|
||||
if single_count:
|
||||
return_type = tag_final_type
|
||||
if is_container(biggest_type):
|
||||
return_type += ' const&'
|
||||
|
@ -289,11 +296,16 @@ def generate_getter(tag: Tag) -> str:
|
|||
|
||||
signature = fR" Optional<{return_type}> {pascal_case_to_snake_case(tag.name)}() const"
|
||||
|
||||
if tag.default and single_count:
|
||||
return_if_empty = f'{default_value_to_cpp(tag.default)}'
|
||||
else:
|
||||
return_if_empty = 'OptionalNone {}'
|
||||
|
||||
body = fR"""
|
||||
{{
|
||||
auto const& possible_value = m_data.get("{tag.name}"sv);
|
||||
if (!possible_value.has_value())
|
||||
return OptionalNone {{}};
|
||||
return {return_if_empty};
|
||||
{unpacked_if_needed}
|
||||
}}
|
||||
"""
|
||||
|
|
Loading…
Reference in a new issue