mirror of
https://github.com/SerenityOS/serenity
synced 2024-07-23 11:04:40 +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
|
@ -227,6 +227,38 @@ public:
|
|||
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& operator*() { return value(); }
|
||||
|
||||
|
@ -424,6 +456,38 @@ public:
|
|||
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>>>
|
||||
ALWAYS_INLINE Conditional<IsErrorOr, ErrorOr<OptionalType>, OptionalType> map(F&& mapper)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue