forked from OSchip/llvm-project
Add Optional::map.
This commit is contained in:
parent
a9db0d9f17
commit
2e2d142efe
|
@ -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
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue