1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-08 05:27:14 +09:00

AK: Allow Optional<{,Fly}String> to be used in constant expressions

This commit is contained in:
Jonne Ransijn 2025-04-10 15:47:41 +02:00 committed by Andrew Kaster
parent a059ab4677
commit e37377600d
Notes: github-actions[bot] 2025-04-23 03:21:28 +00:00
3 changed files with 37 additions and 37 deletions

View file

@ -85,19 +85,19 @@ public:
private: private:
friend class Optional<FlyString>; friend class Optional<FlyString>;
explicit FlyString(nullptr_t) explicit constexpr FlyString(nullptr_t)
: m_data(nullptr) : m_data(nullptr)
{ {
} }
explicit FlyString(Detail::StringBase data) explicit constexpr FlyString(Detail::StringBase data)
: m_data(move(data)) : m_data(move(data))
{ {
} }
Detail::StringBase m_data; Detail::StringBase m_data;
bool is_invalid() const { return m_data.raw(Badge<FlyString> {}) == 0; } constexpr bool is_invalid() const { return m_data.raw(Badge<FlyString> {}) == 0; }
}; };
void did_destroy_fly_string_data(Badge<Detail::StringData>, Detail::StringData const&); void did_destroy_fly_string_data(Badge<Detail::StringData>, Detail::StringData const&);
@ -110,38 +110,38 @@ class Optional<FlyString> : public OptionalBase<FlyString> {
public: public:
using ValueType = FlyString; using ValueType = FlyString;
Optional() = default; constexpr Optional() = default;
template<SameAs<OptionalNone> V> template<SameAs<OptionalNone> V>
Optional(V) { } constexpr Optional(V) { }
Optional(Optional<FlyString> const& other) constexpr Optional(Optional<FlyString> const& other)
{ {
if (other.has_value()) if (other.has_value())
m_value = other.m_value; m_value = other.m_value;
} }
Optional(Optional&& other) constexpr Optional(Optional&& other)
: m_value(other.m_value) : m_value(move(other.m_value))
{ {
} }
template<typename U = FlyString> template<typename U = FlyString>
requires(!IsSame<OptionalNone, RemoveCVReference<U>>) requires(!IsSame<OptionalNone, RemoveCVReference<U>>)
explicit(!IsConvertible<U&&, FlyString>) Optional(U&& value) explicit(!IsConvertible<U&&, FlyString>) constexpr Optional(U&& value)
requires(!IsSame<RemoveCVReference<U>, Optional<FlyString>> && IsConstructible<FlyString, U &&>) requires(!IsSame<RemoveCVReference<U>, Optional<FlyString>> && IsConstructible<FlyString, U &&>)
: m_value(forward<U>(value)) : m_value(forward<U>(value))
{ {
} }
template<SameAs<OptionalNone> V> template<SameAs<OptionalNone> V>
Optional& operator=(V) constexpr Optional& operator=(V)
{ {
clear(); clear();
return *this; return *this;
} }
Optional& operator=(Optional const& other) constexpr Optional& operator=(Optional const& other)
{ {
if (this != &other) { if (this != &other) {
clear(); clear();
@ -150,7 +150,7 @@ public:
return *this; return *this;
} }
Optional& operator=(Optional&& other) constexpr Optional& operator=(Optional&& other)
{ {
if (this != &other) { if (this != &other) {
clear(); clear();
@ -159,37 +159,37 @@ public:
return *this; return *this;
} }
void clear() constexpr void clear()
{ {
m_value = FlyString(nullptr); m_value = FlyString(nullptr);
} }
[[nodiscard]] bool has_value() const [[nodiscard]] constexpr bool has_value() const
{ {
return !m_value.is_invalid(); return !m_value.is_invalid();
} }
[[nodiscard]] FlyString& value() & [[nodiscard]] constexpr FlyString& value() &
{ {
VERIFY(has_value()); VERIFY(has_value());
return m_value; return m_value;
} }
[[nodiscard]] FlyString const& value() const& [[nodiscard]] constexpr FlyString const& value() const&
{ {
VERIFY(has_value()); VERIFY(has_value());
return m_value; return m_value;
} }
[[nodiscard]] FlyString value() && [[nodiscard]] constexpr FlyString value() &&
{ {
return release_value(); return release_value();
} }
[[nodiscard]] FlyString release_value() [[nodiscard]] constexpr FlyString release_value()
{ {
VERIFY(has_value()); VERIFY(has_value());
FlyString released_value = m_value; FlyString released_value = move(m_value);
clear(); clear();
return released_value; return released_value;
} }

View file

@ -227,7 +227,7 @@ private:
using ShortString = Detail::ShortString; using ShortString = Detail::ShortString;
bool is_invalid() const constexpr bool is_invalid() const
{ {
return raw(Badge<String> {}) == 0; return raw(Badge<String> {}) == 0;
} }
@ -251,38 +251,38 @@ class Optional<String> : public OptionalBase<String> {
public: public:
using ValueType = String; using ValueType = String;
Optional() = default; constexpr Optional() = default;
template<SameAs<OptionalNone> V> template<SameAs<OptionalNone> V>
Optional(V) { } constexpr Optional(V) { }
Optional(Optional<String> const& other) constexpr Optional(Optional<String> const& other)
{ {
if (other.has_value()) if (other.has_value())
m_value = other.m_value; m_value = other.m_value;
} }
Optional(Optional&& other) constexpr Optional(Optional&& other)
: m_value(move(other.m_value)) : m_value(move(other.m_value))
{ {
} }
template<typename U = String> template<typename U = String>
requires(!IsSame<OptionalNone, RemoveCVReference<U>>) requires(!IsSame<OptionalNone, RemoveCVReference<U>>)
explicit(!IsConvertible<U&&, String>) Optional(U&& value) explicit(!IsConvertible<U&&, String>) constexpr Optional(U&& value)
requires(!IsSame<RemoveCVReference<U>, Optional<String>> && IsConstructible<String, U &&>) requires(!IsSame<RemoveCVReference<U>, Optional<String>> && IsConstructible<String, U &&>)
: m_value(forward<U>(value)) : m_value(forward<U>(value))
{ {
} }
template<SameAs<OptionalNone> V> template<SameAs<OptionalNone> V>
Optional& operator=(V) constexpr Optional& operator=(V)
{ {
clear(); clear();
return *this; return *this;
} }
Optional& operator=(Optional const& other) constexpr Optional& operator=(Optional const& other)
{ {
if (this != &other) { if (this != &other) {
m_value = other.m_value; m_value = other.m_value;
@ -290,7 +290,7 @@ public:
return *this; return *this;
} }
Optional& operator=(Optional&& other) constexpr Optional& operator=(Optional&& other)
{ {
if (this != &other) { if (this != &other) {
m_value = move(other.m_value); m_value = move(other.m_value);
@ -298,37 +298,37 @@ public:
return *this; return *this;
} }
void clear() constexpr void clear()
{ {
m_value = String(nullptr); m_value = String(nullptr);
} }
[[nodiscard]] bool has_value() const [[nodiscard]] constexpr bool has_value() const
{ {
return !m_value.is_invalid(); return !m_value.is_invalid();
} }
[[nodiscard]] String& value() & [[nodiscard]] constexpr String& value() &
{ {
VERIFY(has_value()); VERIFY(has_value());
return m_value; return m_value;
} }
[[nodiscard]] String const& value() const& [[nodiscard]] constexpr String const& value() const&
{ {
VERIFY(has_value()); VERIFY(has_value());
return m_value; return m_value;
} }
[[nodiscard]] String value() && [[nodiscard]] constexpr String value() &&
{ {
return release_value(); return release_value();
} }
[[nodiscard]] String release_value() [[nodiscard]] constexpr String release_value()
{ {
VERIFY(has_value()); VERIFY(has_value());
String released_value = m_value; String released_value = move(m_value);
clear(); clear();
return released_value; return released_value;
} }

View file

@ -70,8 +70,8 @@ public:
[[nodiscard]] bool operator==(StringBase const&) const; [[nodiscard]] bool operator==(StringBase const&) const;
[[nodiscard]] ALWAYS_INLINE FlatPtr raw(Badge<FlyString>) const { return bit_cast<FlatPtr>(m_impl); } [[nodiscard]] ALWAYS_INLINE constexpr FlatPtr raw(Badge<FlyString>) const { return bit_cast<FlatPtr>(m_impl); }
[[nodiscard]] ALWAYS_INLINE FlatPtr raw(Badge<String>) const { return bit_cast<FlatPtr>(m_impl); } [[nodiscard]] ALWAYS_INLINE constexpr FlatPtr raw(Badge<String>) const { return bit_cast<FlatPtr>(m_impl); }
template<typename Func> template<typename Func>
ALWAYS_INLINE ErrorOr<void> replace_with_new_string(Badge<StringView>, size_t byte_count, Func&& callback) ALWAYS_INLINE ErrorOr<void> replace_with_new_string(Badge<StringView>, size_t byte_count, Func&& callback)