diff --git a/doc/design.md b/doc/design.md index 319ee3de..38cee6ab 100644 --- a/doc/design.md +++ b/doc/design.md @@ -108,7 +108,7 @@ Each `ServerField` invokes `PassField`, which: `ServerRet` invokes the next layer (typically `ServerCall`), stores the result, and calls `BuildField` to serialize it into the `Results::Builder`. -`ServerCall` uses the generated `ProxyMethod::impl` pointer-to-member to invoke the actual C++ method on the wrapped implementation object. +`ServerCall` uses the generated `ProxyMethod::impl` pointer-to-member to invoke the actual C++ method on the wrapped implementation object. ```mermaid sequenceDiagram diff --git a/include/mp/proxy-types.h b/include/mp/proxy-types.h index 9822c121..b8c4a2a1 100644 --- a/include/mp/proxy-types.h +++ b/include/mp/proxy-types.h @@ -557,6 +557,7 @@ struct ServerCall return TryFinally( [&]() -> decltype(auto) { return ProxyServerMethodTraits< + typename std::remove_reference_t::Interface, typename decltype(server_context.call_context.getParams())::Reads >::invoke(server_context, std::forward(args)...); }, @@ -750,7 +751,7 @@ void clientInvoke(ProxyClient& proxy_client, const GetRequest& get_request, Fiel auto request = (proxy_client.m_client.*get_request)(nullptr); using Request = CapRequestTraits; - using FieldList = typename ProxyClientMethodTraits::Fields; + using FieldList = typename ProxyClientMethodTraits::Fields; invoke_context.emplace(*proxy_client.m_context.connection, thread_context); IterateFields().handleChain(*invoke_context, request, FieldList(), typename FieldObjs::BuildParams{&fields}...); MP_LOGPLAIN(*proxy_client.m_context.loop, Log::Debug) @@ -836,7 +837,7 @@ kj::Promise serverInvoke(Server& server, CallContext& call_context, Fn fn) try { using ServerContext = ServerInvokeContext; - using ArgList = typename ProxyClientMethodTraits::Params; + using ArgList = typename ProxyClientMethodTraits::Params; ServerContext server_context{server, call_context, req}; // ReplaceVoid is used to support fn.invoke implementations that // execute asynchronously and return promises, as well as diff --git a/include/mp/proxy.h b/include/mp/proxy.h index 2144b571..14ed9ae7 100644 --- a/include/mp/proxy.h +++ b/include/mp/proxy.h @@ -26,8 +26,11 @@ template struct ProxyClient; // IWYU pragma: export //! Mapping from capnp interface type to proxy server implementation (specializations are generated by //! proxy-codegen.cpp). template struct ProxyServer; // IWYU pragma: export -//! Mapping from capnp method params type to method traits (specializations are generated by proxy-codegen.cpp). -template struct ProxyMethod; // IWYU pragma: export +//! Mapping from capnp interface and method params types to method traits +//! (specializations are generated by proxy-codegen.cpp). The interface type is +//! the wrapped interface handling the call, which may differ from the +//! interface declaring the method if the method is inherited. +template struct ProxyMethod; // IWYU pragma: export //! Mapping from capnp struct type to struct traits (specializations are generated by proxy-codegen.cpp). template struct ProxyStruct; // IWYU pragma: export //! Mapping from local c++ type to capnp type and traits (specializations are generated by proxy-codegen.cpp). @@ -247,8 +250,10 @@ struct FunctionTraits<_Result (_Class::*const)(_Params...)> //! an additional invoke() method that calls the C++ method which is being //! proxied, forwarding any arguments. //! -//! The template argument should be the InterfaceName::MethodNameParams class -//! (generated by Cap'n Proto) associated with the method. +//! The template arguments should be the wrapped InterfaceName class handling the +//! call and the MethodNameParams class (generated by Cap'n Proto) associated +//! with the method. If the method is inherited, the params class is declared by +//! the base interface, e.g. BaseInterfaceName::MethodNameParams. //! //! Note: The class definition here is just the fallback definition used when //! the other specialization below doesn't match. The fallback is only used for @@ -256,7 +261,7 @@ struct FunctionTraits<_Result (_Class::*const)(_Params...)> //! is just the two special construct() and destroy() methods described in \ref //! ProxyClientBase. These methods don't have any C++ parameters or return //! types, so the trait information below reflects that. -template +template struct ProxyMethodTraits { using Params = TypeList<>; @@ -270,39 +275,39 @@ struct ProxyMethodTraits }; //! Specialization of above for proxy methods that have a -//! ProxyMethod::impl pointer-to-method -//! constant defined by generated code. This includes all functions defined in -//! the capnp interface except any construct() or destroy() methods, that are +//! ProxyMethod::impl pointer-to-method constant +//! defined by generated code. This includes all functions defined or inherited +//! by the capnp interface except any construct() or destroy() methods, that are //! assumed not to correspond to real member functions in the C++ class, and //! will use the fallback traits definition above. The generated code this //! specialization relies on looks like: //! -//! struct ProxyMethod +//! struct ProxyMethod //! { //! static constexpr auto impl = &ClassName::methodName; //! }; -template -struct ProxyMethodTraits::impl)>> - : public FunctionTraits::impl)> +template +struct ProxyMethodTraits::impl)>> + : public FunctionTraits::impl)> { template static decltype(auto) invoke(ServerContext& server_context, Args&&... args) { - return (server_context.proxy_server.m_impl.get()->*ProxyMethod::impl)(std::forward(args)...); + return (server_context.proxy_server.m_impl.get()->*ProxyMethod::impl)(std::forward(args)...); } }; //! Customizable (through template specialization) traits class used in generated ProxyClient implementations from //! proxy-codegen.cpp. -template -struct ProxyClientMethodTraits : public ProxyMethodTraits +template +struct ProxyClientMethodTraits : public ProxyMethodTraits { }; //! Customizable (through template specialization) traits class used in generated ProxyServer implementations from //! proxy-codegen.cpp. -template -struct ProxyServerMethodTraits : public ProxyMethodTraits +template +struct ProxyServerMethodTraits : public ProxyMethodTraits { }; diff --git a/src/mp/gen.cpp b/src/mp/gen.cpp index 8d733a91..e27b1d2c 100644 --- a/src/mp/gen.cpp +++ b/src/mp/gen.cpp @@ -542,9 +542,14 @@ static void Generate(kj::StringPtr src_prefix, } fields.mergeFields(); - if (!is_construct && !is_destroy && (&method_interface == &interface)) { + // Generate a ProxyMethod specialization for every method, + // including inherited methods, so each wrapped interface maps + // the methods it handles to its own C++ class. The interface + // declaring an inherited method may be wrapped by a different + // C++ class, or not be wrapped at all. + if (!is_construct && !is_destroy) { methods << "template<>\n"; - methods << "struct ProxyMethod<" << method_prefix << "Params>\n"; + methods << "struct ProxyMethod<" << message_namespace << "::" << node_name << ", " << method_prefix << "Params>\n"; methods << "{\n"; methods << " static constexpr auto impl = &" << proxied_class_type << "::" << proxied_method_name << ";\n"; @@ -611,8 +616,8 @@ static void Generate(kj::StringPtr src_prefix, const std::string super_str{is_construct || is_destroy ? "Super& super" : ""}; const std::string self_str{is_construct || is_destroy ? "super" : "*this"}; - client << " using M" << method_ordinal << " = ProxyClientMethodTraits<" << method_prefix - << "Params>;\n"; + client << " using M" << method_ordinal << " = ProxyClientMethodTraits<" << message_namespace + << "::" << node_name << ", " << method_prefix << "Params>;\n"; client << " " << static_str << "typename M" << method_ordinal << "::Result " << method_name << "(" << super_str << client_args.str() << ")"; client << ";\n"; diff --git a/test/mp/test/foo-types.h b/test/mp/test/foo-types.h index 85c36349..9ebadc90 100644 --- a/test/mp/test/foo-types.h +++ b/test/mp/test/foo-types.h @@ -37,6 +37,7 @@ namespace mp { namespace test { namespace messages { struct ExtendedCallback; // IWYU pragma: export +struct FlatCallback; // IWYU pragma: export struct FooCallback; // IWYU pragma: export struct FooFn; // IWYU pragma: export struct FooInterface; // IWYU pragma: export diff --git a/test/mp/test/foo.capnp b/test/mp/test/foo.capnp index 392719ec..5e7a7813 100644 --- a/test/mp/test/foo.capnp +++ b/test/mp/test/foo.capnp @@ -39,6 +39,7 @@ interface FooInterface $Proxy.wrap("mp::test::FooImplementation") { passDataPointers @22 (arg :List(Data)) -> (result :List(Data)); listBars @25 (context :Proxy.Context, n :Int32) -> (result :List(BarInterface)); callMessageAsync @26 (context :Proxy.Context) -> (result :FooMessage); + callbackFlat @27 (context :Proxy.Context, callback :FlatCallback, arg: Int32) -> (result :Int32); } interface FooInit $Proxy.wrap("mp::test::FooInit") { @@ -54,6 +55,17 @@ interface ExtendedCallback extends(FooCallback) $Proxy.wrap("mp::test::ExtendedC callExtended @0 (context :Proxy.Context, arg :Int32) -> (result :Int32); } +# Interface without a $Proxy.wrap annotation, so no C++ class corresponds to +# it. Its methods are handled by C++ classes wrapping derived interfaces. +interface CallbackBase { + destroy @0 (context :Proxy.Context) -> (); + call @1 (context :Proxy.Context, arg :Int32) -> (result :Int32); +} + +interface FlatCallback extends(CallbackBase) $Proxy.wrap("mp::test::FlatCallback") { + callExtended @0 (context :Proxy.Context, arg :Int32) -> (result :Int32); +} + interface BarInterface $Proxy.wrap("mp::test::Bar") { destroy @0 (context :Proxy.Context) -> (); value @1 (context :Proxy.Context) -> (result :Int32); diff --git a/test/mp/test/foo.h b/test/mp/test/foo.h index e8782708..639cbb98 100644 --- a/test/mp/test/foo.h +++ b/test/mp/test/foo.h @@ -67,6 +67,17 @@ class ExtendedCallback : public FooCallback virtual int callExtended(int arg) = 0; }; +//! C++ class wrapped by the FlatCallback capnp interface, which inherits its +//! call method from the CallbackBase capnp interface. Unlike ExtendedCallback, +//! there is no C++ base class corresponding to the capnp base interface. +class FlatCallback +{ +public: + virtual ~FlatCallback() = default; + virtual int call(int arg) = 0; + virtual int callExtended(int arg) = 0; +}; + class FooInit { }; @@ -104,6 +115,7 @@ class FooImplementation void saveCallback(std::shared_ptr callback) { m_callback = std::move(callback); } int callbackSaved(int arg) { return m_callback->call(arg); } int callbackExtended(ExtendedCallback& callback, int arg) { return callback.callExtended(arg); } + int callbackFlat(FlatCallback& callback, int arg) { return callback.call(arg) + callback.callExtended(arg); } FooCustom passCustom(FooCustom foo) { return foo; } FooEmpty passEmpty(FooEmpty foo) { return foo; } FooData passData(FooData foo) { return foo; } diff --git a/test/mp/test/test.cpp b/test/mp/test/test.cpp index 0492c9ee..ba4ebf20 100644 --- a/test/mp/test/test.cpp +++ b/test/mp/test/test.cpp @@ -240,6 +240,15 @@ KJ_TEST("Call FooInterface methods") KJ_EXPECT(saved.use_count() == 1); KJ_EXPECT(foo->callbackExtended(callback, 11) == 12); + class FlatCallbackImpl : public FlatCallback + { + public: + int call(int arg) override { return arg + 1; } + int callExtended(int arg) override { return arg * 10; } + }; + FlatCallbackImpl flat_callback; + KJ_EXPECT(foo->callbackFlat(flat_callback, 2) == 23); + FooCustom custom_in; custom_in.v1 = "v1"; custom_in.v2 = 5;