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:
parent
a059ab4677
commit
e37377600d
Notes:
github-actions[bot]
2025-04-23 03:21:28 +00:00
Author: https://github.com/yyny
Commit: e37377600d
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4310
Reviewed-by: https://github.com/ADKaster ✅
Reviewed-by: https://github.com/DanShaders
Reviewed-by: https://github.com/Hendiadyoin1
Reviewed-by: https://github.com/alimpfard ✅
3 changed files with 37 additions and 37 deletions
|
@ -85,19 +85,19 @@ public:
|
|||
private:
|
||||
friend class Optional<FlyString>;
|
||||
|
||||
explicit FlyString(nullptr_t)
|
||||
explicit constexpr FlyString(nullptr_t)
|
||||
: m_data(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
explicit FlyString(Detail::StringBase data)
|
||||
explicit constexpr FlyString(Detail::StringBase data)
|
||||
: m_data(move(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&);
|
||||
|
@ -110,38 +110,38 @@ class Optional<FlyString> : public OptionalBase<FlyString> {
|
|||
public:
|
||||
using ValueType = FlyString;
|
||||
|
||||
Optional() = default;
|
||||
constexpr Optional() = default;
|
||||
|
||||
template<SameAs<OptionalNone> V>
|
||||
Optional(V) { }
|
||||
constexpr Optional(V) { }
|
||||
|
||||
Optional(Optional<FlyString> const& other)
|
||||
constexpr Optional(Optional<FlyString> const& other)
|
||||
{
|
||||
if (other.has_value())
|
||||
m_value = other.m_value;
|
||||
}
|
||||
|
||||
Optional(Optional&& other)
|
||||
: m_value(other.m_value)
|
||||
constexpr Optional(Optional&& other)
|
||||
: m_value(move(other.m_value))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U = FlyString>
|
||||
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 &&>)
|
||||
: m_value(forward<U>(value))
|
||||
{
|
||||
}
|
||||
|
||||
template<SameAs<OptionalNone> V>
|
||||
Optional& operator=(V)
|
||||
constexpr Optional& operator=(V)
|
||||
{
|
||||
clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Optional& operator=(Optional const& other)
|
||||
constexpr Optional& operator=(Optional const& other)
|
||||
{
|
||||
if (this != &other) {
|
||||
clear();
|
||||
|
@ -150,7 +150,7 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
Optional& operator=(Optional&& other)
|
||||
constexpr Optional& operator=(Optional&& other)
|
||||
{
|
||||
if (this != &other) {
|
||||
clear();
|
||||
|
@ -159,37 +159,37 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
void clear()
|
||||
constexpr void clear()
|
||||
{
|
||||
m_value = FlyString(nullptr);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool has_value() const
|
||||
[[nodiscard]] constexpr bool has_value() const
|
||||
{
|
||||
return !m_value.is_invalid();
|
||||
}
|
||||
|
||||
[[nodiscard]] FlyString& value() &
|
||||
[[nodiscard]] constexpr FlyString& value() &
|
||||
{
|
||||
VERIFY(has_value());
|
||||
return m_value;
|
||||
}
|
||||
|
||||
[[nodiscard]] FlyString const& value() const&
|
||||
[[nodiscard]] constexpr FlyString const& value() const&
|
||||
{
|
||||
VERIFY(has_value());
|
||||
return m_value;
|
||||
}
|
||||
|
||||
[[nodiscard]] FlyString value() &&
|
||||
[[nodiscard]] constexpr FlyString value() &&
|
||||
{
|
||||
return release_value();
|
||||
}
|
||||
|
||||
[[nodiscard]] FlyString release_value()
|
||||
[[nodiscard]] constexpr FlyString release_value()
|
||||
{
|
||||
VERIFY(has_value());
|
||||
FlyString released_value = m_value;
|
||||
FlyString released_value = move(m_value);
|
||||
clear();
|
||||
return released_value;
|
||||
}
|
||||
|
|
32
AK/String.h
32
AK/String.h
|
@ -227,7 +227,7 @@ private:
|
|||
|
||||
using ShortString = Detail::ShortString;
|
||||
|
||||
bool is_invalid() const
|
||||
constexpr bool is_invalid() const
|
||||
{
|
||||
return raw(Badge<String> {}) == 0;
|
||||
}
|
||||
|
@ -251,38 +251,38 @@ class Optional<String> : public OptionalBase<String> {
|
|||
public:
|
||||
using ValueType = String;
|
||||
|
||||
Optional() = default;
|
||||
constexpr Optional() = default;
|
||||
|
||||
template<SameAs<OptionalNone> V>
|
||||
Optional(V) { }
|
||||
constexpr Optional(V) { }
|
||||
|
||||
Optional(Optional<String> const& other)
|
||||
constexpr Optional(Optional<String> const& other)
|
||||
{
|
||||
if (other.has_value())
|
||||
m_value = other.m_value;
|
||||
}
|
||||
|
||||
Optional(Optional&& other)
|
||||
constexpr Optional(Optional&& other)
|
||||
: m_value(move(other.m_value))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U = String>
|
||||
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 &&>)
|
||||
: m_value(forward<U>(value))
|
||||
{
|
||||
}
|
||||
|
||||
template<SameAs<OptionalNone> V>
|
||||
Optional& operator=(V)
|
||||
constexpr Optional& operator=(V)
|
||||
{
|
||||
clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Optional& operator=(Optional const& other)
|
||||
constexpr Optional& operator=(Optional const& other)
|
||||
{
|
||||
if (this != &other) {
|
||||
m_value = other.m_value;
|
||||
|
@ -290,7 +290,7 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
Optional& operator=(Optional&& other)
|
||||
constexpr Optional& operator=(Optional&& other)
|
||||
{
|
||||
if (this != &other) {
|
||||
m_value = move(other.m_value);
|
||||
|
@ -298,37 +298,37 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
void clear()
|
||||
constexpr void clear()
|
||||
{
|
||||
m_value = String(nullptr);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool has_value() const
|
||||
[[nodiscard]] constexpr bool has_value() const
|
||||
{
|
||||
return !m_value.is_invalid();
|
||||
}
|
||||
|
||||
[[nodiscard]] String& value() &
|
||||
[[nodiscard]] constexpr String& value() &
|
||||
{
|
||||
VERIFY(has_value());
|
||||
return m_value;
|
||||
}
|
||||
|
||||
[[nodiscard]] String const& value() const&
|
||||
[[nodiscard]] constexpr String const& value() const&
|
||||
{
|
||||
VERIFY(has_value());
|
||||
return m_value;
|
||||
}
|
||||
|
||||
[[nodiscard]] String value() &&
|
||||
[[nodiscard]] constexpr String value() &&
|
||||
{
|
||||
return release_value();
|
||||
}
|
||||
|
||||
[[nodiscard]] String release_value()
|
||||
[[nodiscard]] constexpr String release_value()
|
||||
{
|
||||
VERIFY(has_value());
|
||||
String released_value = m_value;
|
||||
String released_value = move(m_value);
|
||||
clear();
|
||||
return released_value;
|
||||
}
|
||||
|
|
|
@ -70,8 +70,8 @@ public:
|
|||
|
||||
[[nodiscard]] bool operator==(StringBase const&) const;
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE 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<FlyString>) 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>
|
||||
ALWAYS_INLINE ErrorOr<void> replace_with_new_string(Badge<StringView>, size_t byte_count, Func&& callback)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue