diff options
author | Karol Kosek <krkk@serenityos.org> | 2023-05-14 21:20:02 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-05-15 06:39:22 +0200 |
commit | acf8e19eac8612ae76778a64f1a5021c71342b63 (patch) | |
tree | 05b7a7320c2a8f8c8f3f3476b35ef181372b71d6 /Userland/Libraries | |
parent | 631fe129e9477267de144519c01663bf3816595f (diff) | |
download | serenity-acf8e19eac8612ae76778a64f1a5021c71342b63.zip |
LibCore: Use StringView in Object::find_{child,descendant}_of_type_named
It's unnecessary to allocate a string when we only want to compare it
with another string.
This change also adds a helper for string literals, so that we won't
need to add -sv suffix everywhere. :^)
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibCore/Object.h | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/Userland/Libraries/LibCore/Object.h b/Userland/Libraries/LibCore/Object.h index 8e5503776f..cea9759599 100644 --- a/Userland/Libraries/LibCore/Object.h +++ b/Userland/Libraries/LibCore/Object.h @@ -128,13 +128,27 @@ public: requires IsBaseOf<Object, T>; template<typename T> - T* find_child_of_type_named(DeprecatedString const&) + T* find_child_of_type_named(StringView) requires IsBaseOf<Object, T>; + template<typename T, size_t N> + ALWAYS_INLINE T* find_child_of_type_named(char const (&string_literal)[N]) + requires IsBaseOf<Object, T> + { + return find_child_of_type_named<T>(StringView { string_literal, N - 1 }); + } + template<typename T> - T* find_descendant_of_type_named(DeprecatedString const&) + T* find_descendant_of_type_named(StringView) requires IsBaseOf<Object, T>; + template<typename T, size_t N> + ALWAYS_INLINE T* find_descendant_of_type_named(char const (&string_literal)[N]) + requires IsBaseOf<Object, T> + { + return find_descendant_of_type_named<T>(StringView { string_literal, N - 1 }); + } + bool is_ancestor_of(Object const&) const; Object* parent() { return m_parent; } @@ -243,7 +257,7 @@ requires IsBaseOf<Object, T> } template<typename T> -T* Object::find_child_of_type_named(DeprecatedString const& name) +T* Object::find_child_of_type_named(StringView name) requires IsBaseOf<Object, T> { T* found_child = nullptr; @@ -259,7 +273,7 @@ requires IsBaseOf<Object, T> } template<typename T> -T* Object::find_descendant_of_type_named(DeprecatedString const& name) +T* Object::find_descendant_of_type_named(StringView name) requires IsBaseOf<Object, T> { if (is<T>(*this) && this->name() == name) { |