16#ifndef YASMIN_ROS__ROS_CLIENTS_CACHE_HPP_
17#define YASMIN_ROS__ROS_CLIENTS_CACHE_HPP_
29#include <rclcpp/rclcpp.hpp>
30#include <rclcpp_action/rclcpp_action.hpp>
64 template <
typename ActionT>
65 static typename rclcpp_action::Client<ActionT>::SharedPtr
67 rclcpp::Node::SharedPtr node,
const std::string &action_name,
68 rclcpp::CallbackGroup::SharedPtr callback_group =
nullptr) {
71 std::string node_name = node->get_name();
75 std::make_tuple(node_name, action_type_name, action_name,
76 callback_group_name, std::type_index(
typeid(ActionT)));
78 std::lock_guard<std::recursive_mutex> lock(
get_lock());
82 auto it = action_clients.find(cache_key);
84 if (it != action_clients.end()) {
86 action_name.c_str(), action_type_name.c_str());
87 return std::static_pointer_cast<rclcpp_action::Client<ActionT>>(
93 action_name.c_str(), action_type_name.c_str());
95 auto action_client = rclcpp_action::create_client<ActionT>(
96 node, action_name, callback_group);
99 action_clients[cache_key] = std::static_pointer_cast<void>(action_client);
101 return action_client;
113 template <
typename ServiceT>
114 static typename rclcpp::Client<ServiceT>::SharedPtr
116 rclcpp::Node::SharedPtr node,
const std::string &service_name,
117 rclcpp::CallbackGroup::SharedPtr callback_group =
nullptr) {
120 std::string node_name = node->get_name();
124 std::make_tuple(node_name, service_type_name, service_name,
125 callback_group_name, std::type_index(
typeid(ServiceT)));
127 std::lock_guard<std::recursive_mutex> lock(
get_lock());
131 auto it = service_clients.find(cache_key);
133 if (it != service_clients.end()) {
134 YASMIN_LOG_INFO(
"Reusing existing service client for '%s' of type '%s'",
135 service_name.c_str(), service_type_name.c_str());
136 return std::static_pointer_cast<rclcpp::Client<ServiceT>>(it->second);
141 service_name.c_str(), service_type_name.c_str());
143#if __has_include("rclcpp/version.h")
144#include "rclcpp/version.h"
145#if RCLCPP_VERSION_GTE(28, 1, 1)
146 auto qos = rclcpp::QoS(
147 rclcpp::QoSInitialization::from_rmw(rmw_qos_profile_services_default));
149 auto qos = rmw_qos_profile_services_default;
152 auto qos = rmw_qos_profile_services_default;
155 auto service_client =
156 node->create_client<ServiceT>(service_name, qos, callback_group);
159 service_clients[cache_key] = std::static_pointer_cast<void>(service_client);
161 return service_client;
174 template <
typename MsgT>
176 rclcpp::Node::SharedPtr node,
const std::string &topic_name,
177 const rclcpp::QoS &qos_profile = rclcpp::QoS(10),
178 rclcpp::CallbackGroup::SharedPtr callback_group =
nullptr) {
181 std::string node_name = node->get_name();
184 auto cache_key = std::make_tuple(node_name, msg_type_name, topic_name,
185 qos_hash, std::type_index(
typeid(MsgT)));
187 std::lock_guard<std::recursive_mutex> lock(
get_lock());
191 auto it = publishers.find(cache_key);
193 if (it != publishers.end()) {
194 YASMIN_LOG_INFO(
"Reusing existing publisher for topic '%s' of type '%s'",
195 topic_name.c_str(), msg_type_name.c_str());
196 return std::static_pointer_cast<rclcpp::Publisher<MsgT>>(it->second);
201 topic_name.c_str(), msg_type_name.c_str());
203 rclcpp::PublisherOptions options;
204 options.callback_group = callback_group;
207 node->create_publisher<MsgT>(topic_name, qos_profile, options);
210 publishers[cache_key] = std::static_pointer_cast<void>(publisher);
266 std::string, std::type_index>;
268 std::string, std::type_index>;
270 std::string, std::type_index>;
274 static std::map<ServiceClientKey, std::shared_ptr<void>> &
276 static std::map<PublisherKey, std::shared_ptr<void>> &
get_publishers();
279 static std::recursive_mutex &
get_lock();
288 return typeid(T).name();
307 template <
typename CallbackT>
310 std::ostringstream oss;
311 oss <<
typeid(CallbackT).name() <<
"_"
312 <<
reinterpret_cast<const void *
>(&callback);
Centralized cache for managing ROS 2 client objects.
Definition ros_clients_cache.hpp:53
static size_t get_service_clients_count()
Get the number of cached service clients.
Definition ros_clients_cache.cpp:77
static std::string get_callback_name(const CallbackT &callback)
Get a string representation of a callback function.
Definition ros_clients_cache.hpp:308
static std::map< std::string, size_t > get_cache_stats()
Get statistics about all caches.
Definition ros_clients_cache.cpp:87
static size_t get_publishers_count()
Get the number of cached publishers.
Definition ros_clients_cache.cpp:82
static void clear_all()
Clear all clients caches.
Definition ros_clients_cache.cpp:64
static std::recursive_mutex & get_lock()
Definition ros_clients_cache.cpp:41
std::tuple< std::string, std::string, std::string, std::string, std::type_index > ServiceClientKey
Definition ros_clients_cache.hpp:267
static rclcpp_action::Client< ActionT >::SharedPtr get_or_create_action_client(rclcpp::Node::SharedPtr node, const std::string &action_name, rclcpp::CallbackGroup::SharedPtr callback_group=nullptr)
Get an existing action client from the cache or create a new one.
Definition ros_clients_cache.hpp:66
static size_t get_action_clients_count()
Get the number of cached action clients.
Definition ros_clients_cache.cpp:72
static std::string get_callback_group_name(rclcpp::CallbackGroup::SharedPtr callback_group)
Get a string representation of a callback group.
Definition ros_clients_cache.cpp:100
static std::map< ActionClientKey, std::shared_ptr< void > > & get_action_clients()
Definition ros_clients_cache.cpp:24
static std::string get_type_name()
Get a string representation of a type.
Definition ros_clients_cache.hpp:287
static void clear_action_clients()
Clear the action clients cache.
Definition ros_clients_cache.cpp:46
static rclcpp::Client< ServiceT >::SharedPtr get_or_create_service_client(rclcpp::Node::SharedPtr node, const std::string &service_name, rclcpp::CallbackGroup::SharedPtr callback_group=nullptr)
Get an existing service client from the cache or create a new one.
Definition ros_clients_cache.hpp:115
std::tuple< std::string, std::string, std::string, std::string, std::type_index > PublisherKey
Definition ros_clients_cache.hpp:269
static std::string hash_qos_profile(const rclcpp::QoS &qos_profile)
Create a hash for a QoS profile.
Definition ros_clients_cache.cpp:112
static void clear_publishers()
Clear the publishers cache.
Definition ros_clients_cache.cpp:58
std::tuple< std::string, std::string, std::string, std::string, std::type_index > ActionClientKey
Definition ros_clients_cache.hpp:265
static rclcpp::Publisher< MsgT >::SharedPtr get_or_create_publisher(rclcpp::Node::SharedPtr node, const std::string &topic_name, const rclcpp::QoS &qos_profile=rclcpp::QoS(10), rclcpp::CallbackGroup::SharedPtr callback_group=nullptr)
Get an existing publisher from the cache or create a new one.
Definition ros_clients_cache.hpp:175
static std::map< ServiceClientKey, std::shared_ptr< void > > & get_service_clients()
Definition ros_clients_cache.cpp:30
static std::map< PublisherKey, std::shared_ptr< void > > & get_publishers()
Definition ros_clients_cache.cpp:36
static void clear_service_clients()
Clear the service clients cache.
Definition ros_clients_cache.cpp:52
#define YASMIN_LOG_INFO(text,...)
Definition logs.hpp:169
Definition action_state.hpp:37