[PATCH] D132278: [ADT] Implement Any::has_value

Kazu Hirata via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 19 21:29:50 PDT 2022


kazu created this revision.
Herald added a project: All.
kazu requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This patch implements Any::has_value for consistency with std::any in
C++17.

My plan is to deprecate Any::hasValue after migrating all of its uses
to Any::has_value.  Since I am about to do so, this patch simply
replaces hasValue with has_value in the unit test instead of adding
tests for has_value.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132278

Files:
  llvm/include/llvm/ADT/Any.h
  llvm/unittests/ADT/AnyTest.cpp


Index: llvm/unittests/ADT/AnyTest.cpp
===================================================================
--- llvm/unittests/ADT/AnyTest.cpp
+++ llvm/unittests/ADT/AnyTest.cpp
@@ -23,54 +23,54 @@
   llvm::Any E{3.7};
 
   // An empty Any is not anything.
-  EXPECT_FALSE(A.hasValue());
+  EXPECT_FALSE(A.has_value());
   EXPECT_FALSE(any_isa<int>(A));
 
   // An int is an int but not something else.
-  EXPECT_TRUE(B.hasValue());
+  EXPECT_TRUE(B.has_value());
   EXPECT_TRUE(any_isa<int>(B));
   EXPECT_FALSE(any_isa<float>(B));
 
-  EXPECT_TRUE(C.hasValue());
+  EXPECT_TRUE(C.has_value());
   EXPECT_TRUE(any_isa<int>(C));
 
   // A const char * is a const char * but not an int.
-  EXPECT_TRUE(D.hasValue());
+  EXPECT_TRUE(D.has_value());
   EXPECT_TRUE(any_isa<const char *>(D));
   EXPECT_FALSE(any_isa<int>(D));
 
   // A double is a double but not a float.
-  EXPECT_TRUE(E.hasValue());
+  EXPECT_TRUE(E.has_value());
   EXPECT_TRUE(any_isa<double>(E));
   EXPECT_FALSE(any_isa<float>(E));
 
   // After copy constructing from an int, the new item and old item are both
   // ints.
   llvm::Any F(B);
-  EXPECT_TRUE(B.hasValue());
-  EXPECT_TRUE(F.hasValue());
+  EXPECT_TRUE(B.has_value());
+  EXPECT_TRUE(F.has_value());
   EXPECT_TRUE(any_isa<int>(F));
   EXPECT_TRUE(any_isa<int>(B));
 
   // After move constructing from an int, the new item is an int and the old one
   // isn't.
   llvm::Any G(std::move(C));
-  EXPECT_FALSE(C.hasValue());
-  EXPECT_TRUE(G.hasValue());
+  EXPECT_FALSE(C.has_value());
+  EXPECT_TRUE(G.has_value());
   EXPECT_TRUE(any_isa<int>(G));
   EXPECT_FALSE(any_isa<int>(C));
 
   // After copy-assigning from an int, the new item and old item are both ints.
   A = F;
-  EXPECT_TRUE(A.hasValue());
-  EXPECT_TRUE(F.hasValue());
+  EXPECT_TRUE(A.has_value());
+  EXPECT_TRUE(F.has_value());
   EXPECT_TRUE(any_isa<int>(A));
   EXPECT_TRUE(any_isa<int>(F));
 
   // After move-assigning from an int, the new item and old item are both ints.
   B = std::move(G);
-  EXPECT_TRUE(B.hasValue());
-  EXPECT_FALSE(G.hasValue());
+  EXPECT_TRUE(B.has_value());
+  EXPECT_FALSE(G.has_value());
   EXPECT_TRUE(any_isa<int>(B));
   EXPECT_FALSE(any_isa<int>(G));
 }
@@ -111,7 +111,7 @@
   // Make sure we can any_cast from an rvalue and that it's properly destroyed
   // in the process.
   EXPECT_EQ(8, llvm::any_cast<int>(std::move(E)));
-  EXPECT_TRUE(E.hasValue());
+  EXPECT_TRUE(E.has_value());
 
   // Make sure moving from pointers gives back pointers, and that we can modify
   // the underlying value through those pointers.
Index: llvm/include/llvm/ADT/Any.h
===================================================================
--- llvm/include/llvm/ADT/Any.h
+++ llvm/include/llvm/ADT/Any.h
@@ -101,6 +101,7 @@
   }
 
   bool hasValue() const { return !!Storage; }
+  bool has_value() const { return !!Storage; }
 
   void reset() { Storage.reset(); }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132278.454161.patch
Type: text/x-patch
Size: 2895 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220820/00c3bcd0/attachment-0001.bin>


More information about the llvm-commits mailing list