diff options
| author | James Feist <james.feist@linux.intel.com> | 2019-06-04 14:11:48 -0700 |
|---|---|---|
| committer | James Feist <james.feist@linux.intel.com> | 2019-06-04 15:19:12 -0700 |
| commit | c14699f63e909ed6d8599e65503ee9c1e3a104d6 (patch) | |
| tree | f1c97de847ec34c78f5f0d0ec19d2573752b0b2d | |
| parent | 66ef099b5a77315bb531300dfcc81a53867cd5fa (diff) | |
| download | sdbusplus-c14699f63e909ed6d8599e65503ee9c1e3a104d6.tar.gz sdbusplus-c14699f63e909ed6d8599e65503ee9c1e3a104d6.zip | |
asio: Allow mutable lambdas + small cleanup
There are many cases in which we want to have a mutable
capture so that the lambda takes over the parameter and
we can modify it after the async callback. By making the
top level lambda mutable, the users callback can be mutable.
Also remove a couple references to experimental and a copy
that I saw when I was fixing this issue.
Tested: Added test and made sure that refish which uses
many async_method_calls still worked fine
Change-Id: Ifb1f9d8b9217187799e2defe429e76a937297ca1
Signed-off-by: James Feist <james.feist@linux.intel.com>
| -rw-r--r-- | example/asio-example.cpp | 9 | ||||
| -rw-r--r-- | sdbusplus/asio/connection.hpp | 11 | ||||
| -rw-r--r-- | sdbusplus/asio/object_server.hpp | 8 |
3 files changed, 16 insertions, 12 deletions
diff --git a/example/asio-example.cpp b/example/asio-example.cpp index f04af3f..f7ec6b0 100644 --- a/example/asio-example.cpp +++ b/example/asio-example.cpp @@ -339,10 +339,13 @@ int client() "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/org/openbmc/control", 2, std::vector<std::string>()); + std::string nonConstCapture = "lalalala"; conn->async_method_call( - [](boost::system::error_code ec, - const std::vector<std::string>& things) { + [nonConstCapture = std::move(nonConstCapture)]( + boost::system::error_code ec, + const std::vector<std::string>& things) mutable { std::cout << "async_method_call callback\n"; + nonConstCapture += " stuff"; if (ec) { std::cerr << "async_method_call expected failure: " << ec @@ -350,7 +353,7 @@ int client() } else { - std::cerr << "asyn_method_call should have faild!\n"; + std::cerr << "async_method_call should have failed!\n"; } }, "xyz.openbmc_project.ObjectMapper", diff --git a/sdbusplus/asio/connection.hpp b/sdbusplus/asio/connection.hpp index 083bfdf..4c447f8 100644 --- a/sdbusplus/asio/connection.hpp +++ b/sdbusplus/asio/connection.hpp @@ -25,12 +25,12 @@ #include <boost/asio/spawn.hpp> #include <boost/callable_traits.hpp> #include <chrono> -#include <experimental/tuple> #include <sdbusplus/asio/detail/async_send_handler.hpp> #include <sdbusplus/message.hpp> #include <sdbusplus/utility/read_into_tuple.hpp> #include <sdbusplus/utility/type_traits.hpp> #include <string> +#include <tuple> namespace sdbusplus { @@ -115,7 +115,7 @@ class connection : public sdbusplus::bus::bus * complete. */ template <typename MessageHandler, typename... InputArgs> - void async_method_call(MessageHandler handler, const std::string& service, + void async_method_call(MessageHandler&& handler, const std::string& service, const std::string& objpath, const std::string& interf, const std::string& method, const InputArgs&... a) @@ -123,8 +123,9 @@ class connection : public sdbusplus::bus::bus message::message m = new_method_call(service.c_str(), objpath.c_str(), interf.c_str(), method.c_str()); m.append(a...); - async_send(m, [handler](boost::system::error_code ec, - message::message& r) { + async_send(m, [handler = std::forward<MessageHandler>(handler)]( + boost::system::error_code ec, + message::message& r) mutable { using FunctionTuple = boost::callable_traits::args_t<MessageHandler>; using UnpackType = typename utility::strip_first_arg< @@ -146,7 +147,7 @@ class connection : public sdbusplus::bus::bus // Note. Callback is called whether or not the unpack was // successful to allow the user to implement their own handling auto response = std::tuple_cat(std::make_tuple(ec), responseData); - std::experimental::apply(handler, response); + std::apply(handler, response); }); } diff --git a/sdbusplus/asio/object_server.hpp b/sdbusplus/asio/object_server.hpp index fda791b..5ec0a58 100644 --- a/sdbusplus/asio/object_server.hpp +++ b/sdbusplus/asio/object_server.hpp @@ -128,14 +128,14 @@ class callback_method_instance : public callback std::enable_if_t<!std::is_void<T>::value, void> callFunction(message::message& m, InputTupleType& inputArgs) { - ResultType r = std::experimental::apply(func_, inputArgs); + ResultType r = std::apply(func_, inputArgs); m.append(r); } template <typename T> std::enable_if_t<std::is_void<T>::value, void> callFunction(message::message& m, InputTupleType& inputArgs) { - std::experimental::apply(func_, inputArgs); + std::apply(func_, inputArgs); } #ifdef __cpp_if_constexpr // optional message-first-argument callback @@ -218,14 +218,14 @@ class coroutine_method_instance : public callback std::enable_if_t<!std::is_void<T>::value, void> callFunction(message::message& m, InputTupleType& inputArgs) { - ResultType r = std::experimental::apply(func_, inputArgs); + ResultType r = std::apply(func_, inputArgs); m.append(r); } template <typename T> std::enable_if_t<std::is_void<T>::value, void> callFunction(message::message& m, InputTupleType& inputArgs) { - std::experimental::apply(func_, inputArgs); + std::apply(func_, inputArgs); } // co-routine body for call void expandCall(boost::asio::yield_context yield, message::message& m) |

