forked from OSchip/llvm-project
[ADT] Implement Any::has_value
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. Differential Revision: https://reviews.llvm.org/D132278
This commit is contained in:
parent
0e0e638249
commit
e15359debf
|
@ -101,6 +101,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hasValue() const { return !!Storage; }
|
bool hasValue() const { return !!Storage; }
|
||||||
|
bool has_value() const { return !!Storage; }
|
||||||
|
|
||||||
void reset() { Storage.reset(); }
|
void reset() { Storage.reset(); }
|
||||||
|
|
||||||
|
|
|
@ -23,54 +23,54 @@ TEST(AnyTest, ConstructionAndAssignment) {
|
||||||
llvm::Any E{3.7};
|
llvm::Any E{3.7};
|
||||||
|
|
||||||
// An empty Any is not anything.
|
// An empty Any is not anything.
|
||||||
EXPECT_FALSE(A.hasValue());
|
EXPECT_FALSE(A.has_value());
|
||||||
EXPECT_FALSE(any_isa<int>(A));
|
EXPECT_FALSE(any_isa<int>(A));
|
||||||
|
|
||||||
// An int is an int but not something else.
|
// 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_TRUE(any_isa<int>(B));
|
||||||
EXPECT_FALSE(any_isa<float>(B));
|
EXPECT_FALSE(any_isa<float>(B));
|
||||||
|
|
||||||
EXPECT_TRUE(C.hasValue());
|
EXPECT_TRUE(C.has_value());
|
||||||
EXPECT_TRUE(any_isa<int>(C));
|
EXPECT_TRUE(any_isa<int>(C));
|
||||||
|
|
||||||
// A const char * is a const char * but not an int.
|
// 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_TRUE(any_isa<const char *>(D));
|
||||||
EXPECT_FALSE(any_isa<int>(D));
|
EXPECT_FALSE(any_isa<int>(D));
|
||||||
|
|
||||||
// A double is a double but not a float.
|
// 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_TRUE(any_isa<double>(E));
|
||||||
EXPECT_FALSE(any_isa<float>(E));
|
EXPECT_FALSE(any_isa<float>(E));
|
||||||
|
|
||||||
// After copy constructing from an int, the new item and old item are both
|
// After copy constructing from an int, the new item and old item are both
|
||||||
// ints.
|
// ints.
|
||||||
llvm::Any F(B);
|
llvm::Any F(B);
|
||||||
EXPECT_TRUE(B.hasValue());
|
EXPECT_TRUE(B.has_value());
|
||||||
EXPECT_TRUE(F.hasValue());
|
EXPECT_TRUE(F.has_value());
|
||||||
EXPECT_TRUE(any_isa<int>(F));
|
EXPECT_TRUE(any_isa<int>(F));
|
||||||
EXPECT_TRUE(any_isa<int>(B));
|
EXPECT_TRUE(any_isa<int>(B));
|
||||||
|
|
||||||
// After move constructing from an int, the new item is an int and the old one
|
// After move constructing from an int, the new item is an int and the old one
|
||||||
// isn't.
|
// isn't.
|
||||||
llvm::Any G(std::move(C));
|
llvm::Any G(std::move(C));
|
||||||
EXPECT_FALSE(C.hasValue());
|
EXPECT_FALSE(C.has_value());
|
||||||
EXPECT_TRUE(G.hasValue());
|
EXPECT_TRUE(G.has_value());
|
||||||
EXPECT_TRUE(any_isa<int>(G));
|
EXPECT_TRUE(any_isa<int>(G));
|
||||||
EXPECT_FALSE(any_isa<int>(C));
|
EXPECT_FALSE(any_isa<int>(C));
|
||||||
|
|
||||||
// After copy-assigning from an int, the new item and old item are both ints.
|
// After copy-assigning from an int, the new item and old item are both ints.
|
||||||
A = F;
|
A = F;
|
||||||
EXPECT_TRUE(A.hasValue());
|
EXPECT_TRUE(A.has_value());
|
||||||
EXPECT_TRUE(F.hasValue());
|
EXPECT_TRUE(F.has_value());
|
||||||
EXPECT_TRUE(any_isa<int>(A));
|
EXPECT_TRUE(any_isa<int>(A));
|
||||||
EXPECT_TRUE(any_isa<int>(F));
|
EXPECT_TRUE(any_isa<int>(F));
|
||||||
|
|
||||||
// After move-assigning from an int, the new item and old item are both ints.
|
// After move-assigning from an int, the new item and old item are both ints.
|
||||||
B = std::move(G);
|
B = std::move(G);
|
||||||
EXPECT_TRUE(B.hasValue());
|
EXPECT_TRUE(B.has_value());
|
||||||
EXPECT_FALSE(G.hasValue());
|
EXPECT_FALSE(G.has_value());
|
||||||
EXPECT_TRUE(any_isa<int>(B));
|
EXPECT_TRUE(any_isa<int>(B));
|
||||||
EXPECT_FALSE(any_isa<int>(G));
|
EXPECT_FALSE(any_isa<int>(G));
|
||||||
}
|
}
|
||||||
|
@ -111,7 +111,7 @@ TEST(AnyTest, GoodAnyCast) {
|
||||||
// Make sure we can any_cast from an rvalue and that it's properly destroyed
|
// Make sure we can any_cast from an rvalue and that it's properly destroyed
|
||||||
// in the process.
|
// in the process.
|
||||||
EXPECT_EQ(8, llvm::any_cast<int>(std::move(E)));
|
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
|
// Make sure moving from pointers gives back pointers, and that we can modify
|
||||||
// the underlying value through those pointers.
|
// the underlying value through those pointers.
|
||||||
|
|
Loading…
Reference in New Issue