mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47:45 +00:00
AK: Add a Optional::value_or_lazy_evaluated(constructor_function) API
This allows the user to avoid constructing the default value if the optional already contains a value. This is used by the Jakt runtime.
This commit is contained in:
parent
cc948d06a6
commit
21c2d8bd98
1 changed files with 64 additions and 0 deletions
|
@ -227,6 +227,38 @@ public:
|
||||||
return move(fallback);
|
return move(fallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
[[nodiscard]] ALWAYS_INLINE T value_or_lazy_evaluated(Callback callback) const
|
||||||
|
{
|
||||||
|
if (m_has_value)
|
||||||
|
return value();
|
||||||
|
return callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
[[nodiscard]] ALWAYS_INLINE Optional<T> value_or_lazy_evaluated_optional(Callback callback) const
|
||||||
|
{
|
||||||
|
if (m_has_value)
|
||||||
|
return value();
|
||||||
|
return callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
[[nodiscard]] ALWAYS_INLINE ErrorOr<T> try_value_or_lazy_evaluated(Callback callback) const
|
||||||
|
{
|
||||||
|
if (m_has_value)
|
||||||
|
return value();
|
||||||
|
return TRY(callback());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
[[nodiscard]] ALWAYS_INLINE ErrorOr<Optional<T>> try_value_or_lazy_evaluated_optional(Callback callback) const
|
||||||
|
{
|
||||||
|
if (m_has_value)
|
||||||
|
return value();
|
||||||
|
return TRY(callback());
|
||||||
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE T const& operator*() const { return value(); }
|
ALWAYS_INLINE T const& operator*() const { return value(); }
|
||||||
ALWAYS_INLINE T& operator*() { return value(); }
|
ALWAYS_INLINE T& operator*() { return value(); }
|
||||||
|
|
||||||
|
@ -424,6 +456,38 @@ public:
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
[[nodiscard]] ALWAYS_INLINE T value_or_lazy_evaluated(Callback callback) const
|
||||||
|
{
|
||||||
|
if (m_pointer != nullptr)
|
||||||
|
return value();
|
||||||
|
return callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
[[nodiscard]] ALWAYS_INLINE Optional<T> value_or_lazy_evaluated_optional(Callback callback) const
|
||||||
|
{
|
||||||
|
if (m_pointer != nullptr)
|
||||||
|
return value();
|
||||||
|
return callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
[[nodiscard]] ALWAYS_INLINE ErrorOr<T> try_value_or_lazy_evaluated(Callback callback) const
|
||||||
|
{
|
||||||
|
if (m_pointer != nullptr)
|
||||||
|
return value();
|
||||||
|
return TRY(callback());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
[[nodiscard]] ALWAYS_INLINE ErrorOr<Optional<T>> try_value_or_lazy_evaluated_optional(Callback callback) const
|
||||||
|
{
|
||||||
|
if (m_pointer != nullptr)
|
||||||
|
return value();
|
||||||
|
return TRY(callback());
|
||||||
|
}
|
||||||
|
|
||||||
template<typename F, typename MappedType = decltype(declval<F>()(declval<T&>())), auto IsErrorOr = IsSpecializationOf<MappedType, ErrorOr>, typename OptionalType = Optional<ConditionallyResultType<IsErrorOr, MappedType>>>
|
template<typename F, typename MappedType = decltype(declval<F>()(declval<T&>())), auto IsErrorOr = IsSpecializationOf<MappedType, ErrorOr>, typename OptionalType = Optional<ConditionallyResultType<IsErrorOr, MappedType>>>
|
||||||
ALWAYS_INLINE Conditional<IsErrorOr, ErrorOr<OptionalType>, OptionalType> map(F&& mapper)
|
ALWAYS_INLINE Conditional<IsErrorOr, ErrorOr<OptionalType>, OptionalType> map(F&& mapper)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue