Commit 2d5efc24 authored by Kenton Varda's avatar Kenton Varda

Add asGeneric() methods to generic readers, builders, and interfaces which casts…

Add asGeneric() methods to generic readers, builders, and interfaces which casts them to the same type but with different generic parameters.
parent f0970f96
......@@ -787,9 +787,16 @@ TEST(Capability, Generics) {
List<uint32_t>::Reader qux = response.getQux();
qux.size();
checkTestMessage(response.getGen().getFoo());
}, [](kj::Exception&& e) {});
}, [](kj::Exception&& e) {
// Ignore exception (which we'll always get because we're calling a null capability).
});
promise.wait(waitScope);
// Check that asGeneric<>() compiles.
test::TestGenerics<TestAllTypes>::Interface<>::Client castClient = client.asGeneric<>();
test::TestGenerics<TestAllTypes>::Interface<TestAllTypes>::Client castClient2 =
client.asGeneric<TestAllTypes>();
}
TEST(Capability, Generics2) {
......
......@@ -576,7 +576,7 @@ private:
return node.getIsGeneric();
}
kj::StringTree decl(bool withDefaults) const {
kj::StringTree decl(bool withDefaults, kj::StringPtr suffix = nullptr) const {
// "template <typename T, typename U>" for this type. Includes default assignments
// ("= ::capnp::AnyPointer") if `withDefaults` is true. Returns empty string if this type
// is not parameterized.
......@@ -588,7 +588,7 @@ private:
} else {
return kj::strTree(
"template <", kj::StringTree(KJ_MAP(p, params) {
return kj::strTree("typename ", p.getName(),
return kj::strTree("typename ", p.getName(), suffix,
withDefaults ? " = ::capnp::AnyPointer" : "");
}, ", "), ">\n");
}
......@@ -608,7 +608,7 @@ private:
}
}
kj::StringTree args() const {
kj::StringTree args(kj::StringPtr suffix = nullptr) const {
// "<T, U>" for this type.
auto params = node.getParameters();
......@@ -617,7 +617,7 @@ private:
} else {
return kj::strTree(
"<", kj::StringTree(KJ_MAP(p, params) {
return kj::strTree(p.getName());
return kj::strTree(p.getName(), suffix);
}, ", "), ">");
}
}
......@@ -1741,6 +1741,13 @@ private:
" }\n"
"#endif // !CAPNP_LITE\n"
"\n",
templateContext.isGeneric() ? kj::strTree(
" ", templateContext.decl(true, "2"),
" typename ", unqualifiedParentType, templateContext.args("2"), "::Reader asGeneric() {\n"
" return typename ", unqualifiedParentType, templateContext.args("2"),
"::Reader(_reader);\n"
" }\n"
"\n") : kj::strTree(),
isUnion ? kj::strTree(" inline Which which() const;\n") : kj::strTree(),
kj::mv(methodDecls),
"private:\n"
......@@ -1778,6 +1785,13 @@ private:
" inline ::kj::StringTree toString() const { return asReader().toString(); }\n"
"#endif // !CAPNP_LITE\n"
"\n",
templateContext.isGeneric() ? kj::strTree(
" ", templateContext.decl(true, "2"),
" typename ", unqualifiedParentType, templateContext.args("2"), "::Builder asGeneric() {\n"
" return typename ", unqualifiedParentType, templateContext.args("2"),
"::Builder(_builder);\n"
" }\n"
"\n") : kj::strTree(),
isUnion ? kj::strTree(" inline Which which();\n") : kj::strTree(),
kj::mv(methodDecls),
"private:\n"
......@@ -2213,6 +2227,12 @@ private:
" Client& operator=(Client& other);\n"
" Client& operator=(Client&& other);\n"
"\n",
templateContext.isGeneric() ? kj::strTree(
" ", templateContext.decl(true, "2"),
" typename ", name, templateContext.args("2"), "::Client asGeneric() {\n"
" return castAs<", name, templateContext.args("2"), ">();\n"
" }\n"
"\n") : kj::strTree(),
KJ_MAP(m, methods) { return kj::mv(m.clientDecls); },
"\n"
"protected:\n"
......
......@@ -1687,6 +1687,22 @@ TEST(Encoding, Generics) {
initTestMessage(root.initBasic().initFoo());
checkTestMessage(reader.getBasic().getFoo());
{
auto typed = root.getBasic();
test::TestGenerics<>::Reader generic = typed.asGeneric<>();
checkTestMessage(generic.getFoo().getAs<TestAllTypes>());
test::TestGenerics<TestAllTypes>::Reader halfGeneric = typed.asGeneric<TestAllTypes>();
checkTestMessage(halfGeneric.getFoo());
}
{
auto typed = root.getBasic().asReader();
test::TestGenerics<>::Reader generic = typed.asGeneric<>();
checkTestMessage(generic.getFoo().getAs<TestAllTypes>());
test::TestGenerics<TestAllTypes>::Reader halfGeneric = typed.asGeneric<TestAllTypes>();
checkTestMessage(halfGeneric.getFoo());
}
initTestMessage(root.initInner().initFoo());
checkTestMessage(reader.getInner().getFoo());
......
......@@ -178,6 +178,11 @@ public:
Client& operator=(Client& other);
Client& operator=(Client&& other);
template <typename SturdyRef2 = ::capnp::AnyPointer, typename Owner2 = ::capnp::AnyPointer>
typename Persistent<SturdyRef2, Owner2>::Client asGeneric() {
return castAs<Persistent<SturdyRef2, Owner2>>();
}
::capnp::Request<typename ::capnp::Persistent<SturdyRef, Owner>::SaveParams, typename ::capnp::Persistent<SturdyRef, Owner>::SaveResults> saveRequest(
::kj::Maybe< ::capnp::MessageSize> sizeHint = nullptr);
......@@ -222,6 +227,10 @@ public:
}
#endif // !CAPNP_LITE
typename SaveParams::Reader asGeneric() {
return typename SaveParams::Reader(_reader);
}
inline bool hasSealFor() const;
inline ::capnp::ReaderFor<Owner> getSealFor() const;
......@@ -254,6 +263,10 @@ public:
inline ::kj::StringTree toString() const { return asReader().toString(); }
#endif // !CAPNP_LITE
typename SaveParams::Builder asGeneric() {
return typename SaveParams::Builder(_builder);
}
inline bool hasSealFor();
inline ::capnp::BuilderFor<Owner> getSealFor();
inline void setSealFor( ::capnp::ReaderFor<Owner> value);
......@@ -308,6 +321,10 @@ public:
}
#endif // !CAPNP_LITE
typename SaveResults::Reader asGeneric() {
return typename SaveResults::Reader(_reader);
}
inline bool hasSturdyRef() const;
inline ::capnp::ReaderFor<SturdyRef> getSturdyRef() const;
......@@ -340,6 +357,10 @@ public:
inline ::kj::StringTree toString() const { return asReader().toString(); }
#endif // !CAPNP_LITE
typename SaveResults::Builder asGeneric() {
return typename SaveResults::Builder(_builder);
}
inline bool hasSturdyRef();
inline ::capnp::BuilderFor<SturdyRef> getSturdyRef();
inline void setSturdyRef( ::capnp::ReaderFor<SturdyRef> value);
......@@ -396,6 +417,11 @@ public:
Client& operator=(Client& other);
Client& operator=(Client&& other);
template <typename InternalRef2 = ::capnp::AnyPointer, typename ExternalRef2 = ::capnp::AnyPointer, typename InternalOwner2 = ::capnp::AnyPointer, typename ExternalOwner2 = ::capnp::AnyPointer>
typename RealmGateway<InternalRef2, ExternalRef2, InternalOwner2, ExternalOwner2>::Client asGeneric() {
return castAs<RealmGateway<InternalRef2, ExternalRef2, InternalOwner2, ExternalOwner2>>();
}
::capnp::Request<typename ::capnp::RealmGateway<InternalRef, ExternalRef, InternalOwner, ExternalOwner>::ImportParams, typename ::capnp::Persistent<InternalRef, InternalOwner>::SaveResults> importRequest(
::kj::Maybe< ::capnp::MessageSize> sizeHint = nullptr);
::capnp::Request<typename ::capnp::RealmGateway<InternalRef, ExternalRef, InternalOwner, ExternalOwner>::ExportParams, typename ::capnp::Persistent<ExternalRef, ExternalOwner>::SaveResults> exportRequest(
......@@ -446,6 +472,10 @@ public:
}
#endif // !CAPNP_LITE
typename ImportParams::Reader asGeneric() {
return typename ImportParams::Reader(_reader);
}
inline bool hasCap() const;
#if !CAPNP_LITE
inline typename ::capnp::Persistent<ExternalRef, ExternalOwner>::Client getCap() const;
......@@ -483,6 +513,10 @@ public:
inline ::kj::StringTree toString() const { return asReader().toString(); }
#endif // !CAPNP_LITE
typename ImportParams::Builder asGeneric() {
return typename ImportParams::Builder(_builder);
}
inline bool hasCap();
#if !CAPNP_LITE
inline typename ::capnp::Persistent<ExternalRef, ExternalOwner>::Client getCap();
......@@ -546,6 +580,10 @@ public:
}
#endif // !CAPNP_LITE
typename ExportParams::Reader asGeneric() {
return typename ExportParams::Reader(_reader);
}
inline bool hasCap() const;
#if !CAPNP_LITE
inline typename ::capnp::Persistent<InternalRef, InternalOwner>::Client getCap() const;
......@@ -583,6 +621,10 @@ public:
inline ::kj::StringTree toString() const { return asReader().toString(); }
#endif // !CAPNP_LITE
typename ExportParams::Builder asGeneric() {
return typename ExportParams::Builder(_builder);
}
inline bool hasCap();
#if !CAPNP_LITE
inline typename ::capnp::Persistent<InternalRef, InternalOwner>::Client getCap();
......
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