1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00

Add Optional::map.

This commit is contained in:
John McCall 2019-12-16 11:57:03 -05:00
parent 0104e43c85
commit 77a4abe86e

View File

@ -267,6 +267,14 @@ public:
return hasValue() ? getValue() : std::forward<U>(value);
}
/// Apply a function to the value if present; otherwise return None.
template <class Function>
auto map(const Function &F) const
-> Optional<decltype(F(getValue()))> {
if (*this) return F(getValue());
return None;
}
#if LLVM_HAS_RVALUE_REFERENCE_THIS
T &&getValue() && { return std::move(Storage.getValue()); }
T &&operator*() && { return std::move(Storage.getValue()); }
@ -275,6 +283,14 @@ public:
T getValueOr(U &&value) && {
return hasValue() ? std::move(getValue()) : std::forward<U>(value);
}
/// Apply a function to the value if present; otherwise return None.
template <class Function>
auto map(const Function &F) &&
-> Optional<decltype(F(std::move(*this).getValue()))> {
if (*this) return F(std::move(*this).getValue());
return None;
}
#endif
};