[llvm] 2a47485 - [ADT] Implement Optional::transform
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 13 11:48:35 PDT 2022
Author: Kazu Hirata
Date: 2022-08-13T11:48:25-07:00
New Revision: 2a4748576e4f734a15c9b0553eec602dd6f03633
URL: https://github.com/llvm/llvm-project/commit/2a4748576e4f734a15c9b0553eec602dd6f03633
DIFF: https://github.com/llvm/llvm-project/commit/2a4748576e4f734a15c9b0553eec602dd6f03633.diff
LOG: [ADT] Implement Optional::transform
This patch implements Optional::transform for consistency with
std::optional::transform in C++23.
Note that the new function is identical to Optional::map. My plan is
to deprecate Optional::map after migrating all of its uses to
Optional::transform.
Differential Revision: https://reviews.llvm.org/D131829
Added:
Modified:
llvm/include/llvm/ADT/Optional.h
llvm/unittests/ADT/OptionalTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/Optional.h b/llvm/include/llvm/ADT/Optional.h
index 1862a2d703aad..874183ce190e5 100644
--- a/llvm/include/llvm/ADT/Optional.h
+++ b/llvm/include/llvm/ADT/Optional.h
@@ -342,6 +342,12 @@ template <typename T> class Optional {
/// Apply a function to the value if present; otherwise return None.
template <class Function>
+ auto transform(const Function &F) const & -> Optional<decltype(F(value()))> {
+ if (*this)
+ return F(value());
+ return None;
+ }
+ template <class Function>
auto map(const Function &F) const & -> Optional<decltype(F(value()))> {
if (*this)
return F(value());
@@ -365,6 +371,13 @@ template <typename T> class Optional {
/// Apply a function to the value if present; otherwise return None.
template <class Function>
+ auto transform(
+ const Function &F) && -> Optional<decltype(F(std::move(*this).value()))> {
+ if (*this)
+ return F(std::move(*this).value());
+ return None;
+ }
+ template <class Function>
auto map(const Function &F)
&& -> Optional<decltype(F(std::move(*this).value()))> {
if (*this)
diff --git a/llvm/unittests/ADT/OptionalTest.cpp b/llvm/unittests/ADT/OptionalTest.cpp
index 3db84ed4c3306..c583ff0df9af1 100644
--- a/llvm/unittests/ADT/OptionalTest.cpp
+++ b/llvm/unittests/ADT/OptionalTest.cpp
@@ -603,6 +603,40 @@ TEST(OptionalTest, MoveValueOr) {
EXPECT_EQ(2u, MoveOnly::Destructions);
}
+TEST(OptionalTest, Transform) {
+ Optional<int> A;
+
+ Optional<int> B = A.transform([&](int N) { return N + 1; });
+ EXPECT_FALSE(B.has_value());
+
+ A = 3;
+ Optional<int> C = A.transform([&](int N) { return N + 1; });
+ EXPECT_TRUE(C.has_value());
+ EXPECT_EQ(4, C.value());
+}
+
+TEST(OptionalTest, MoveTransform) {
+ Optional<MoveOnly> A;
+
+ MoveOnly::ResetCounts();
+ Optional<int> B =
+ std::move(A).transform([&](const MoveOnly &M) { return M.val + 2; });
+ EXPECT_FALSE(B.has_value());
+ EXPECT_EQ(0u, MoveOnly::MoveConstructions);
+ EXPECT_EQ(0u, MoveOnly::MoveAssignments);
+ EXPECT_EQ(0u, MoveOnly::Destructions);
+
+ A = MoveOnly(5);
+ MoveOnly::ResetCounts();
+ Optional<int> C =
+ std::move(A).transform([&](const MoveOnly &M) { return M.val + 2; });
+ EXPECT_TRUE(C.has_value());
+ EXPECT_EQ(7, C.value());
+ EXPECT_EQ(0u, MoveOnly::MoveConstructions);
+ EXPECT_EQ(0u, MoveOnly::MoveAssignments);
+ EXPECT_EQ(0u, MoveOnly::Destructions);
+}
+
struct EqualTo {
template <typename T, typename U> static bool apply(const T &X, const U &Y) {
return X == Y;
More information about the llvm-commits
mailing list