Commit 92b80c31 authored by Kenton Varda's avatar Kenton Varda

Add OneOf::tryGet() returning Maybe, and OneOf::allHandled() to catch missing branches.

parent d16f0071
......@@ -31,6 +31,9 @@ TEST(OneOf, Basic) {
EXPECT_FALSE(var.is<int>());
EXPECT_FALSE(var.is<float>());
EXPECT_FALSE(var.is<String>());
EXPECT_TRUE(var.tryGet<int>() == nullptr);
EXPECT_TRUE(var.tryGet<float>() == nullptr);
EXPECT_TRUE(var.tryGet<String>() == nullptr);
var.init<int>(123);
......@@ -44,6 +47,10 @@ TEST(OneOf, Basic) {
EXPECT_ANY_THROW(var.get<String>());
#endif
EXPECT_EQ(123, KJ_ASSERT_NONNULL(var.tryGet<int>()));
EXPECT_TRUE(var.tryGet<float>() == nullptr);
EXPECT_TRUE(var.tryGet<String>() == nullptr);
var.init<String>(kj::str("foo"));
EXPECT_FALSE(var.is<int>());
......@@ -52,6 +59,10 @@ TEST(OneOf, Basic) {
EXPECT_EQ("foo", var.get<String>());
EXPECT_TRUE(var.tryGet<int>() == nullptr);
EXPECT_TRUE(var.tryGet<float>() == nullptr);
EXPECT_EQ("foo", KJ_ASSERT_NONNULL(var.tryGet<String>()));
OneOf<int, float, String> var2 = kj::mv(var);
EXPECT_EQ("", var.get<String>());
EXPECT_EQ("foo", var2.get<String>());
......@@ -59,6 +70,11 @@ TEST(OneOf, Basic) {
var = kj::mv(var2);
EXPECT_EQ("foo", var.get<String>());
EXPECT_EQ("", var2.get<String>());
if (false) {
var.allHandled<3>();
// var.allHandled<2>(); // doesn't compile
}
}
TEST(OneOf, Copy) {
......
......@@ -81,6 +81,25 @@ public:
return *reinterpret_cast<T*>(space);
}
template <typename T>
Maybe<T&> tryGet() {
if (is<T>()) {
return *reinterpret_cast<T*>(space);
} else {
return nullptr;
}
}
template <uint i>
KJ_NORETURN(void allHandled()) {
// After a series of if/else blocks handling each variant of the OneOf, have the final else
// block call allHandled<n>() where n is the number of variants. This will fail to compile
// if new variants are added in the future.
static_assert(i == sizeof...(Variants), "new OneOf variants need to be handled here");
KJ_UNREACHABLE;
}
private:
uint tag;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment