32 using Deleter = std::function<void(
void)>;
39 explicit LoanedStateInterface(StateInterface::ConstSharedPtr state_interface, Deleter && deleter)
40 : state_interface_(state_interface),
41 interface_name_(state_interface->get_name()),
42 deleter_(std::forward<Deleter>(deleter))
54 auto logger = rclcpp::get_logger(interface_name_);
55 RCLCPP_WARN_EXPRESSION(
57 (get_value_statistics_.failed_counter > 0 || get_value_statistics_.timeout_counter > 0),
58 "LoanedStateInterface %s has %u (%.4f %%) timeouts and %u (%.4f %%) missed calls out of %u "
60 interface_name_.c_str(), get_value_statistics_.timeout_counter,
61 (get_value_statistics_.timeout_counter * 100.0) / get_value_statistics_.total_counter,
62 get_value_statistics_.failed_counter,
63 (get_value_statistics_.failed_counter * 100.0) / get_value_statistics_.total_counter,
64 get_value_statistics_.total_counter);
72 const std::string & get_name()
const {
return state_interface_->get_name(); }
74 const std::string & get_interface_name()
const {
return state_interface_->get_interface_name(); }
76 const std::string & get_prefix_name()
const {
return state_interface_->get_prefix_name(); }
79 "Use std::optional<T> get_optional() instead to retrieve the value. This method will be "
80 "removed by the ROS 2 Lyrical Luth release.")]]
81 double get_value()
const
84 if (opt_value.has_value())
86 return opt_value.value();
90 return std::numeric_limits<double>::quiet_NaN();
108 template <
typename T =
double>
109 [[nodiscard]] std::optional<T>
get_optional(
unsigned int max_tries = 10)
const
111 unsigned int nr_tries = 0;
114 ++get_value_statistics_.total_counter;
115 const std::optional<T> data = state_interface_->get_optional<T>();
116 if (data.has_value())
120 ++get_value_statistics_.failed_counter;
122 std::this_thread::yield();
123 }
while (nr_tries < max_tries);
125 ++get_value_statistics_.timeout_counter;
142 StateInterface::ConstSharedPtr state_interface_;
143 std::string interface_name_;
147 struct HandleRTStatistics
149 unsigned int total_counter = 0;
150 unsigned int failed_counter = 0;
151 unsigned int timeout_counter = 0;
153 mutable HandleRTStatistics get_value_statistics_;