sdbus++: async: client: move context to constructor
The async::context is needed for every client invocation, since that
is how the async task is generated. Rather than pass it in as a
parameter on each function invocation, add it as a constructor to the
client-proxy object.
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Ic1c8ec2260b7d4774649de9559df466058a50e14
diff --git a/example/calculator-client.cpp b/example/calculator-client.cpp
index 78c8880..56e9fb4 100644
--- a/example/calculator-client.cpp
+++ b/example/calculator-client.cpp
@@ -8,35 +8,35 @@
constexpr auto service = "net.poettering.Calculator";
constexpr auto path = "/net/poettering/calculator";
- auto c =
- sdbusplus::client::net::poettering::Calculator().service(service).path(
- path);
+ auto c = sdbusplus::client::net::poettering::Calculator(ctx)
+ .service(service)
+ .path(path);
// Alternatively, sdbusplus::async::client_t<Calculator, ...>() could have
// been used to combine multiple interfaces into a single client-proxy.
{
- auto _ = co_await c.multiply(ctx, 7, 6);
+ auto _ = co_await c.multiply(7, 6);
std::cout << "Should be 42: " << _ << std::endl;
}
{
- auto _ = co_await c.get_property<int64_t>(ctx, "LastResult");
+ auto _ = co_await c.get_property<int64_t>("LastResult");
std::cout << "Should be 42: " << _ << std::endl;
}
{
- co_await c.clear(ctx);
+ co_await c.clear();
}
{
- auto _ = co_await c.get_property<int64_t>(ctx, "LastResult");
+ auto _ = co_await c.get_property<int64_t>("LastResult");
std::cout << "Should be 0: " << _ << std::endl;
}
{
- co_await c.set_property<int64_t>(ctx, "LastResult", 1234);
- auto _ = co_await c.get_property<int64_t>(ctx, "LastResult");
+ co_await c.set_property<int64_t>("LastResult", 1234);
+ auto _ = co_await c.get_property<int64_t>("LastResult");
std::cout << "Should be 1234: " << _ << std::endl;
}
diff --git a/include/sdbusplus/async/client.hpp b/include/sdbusplus/async/client.hpp
index ae78b80..5290894 100644
--- a/include/sdbusplus/async/client.hpp
+++ b/include/sdbusplus/async/client.hpp
@@ -21,38 +21,40 @@
public Types<sdbusplus::async::proxy_ns::proxy<S, P, false, Preserved>>...
{
private:
+ sdbusplus::async::context& ctx{};
sdbusplus::async::proxy_ns::proxy<S, P, false, Preserved> proxy{};
public:
+ constexpr client() = delete;
/* Delete default constructor if Service or Path have been provided. */
- constexpr client()
+ explicit client(sdbusplus::async::context& ctx)
requires(S || P)
= delete;
/* Default (empty) constructor only when Service and Path are missing. */
- constexpr client()
+ explicit client(sdbusplus::async::context& ctx)
requires(!S && !P)
- : Types<decltype(proxy)>(proxy)...
+ : Types<decltype(proxy)>(ctx, proxy)..., ctx(ctx)
{}
/* Conversion constructor for a non-empty (Service and/or Path) proxy. */
- constexpr explicit client(
- sdbusplus::async::proxy_ns::proxy<S, P, false, Preserved> p)
+ explicit client(sdbusplus::async::context& ctx,
+ sdbusplus::async::proxy_ns::proxy<S, P, false, Preserved> p)
requires(S || P)
- : Types<decltype(proxy)>(p)..., proxy(p)
+ : Types<decltype(proxy)>(ctx, p)..., ctx(ctx), proxy(p)
{}
/* Convert a non-Service instance to a Service instance. */
- constexpr auto service(auto& s) const noexcept
+ auto service(auto& s) const noexcept
requires(!S)
{
- return client<true, P, Preserved, Types...>(proxy.service(s));
+ return client<true, P, Preserved, Types...>(ctx, proxy.service(s));
}
/* Convert a non-Path instance to a Path instance. */
- constexpr auto path(auto& p) const noexcept
+ auto path(auto& p) const noexcept
requires(!P)
{
- return client<S, true, Preserved, Types...>(proxy.path(p));
+ return client<S, true, Preserved, Types...>(ctx, proxy.path(p));
}
};
diff --git a/tools/sdbusplus/templates/interface.client.hpp.mako b/tools/sdbusplus/templates/interface.client.hpp.mako
index 879e415..bbb8538 100644
--- a/tools/sdbusplus/templates/interface.client.hpp.mako
+++ b/tools/sdbusplus/templates/interface.client.hpp.mako
@@ -41,15 +41,14 @@
// To be replaced by generators...
template <typename T>
- auto get_property(sdbusplus::async::context& ctx, auto& property) const
+ auto get_property(auto& property) const
{
return proxy.template get_property<T>(ctx, property);
}
// To be replaced by generators...
template <typename T>
- auto set_property(sdbusplus::async::context& ctx, auto& property,
- T&& value) const
+ auto set_property(auto& property, T&& value) const
{
return proxy.template set_property<T>(ctx, property,
std::forward<T>(value));
@@ -57,9 +56,10 @@
private:
// Conversion constructor from proxy used by client_t.
- constexpr explicit ${interface.classname}(Proxy p) :
- proxy(p.interface(interface)) {}
+ constexpr ${interface.classname}(sdbusplus::async::context& ctx, Proxy p) :
+ ctx(ctx), proxy(p.interface(interface)) {}
+ sdbusplus::async::context& ctx{};
decltype(std::declval<Proxy>().interface(interface)) proxy = {};
};
diff --git a/tools/sdbusplus/templates/method.client.hpp.mako b/tools/sdbusplus/templates/method.client.hpp.mako
index ca1c5a2..6d035ca 100644
--- a/tools/sdbusplus/templates/method.client.hpp.mako
+++ b/tools/sdbusplus/templates/method.client.hpp.mako
@@ -14,21 +14,20 @@
% endfor
% endif
*/
- auto ${method.camelCase}(
- sdbusplus::async::context& ctx\
+ auto ${method.camelCase}(\
% if len(method.parameters) != 0:
-,
- % else:
+ ${method.get_parameters_str(interface)}\
+ % else:
% endif
- ${method.get_parameters_str(interface)})
+)
{
return proxy.template call<${method.returns_as_list(interface)}>(
ctx, "${method.name}"\
% if len(method.parameters) != 0:
,
+ ${method.parameters_as_list()}\
% else:
-
% endif
- ${method.parameters_as_list()});
+);
}