diff --git a/AK/Optional.h b/AK/Optional.h index 2620498fe7a..59b1dd1013e 100644 --- a/AK/Optional.h +++ b/AK/Optional.h @@ -227,6 +227,38 @@ public: return move(fallback); } + template + [[nodiscard]] ALWAYS_INLINE T value_or_lazy_evaluated(Callback callback) const + { + if (m_has_value) + return value(); + return callback(); + } + + template + [[nodiscard]] ALWAYS_INLINE Optional value_or_lazy_evaluated_optional(Callback callback) const + { + if (m_has_value) + return value(); + return callback(); + } + + template + [[nodiscard]] ALWAYS_INLINE ErrorOr try_value_or_lazy_evaluated(Callback callback) const + { + if (m_has_value) + return value(); + return TRY(callback()); + } + + template + [[nodiscard]] ALWAYS_INLINE ErrorOr> 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 + [[nodiscard]] ALWAYS_INLINE T value_or_lazy_evaluated(Callback callback) const + { + if (m_pointer != nullptr) + return value(); + return callback(); + } + + template + [[nodiscard]] ALWAYS_INLINE Optional value_or_lazy_evaluated_optional(Callback callback) const + { + if (m_pointer != nullptr) + return value(); + return callback(); + } + + template + [[nodiscard]] ALWAYS_INLINE ErrorOr try_value_or_lazy_evaluated(Callback callback) const + { + if (m_pointer != nullptr) + return value(); + return TRY(callback()); + } + + template + [[nodiscard]] ALWAYS_INLINE ErrorOr> try_value_or_lazy_evaluated_optional(Callback callback) const + { + if (m_pointer != nullptr) + return value(); + return TRY(callback()); + } + template()(declval())), auto IsErrorOr = IsSpecializationOf, typename OptionalType = Optional>> ALWAYS_INLINE Conditional, OptionalType> map(F&& mapper) {