mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-05-05 06:32:30 +02:00
AK: Add SentinelOptional
We specialize `Optional<T>` for value types that inherently support some kind of "empty" value or whose value range allow for a unlikely to be useful sentinel value that can mean "empty", instead of the boolean flag a regular Optional<T> needs to store. Because of padding, this often means saving 4 to 8 bytes per instance. By extending the new `SentinelOptional<T, Traits>`, these specializations are significantly simplified to just having to define what the sentinel value is, and how to identify a sentinel value.
This commit is contained in:
committed by
Jelle Raaijmakers
parent
9e245b014c
commit
e123d48043
Notes:
github-actions[bot]
2026-03-20 11:04:58 +00:00
Author: https://github.com/gmta Commit: https://github.com/LadybirdBrowser/ladybird/commit/e123d480436 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/8508 Reviewed-by: https://github.com/konradekk
@@ -179,7 +179,7 @@ public:
|
||||
|
||||
private:
|
||||
friend Traits<PropertyKey>;
|
||||
friend class Optional<PropertyKey>;
|
||||
friend struct AK::SentinelOptionalTraits<PropertyKey>;
|
||||
|
||||
enum class ShouldMakeEmptyOptional {
|
||||
Indeed,
|
||||
@@ -242,99 +242,15 @@ struct Formatter<JS::PropertyKey> : Formatter<Utf16String> {
|
||||
};
|
||||
|
||||
template<>
|
||||
class Optional<JS::PropertyKey> : public OptionalBase<JS::PropertyKey> {
|
||||
template<typename U>
|
||||
friend class Optional;
|
||||
struct SentinelOptionalTraits<JS::PropertyKey> {
|
||||
static JS::PropertyKey sentinel_value() { return JS::PropertyKey { JS::PropertyKey::ShouldMakeEmptyOptional::Indeed }; }
|
||||
static bool is_sentinel(JS::PropertyKey const& value) { return value.is_empty_optional(); }
|
||||
};
|
||||
|
||||
template<>
|
||||
class Optional<JS::PropertyKey> : public SentinelOptional<JS::PropertyKey> {
|
||||
public:
|
||||
using ValueType = JS::PropertyKey;
|
||||
|
||||
Optional() = default;
|
||||
|
||||
template<SameAs<OptionalNone> V>
|
||||
Optional(V) { }
|
||||
|
||||
Optional(Optional<JS::PropertyKey> const& other)
|
||||
{
|
||||
if (other.has_value())
|
||||
m_value = other.m_value;
|
||||
}
|
||||
|
||||
Optional(Optional&& other)
|
||||
: m_value(other.m_value)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U = JS::PropertyKey>
|
||||
requires(!IsSame<OptionalNone, RemoveCVReference<U>>)
|
||||
explicit(!IsConvertible<U&&, JS::PropertyKey>) Optional(U&& value)
|
||||
requires(!IsSame<RemoveCVReference<U>, Optional<JS::PropertyKey>> && IsConstructible<JS::PropertyKey, U &&>)
|
||||
: m_value(forward<U>(value))
|
||||
{
|
||||
}
|
||||
|
||||
template<SameAs<OptionalNone> V>
|
||||
Optional& operator=(V)
|
||||
{
|
||||
clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Optional& operator=(Optional const& other)
|
||||
{
|
||||
if (this != &other) {
|
||||
clear();
|
||||
m_value = other.m_value;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Optional& operator=(Optional&& other)
|
||||
{
|
||||
if (this != &other) {
|
||||
clear();
|
||||
m_value = other.m_value;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
m_value = JS::PropertyKey { JS::PropertyKey::ShouldMakeEmptyOptional::Indeed };
|
||||
}
|
||||
|
||||
[[nodiscard]] bool has_value() const
|
||||
{
|
||||
return !m_value.is_empty_optional();
|
||||
}
|
||||
|
||||
[[nodiscard]] JS::PropertyKey& value() &
|
||||
{
|
||||
VERIFY(has_value());
|
||||
return m_value;
|
||||
}
|
||||
|
||||
[[nodiscard]] JS::PropertyKey const& value() const&
|
||||
{
|
||||
VERIFY(has_value());
|
||||
return m_value;
|
||||
}
|
||||
|
||||
[[nodiscard]] JS::PropertyKey value() &&
|
||||
{
|
||||
return release_value();
|
||||
}
|
||||
|
||||
[[nodiscard]] JS::PropertyKey release_value()
|
||||
{
|
||||
VERIFY(has_value());
|
||||
JS::PropertyKey released_value = m_value;
|
||||
clear();
|
||||
return released_value;
|
||||
}
|
||||
|
||||
private:
|
||||
JS::PropertyKey m_value { JS::PropertyKey::ShouldMakeEmptyOptional::Indeed };
|
||||
using SentinelOptional::SentinelOptional;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user