mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-10-17 13:39:25 +00:00
Everywhere: Use Optional<T>::ensure() where useful
No functional changes.
This commit is contained in:
parent
0fe9255991
commit
c31eff6a47
Notes:
github-actions[bot]
2025-09-17 16:02:17 +00:00
Author: https://github.com/gmta
Commit: c31eff6a47
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6220
Reviewed-by: https://github.com/rmg-x
Reviewed-by: https://github.com/trflynn89 ✅
14 changed files with 53 additions and 87 deletions
|
@ -360,13 +360,11 @@ FLATTEN SignedBigInteger SignedBigInteger::negated_value() const
|
||||||
|
|
||||||
u32 SignedBigInteger::hash() const
|
u32 SignedBigInteger::hash() const
|
||||||
{
|
{
|
||||||
if (m_hash.has_value())
|
return m_hash.ensure([&] {
|
||||||
return *m_hash;
|
auto buffer = MUST(ByteBuffer::create_zeroed(byte_length()));
|
||||||
|
auto result = export_data(buffer);
|
||||||
auto buffer = MUST(ByteBuffer::create_zeroed(byte_length()));
|
return string_hash(reinterpret_cast<char const*>(result.data()), result.size());
|
||||||
auto result = export_data(buffer);
|
});
|
||||||
m_hash = string_hash(reinterpret_cast<char const*>(result.data()), result.size());
|
|
||||||
return *m_hash;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SignedBigInteger::operator==(SignedBigInteger const& other) const
|
bool SignedBigInteger::operator==(SignedBigInteger const& other) const
|
||||||
|
|
|
@ -359,13 +359,11 @@ FLATTEN UnsignedBigInteger UnsignedBigInteger::lcm(UnsignedBigInteger const& oth
|
||||||
|
|
||||||
u32 UnsignedBigInteger::hash() const
|
u32 UnsignedBigInteger::hash() const
|
||||||
{
|
{
|
||||||
if (m_hash.has_value())
|
return m_hash.ensure([&] {
|
||||||
return *m_hash;
|
auto buffer = MUST(ByteBuffer::create_zeroed(byte_length()));
|
||||||
|
auto result = export_data(buffer);
|
||||||
auto buffer = MUST(ByteBuffer::create_zeroed(byte_length()));
|
return string_hash(reinterpret_cast<char const*>(result.data()), result.size());
|
||||||
auto result = export_data(buffer);
|
});
|
||||||
m_hash = string_hash(reinterpret_cast<char const*>(result.data()), result.size());
|
|
||||||
return *m_hash;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool UnsignedBigInteger::operator==(UnsignedBigInteger const& other) const
|
bool UnsignedBigInteger::operator==(UnsignedBigInteger const& other) const
|
||||||
|
|
|
@ -114,12 +114,11 @@ void TypefaceSkia::populate_glyph_page(GlyphPage& glyph_page, size_t page_index)
|
||||||
|
|
||||||
FlyString const& TypefaceSkia::family() const
|
FlyString const& TypefaceSkia::family() const
|
||||||
{
|
{
|
||||||
if (!m_family.has_value()) {
|
return m_family.ensure([&] {
|
||||||
SkString family_name;
|
SkString family_name;
|
||||||
impl().skia_typeface->getFamilyName(&family_name);
|
impl().skia_typeface->getFamilyName(&family_name);
|
||||||
m_family = FlyString::from_utf8_without_validation(ReadonlyBytes { family_name.c_str(), family_name.size() });
|
return FlyString::from_utf8_without_validation(ReadonlyBytes { family_name.c_str(), family_name.size() });
|
||||||
}
|
});
|
||||||
return m_family.value();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
u16 TypefaceSkia::weight() const
|
u16 TypefaceSkia::weight() const
|
||||||
|
|
|
@ -38,9 +38,7 @@ Locale::Locale(Object& prototype)
|
||||||
|
|
||||||
Unicode::LocaleID const& Locale::locale_id() const
|
Unicode::LocaleID const& Locale::locale_id() const
|
||||||
{
|
{
|
||||||
if (!m_cached_locale_id.has_value())
|
return m_cached_locale_id.ensure([&] { return Unicode::parse_unicode_locale_id(locale()); });
|
||||||
m_cached_locale_id = Unicode::parse_unicode_locale_id(locale());
|
|
||||||
return *m_cached_locale_id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 15.5.5 GetLocaleVariants ( locale ), https://tc39.es/ecma402/#sec-getlocalevariants
|
// 15.5.5 GetLocaleVariants ( locale ), https://tc39.es/ecma402/#sec-getlocalevariants
|
||||||
|
|
|
@ -53,9 +53,7 @@ public:
|
||||||
|
|
||||||
Vector<Test::Suite>& ensure_suites()
|
Vector<Test::Suite>& ensure_suites()
|
||||||
{
|
{
|
||||||
if (!m_suites.has_value())
|
return m_suites.ensure([] { return Vector<Suite> {}; });
|
||||||
m_suites = Vector<Suite> {};
|
|
||||||
return *m_suites;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -315,41 +315,29 @@ static FDFlags fd_flags_of(struct stat const& buf);
|
||||||
|
|
||||||
Vector<AK::String> const& Implementation::arguments() const
|
Vector<AK::String> const& Implementation::arguments() const
|
||||||
{
|
{
|
||||||
if (!cache.cached_arguments.has_value()) {
|
return cache.cached_arguments.ensure([&] {
|
||||||
cache.cached_arguments.lazy_emplace([&] {
|
if (provide_arguments)
|
||||||
if (provide_arguments)
|
return provide_arguments();
|
||||||
return provide_arguments();
|
return Vector<AK::String> {};
|
||||||
return Vector<AK::String> {};
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return *cache.cached_arguments;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<AK::String> const& Implementation::environment() const
|
Vector<AK::String> const& Implementation::environment() const
|
||||||
{
|
{
|
||||||
if (!cache.cached_environment.has_value()) {
|
return cache.cached_environment.ensure([&] {
|
||||||
cache.cached_environment.lazy_emplace([&] {
|
if (provide_environment)
|
||||||
if (provide_environment)
|
return provide_environment();
|
||||||
return provide_environment();
|
return Vector<AK::String> {};
|
||||||
return Vector<AK::String> {};
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return *cache.cached_environment;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<Implementation::MappedPath> const& Implementation::preopened_directories() const
|
Vector<Implementation::MappedPath> const& Implementation::preopened_directories() const
|
||||||
{
|
{
|
||||||
if (!cache.cached_preopened_directories.has_value()) {
|
return cache.cached_preopened_directories.ensure([&] {
|
||||||
cache.cached_preopened_directories.lazy_emplace([&] {
|
if (provide_preopened_directories)
|
||||||
if (provide_preopened_directories)
|
return provide_preopened_directories();
|
||||||
return provide_preopened_directories();
|
return Vector<MappedPath> {};
|
||||||
return Vector<MappedPath> {};
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return *cache.cached_preopened_directories;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Implementation::Descriptor Implementation::map_fd(FD fd)
|
Implementation::Descriptor Implementation::map_fd(FD fd)
|
||||||
|
|
|
@ -72,7 +72,7 @@ void CSSRule::clear_caches()
|
||||||
m_cached_layer_name.clear();
|
m_cached_layer_name.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
FlyString const& CSSRule::parent_layer_internal_qualified_name_slow_case() const
|
FlyString CSSRule::parent_layer_internal_qualified_name_slow_case() const
|
||||||
{
|
{
|
||||||
Vector<FlyString> layer_names;
|
Vector<FlyString> layer_names;
|
||||||
for (auto* rule = parent_rule(); rule; rule = rule->parent_rule()) {
|
for (auto* rule = parent_rule(); rule; rule = rule->parent_rule()) {
|
||||||
|
@ -105,8 +105,7 @@ FlyString const& CSSRule::parent_layer_internal_qualified_name_slow_case() const
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_cached_layer_name = MUST(String::join("."sv, layer_names.in_reverse()));
|
return MUST(String::join('.', layer_names.in_reverse()));
|
||||||
return m_cached_layer_name.value();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,12 +70,10 @@ protected:
|
||||||
|
|
||||||
[[nodiscard]] FlyString const& parent_layer_internal_qualified_name() const
|
[[nodiscard]] FlyString const& parent_layer_internal_qualified_name() const
|
||||||
{
|
{
|
||||||
if (!m_cached_layer_name.has_value())
|
return m_cached_layer_name.ensure([&] { return parent_layer_internal_qualified_name_slow_case(); });
|
||||||
return parent_layer_internal_qualified_name_slow_case();
|
|
||||||
return m_cached_layer_name.value();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] FlyString const& parent_layer_internal_qualified_name_slow_case() const;
|
[[nodiscard]] FlyString parent_layer_internal_qualified_name_slow_case() const;
|
||||||
|
|
||||||
Type m_type;
|
Type m_type;
|
||||||
GC::Ptr<CSSRule> m_parent_rule;
|
GC::Ptr<CSSRule> m_parent_rule;
|
||||||
|
|
|
@ -4053,9 +4053,7 @@ GC::Ptr<NamedNodeMap const> Element::attributes() const
|
||||||
|
|
||||||
FlyString const& Element::html_uppercased_qualified_name() const
|
FlyString const& Element::html_uppercased_qualified_name() const
|
||||||
{
|
{
|
||||||
if (!m_html_uppercased_qualified_name.has_value())
|
return m_html_uppercased_qualified_name.ensure([&] { return make_html_uppercased_qualified_name(); });
|
||||||
m_html_uppercased_qualified_name = make_html_uppercased_qualified_name();
|
|
||||||
return m_html_uppercased_qualified_name.value();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Element::play_or_cancel_animations_after_display_property_change()
|
void Element::play_or_cancel_animations_after_display_property_change()
|
||||||
|
|
|
@ -213,9 +213,7 @@ void HTMLTextAreaElement::set_raw_value(Utf16String value)
|
||||||
Utf16String HTMLTextAreaElement::api_value() const
|
Utf16String HTMLTextAreaElement::api_value() const
|
||||||
{
|
{
|
||||||
// The algorithm for obtaining the element's API value is to return the element's raw value, with newlines normalized.
|
// The algorithm for obtaining the element's API value is to return the element's raw value, with newlines normalized.
|
||||||
if (!m_api_value.has_value())
|
return m_api_value.ensure([&] { return Infra::normalize_newlines(m_raw_value); });
|
||||||
m_api_value = Infra::normalize_newlines(m_raw_value);
|
|
||||||
return *m_api_value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
|
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
|
||||||
|
|
|
@ -21,15 +21,14 @@ bool ListOfAvailableImages::Key::operator==(Key const& other) const
|
||||||
|
|
||||||
u32 ListOfAvailableImages::Key::hash() const
|
u32 ListOfAvailableImages::Key::hash() const
|
||||||
{
|
{
|
||||||
if (!cached_hash.has_value()) {
|
return cached_hash.ensure([&] {
|
||||||
u32 url_hash = url.hash();
|
u32 url_hash = url.hash();
|
||||||
u32 mode_hash = static_cast<u32>(mode);
|
u32 mode_hash = static_cast<u32>(mode);
|
||||||
u32 origin_hash = 0;
|
u32 origin_hash = 0;
|
||||||
if (origin.has_value())
|
if (origin.has_value())
|
||||||
origin_hash = Traits<URL::Origin>::hash(origin.value());
|
origin_hash = Traits<URL::Origin>::hash(origin.value());
|
||||||
cached_hash = pair_int_hash(url_hash, pair_int_hash(mode_hash, origin_hash));
|
return pair_int_hash(url_hash, pair_int_hash(mode_hash, origin_hash));
|
||||||
}
|
});
|
||||||
return cached_hash.value();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListOfAvailableImages::visit_edges(JS::Cell::Visitor& visitor)
|
void ListOfAvailableImages::visit_edges(JS::Cell::Visitor& visitor)
|
||||||
|
|
|
@ -45,11 +45,9 @@ void ImageBox::prepare_for_replaced_layout()
|
||||||
set_natural_height(0);
|
set_natural_height(0);
|
||||||
} else {
|
} else {
|
||||||
auto font = Platform::FontPlugin::the().default_font(12);
|
auto font = Platform::FontPlugin::the().default_font(12);
|
||||||
CSSPixels alt_text_width = 0;
|
CSSPixels alt_text_width = m_cached_alt_text_width.ensure([&] {
|
||||||
if (!m_cached_alt_text_width.has_value())
|
return CSSPixels::nearest_value_for(font->width(alt));
|
||||||
m_cached_alt_text_width = CSSPixels::nearest_value_for(font->width(alt));
|
});
|
||||||
alt_text_width = m_cached_alt_text_width.value();
|
|
||||||
|
|
||||||
set_natural_width(alt_text_width + 16);
|
set_natural_width(alt_text_width + 16);
|
||||||
set_natural_height(CSSPixels::nearest_value_for(font->pixel_size()) + 16);
|
set_natural_height(CSSPixels::nearest_value_for(font->pixel_size()) + 16);
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,14 +68,12 @@ CSS::Display Paintable::display() const
|
||||||
|
|
||||||
PaintableBox* Paintable::containing_block() const
|
PaintableBox* Paintable::containing_block() const
|
||||||
{
|
{
|
||||||
if (!m_containing_block.has_value()) {
|
return m_containing_block.ensure([&] -> GC::Ptr<PaintableBox> {
|
||||||
auto containing_layout_box = m_layout_node->containing_block();
|
auto containing_layout_box = m_layout_node->containing_block();
|
||||||
if (containing_layout_box)
|
if (!containing_layout_box)
|
||||||
m_containing_block = const_cast<PaintableBox*>(containing_layout_box->paintable_box());
|
return nullptr;
|
||||||
else
|
return const_cast<PaintableBox*>(containing_layout_box->paintable_box());
|
||||||
m_containing_block = nullptr;
|
});
|
||||||
}
|
|
||||||
return *m_containing_block;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CSS::ImmutableComputedValues const& Paintable::computed_values() const
|
CSS::ImmutableComputedValues const& Paintable::computed_values() const
|
||||||
|
|
|
@ -24,13 +24,12 @@ public:
|
||||||
|
|
||||||
CSSPixelPoint cumulative_offset() const
|
CSSPixelPoint cumulative_offset() const
|
||||||
{
|
{
|
||||||
if (!m_cached_cumulative_offset.has_value()) {
|
return m_cached_cumulative_offset.ensure([&] {
|
||||||
m_cached_cumulative_offset = m_own_offset;
|
auto offset = m_own_offset;
|
||||||
if (m_parent) {
|
if (m_parent)
|
||||||
m_cached_cumulative_offset.value() += m_parent->cumulative_offset();
|
offset += m_parent->cumulative_offset();
|
||||||
}
|
return offset;
|
||||||
}
|
});
|
||||||
return m_cached_cumulative_offset.value();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CSSPixelPoint own_offset() const { return m_own_offset; }
|
CSSPixelPoint own_offset() const { return m_own_offset; }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue