Repository navigation
Open
Description
rclcpp/rclcpp/include/rclcpp/any_service_callback.hpp
Lines 209 to 238 in 1bf4e6a
| using SharedPtrCallback = std::function< | |
| void ( | |
| std::shared_ptr<typename ServiceT::Request>, | |
| std::shared_ptr<typename ServiceT::Response> | |
| )>; | |
| using SharedPtrWithRequestHeaderCallback = std::function< | |
| void ( | |
| std::shared_ptr<rmw_request_id_t>, | |
| std::shared_ptr<typename ServiceT::Request>, | |
| std::shared_ptr<typename ServiceT::Response> | |
| )>; | |
| using SharedPtrDeferResponseCallback = std::function< | |
| void ( | |
| std::shared_ptr<rmw_request_id_t>, | |
| std::shared_ptr<typename ServiceT::Request> | |
| )>; | |
| using SharedPtrDeferResponseCallbackWithServiceHandle = std::function< | |
| void ( | |
| std::shared_ptr<rclcpp::Service<ServiceT>>, | |
| std::shared_ptr<rmw_request_id_t>, | |
| std::shared_ptr<typename ServiceT::Request> | |
| )>; | |
| std::variant< | |
| std::monostate, | |
| SharedPtrCallback, | |
| SharedPtrWithRequestHeaderCallback, | |
| SharedPtrDeferResponseCallback, | |
| SharedPtrDeferResponseCallbackWithServiceHandle> callback_; | |
| }; |
Would it be a good idea to support a service callback with a const reference and reference for services? At least for the overload without the request id.
Many services are able to return immediately and does not need the request/response object lifetime to exceed the call site. In general, it's also much cheaper to copy a small struct instead of doing atomic increments/decrements of shared pointers.
Metadata
Metadata
Assignees
Labels
No labels
Activity
jmachowinski commentedon Feb 20, 2026
Yes, feel free to open a PR for that ;-)
oysstu commentedon Feb 20, 2026
Looking a bit closer, even the subscription taking a reference is just a dereferenced shared pointer. It's shared pointers all the way down to the rmw layer. I'd be rewriting all of rclcpp to make this happen.
It's possible to do something similar to the subscriptions, just dereferencing the shared pointer, and then user-facing code can start using references even if there's shared pointers under the hood.
jmachowinski commentedon Feb 20, 2026
any_subscription_callback.hpp should contain an example of how the shared ptr is received internally, and then converted to a const ref for the user callback. You would need to adapt the service callback accordingly.
mini-1235 commentedon Oct 5, 2026
@jmachowinski Would you be okay with adding support for callbacks taking
const SharedFuture &inasync_send_requestas well? My clang-tidy'sperformance-unnecessary-value-paramkeeps complaining about the current by-value callback, which gets a bit annoying :(Looking at the implementation here:
rclcpp/rclcpp/include/rclcpp/client.hpp
Lines 592 to 612 in 6c35dd2
I think supporting
const SharedFuture &should be safe.