31 using Deleter = std::function<void(
void)>;
38 explicit LoanedStateInterface(StateInterface::ConstSharedPtr state_interface, Deleter && deleter)
39 : state_interface_(*state_interface),
40 interface_name_(state_interface->get_name()),
41 deleter_(std::forward<Deleter>(deleter))
51 auto logger = rclcpp::get_logger(interface_name_);
52 RCLCPP_WARN_EXPRESSION(
54 (get_value_statistics_.failed_counter > 0 || get_value_statistics_.timeout_counter > 0),
55 "LoanedStateInterface %s has %u (%.4f %%) timeouts and %u (%.4f %%) missed calls out of %u "
57 interface_name_.c_str(), get_value_statistics_.timeout_counter,
58 (get_value_statistics_.timeout_counter * 100.0) / get_value_statistics_.total_counter,
59 get_value_statistics_.failed_counter,
60 (get_value_statistics_.failed_counter * 100.0) / get_value_statistics_.total_counter,
61 get_value_statistics_.total_counter);
68 const std::string & get_name()
const {
return state_interface_.get_name(); }
70 const std::string & get_interface_name()
const {
return state_interface_.get_interface_name(); }
72 const std::string & get_prefix_name()
const {
return state_interface_.get_prefix_name(); }
75 "Use std::optional<T> get_optional() instead to retrieve the value. This method will be "
76 "removed by the ROS 2 Lyrical Luth release.")]]
77 double get_value()
const
80 if (opt_value.has_value())
82 return opt_value.value();
86 return std::numeric_limits<double>::quiet_NaN();
104 template <
typename T =
double>
105 [[nodiscard]] std::optional<T>
get_optional(
unsigned int max_tries = 10)
const
107 unsigned int nr_tries = 0;
110 ++get_value_statistics_.total_counter;
111 const std::optional<T> data = state_interface_.
get_optional<T>();
112 if (data.has_value())
116 ++get_value_statistics_.failed_counter;
118 std::this_thread::yield();
119 }
while (nr_tries < max_tries);
121 ++get_value_statistics_.timeout_counter;
140 std::string interface_name_;
143 struct HandleRTStatistics
145 unsigned int total_counter = 0;
146 unsigned int failed_counter = 0;
147 unsigned int timeout_counter = 0;
149 mutable HandleRTStatistics get_value_statistics_;