Commit ee7c1b0c authored by Kenton Varda's avatar Kenton Varda

Actually test weak pointers (and make them work).

parent 8b8f8a2f
......@@ -884,6 +884,16 @@ TEST(Capability, CapabilityServerSet) {
EXPECT_TRUE(resolved1);
EXPECT_TRUE(resolved2);
// Check weak pointer.
{
auto restored = KJ_ASSERT_NONNULL(client2Weak->get());
EXPECT_EQ(&server2, &KJ_ASSERT_NONNULL(set2.getLocalServer(restored).wait(waitScope)));
}
client2 = nullptr;
EXPECT_TRUE(client2Weak->get() == nullptr);
}
} // namespace
......
......@@ -827,7 +827,7 @@ kj::Maybe<NodeTranslator::BrandedDecl> NodeTranslator::BrandedDecl::applyParams(
return nullptr;
} else {
return brand->setParams(kj::mv(params), body.get<Resolver::ResolvedDecl>().kind, subSource)
.map([&](kj::Own<BrandScope>& scope) {
.map([&](kj::Own<BrandScope>&& scope) {
BrandedDecl result = *this;
result.brand = kj::mv(scope);
result.source = subSource;
......
......@@ -1010,7 +1010,7 @@ public:
}
template <typename Func>
auto map(Func&& f) -> Maybe<decltype(f(instance<T&>()))> {
auto map(Func&& f) & -> Maybe<decltype(f(instance<T&>()))> {
if (ptr == nullptr) {
return nullptr;
} else {
......@@ -1019,7 +1019,7 @@ public:
}
template <typename Func>
auto map(Func&& f) const -> Maybe<decltype(f(instance<const T&>()))> {
auto map(Func&& f) const & -> Maybe<decltype(f(instance<const T&>()))> {
if (ptr == nullptr) {
return nullptr;
} else {
......@@ -1027,8 +1027,23 @@ public:
}
}
// TODO(someday): Once it's safe to require GCC 4.8, use ref qualifiers to provide a version of
// map() that uses move semantics if *this is an rvalue.
template <typename Func>
auto map(Func&& f) && -> Maybe<decltype(f(instance<T&&>()))> {
if (ptr == nullptr) {
return nullptr;
} else {
return f(kj::mv(*ptr));
}
}
template <typename Func>
auto map(Func&& f) const && -> Maybe<decltype(f(instance<const T&&>()))> {
if (ptr == nullptr) {
return nullptr;
} else {
return f(kj::mv(*ptr));
}
}
private:
_::NullableValue<T> ptr;
......
......@@ -269,7 +269,7 @@ public:
}
template <typename Func>
auto map(Func&& f) -> Maybe<decltype(f(instance<Own<T>&>()))> {
auto map(Func&& f) & -> Maybe<decltype(f(instance<Own<T>&>()))> {
if (ptr == nullptr) {
return nullptr;
} else {
......@@ -278,7 +278,7 @@ public:
}
template <typename Func>
auto map(Func&& f) const -> Maybe<decltype(f(instance<const Own<T>&>()))> {
auto map(Func&& f) const & -> Maybe<decltype(f(instance<const Own<T>&>()))> {
if (ptr == nullptr) {
return nullptr;
} else {
......@@ -286,8 +286,23 @@ public:
}
}
// TODO(someday): Once it's safe to require GCC 4.8, use ref qualifiers to provide a version of
// map() that uses move semantics if *this is an rvalue.
template <typename Func>
auto map(Func&& f) && -> Maybe<decltype(f(instance<Own<T>&&>()))> {
if (ptr == nullptr) {
return nullptr;
} else {
return f(kj::mv(ptr));
}
}
template <typename Func>
auto map(Func&& f) const && -> Maybe<decltype(f(instance<const Own<T>&&>()))> {
if (ptr == nullptr) {
return nullptr;
} else {
return f(kj::mv(ptr));
}
}
private:
Own<T> ptr;
......
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