31 using Deleter = std::function<void(
void)>;
41 : state_interface_(state_interface),
42 deleter_(std::forward<Deleter>(deleter)),
43 interface_name_(state_interface.get_name())
52 explicit LoanedStateInterface(StateInterface::ConstSharedPtr state_interface, Deleter && deleter)
53 : state_interface_(*state_interface),
54 interface_name_(state_interface->get_name()),
55 deleter_(std::forward<Deleter>(deleter))
65 auto logger = rclcpp::get_logger(interface_name_);
66 RCLCPP_WARN_EXPRESSION(
68 (get_value_statistics_.failed_counter > 0 || get_value_statistics_.timeout_counter > 0),
69 "LoanedStateInterface %s has %u (%.4f %%) timeouts and %u (%.4f %%) missed calls out of %u "
71 interface_name_.c_str(), get_value_statistics_.timeout_counter,
72 (get_value_statistics_.timeout_counter * 100.0) / get_value_statistics_.total_counter,
73 get_value_statistics_.failed_counter,
74 (get_value_statistics_.failed_counter * 100.0) / get_value_statistics_.total_counter,
75 get_value_statistics_.total_counter);
82 const std::string & get_name()
const {
return state_interface_.get_name(); }
84 const std::string & get_interface_name()
const {
return state_interface_.get_interface_name(); }
87 "Replaced by get_name method, which is semantically more correct")]]
const std::string
90 return state_interface_.get_name();
93 const std::string & get_prefix_name()
const {
return state_interface_.get_prefix_name(); }
96 "Use std::optional<T> get_optional() instead to retrieve the value. This method will be "
97 "removed by the ROS 2 Kilted Kaiju release.")]]
98 double get_value()
const
101 if (opt_value.has_value())
103 return opt_value.value();
107 return std::numeric_limits<double>::quiet_NaN();
125 template <
typename T =
double>
126 [[nodiscard]] std::optional<T>
get_optional(
unsigned int max_tries = 10)
const
128 unsigned int nr_tries = 0;
131 ++get_value_statistics_.total_counter;
132 const std::optional<T> data = state_interface_.
get_optional<T>();
133 if (data.has_value())
137 ++get_value_statistics_.failed_counter;
139 std::this_thread::yield();
140 }
while (nr_tries < max_tries);
142 ++get_value_statistics_.timeout_counter;
161 template <
typename T>
163 "Use std::optional<T> get_optional() instead to retrieve the value. This method will be "
164 "removed by the ROS 2 Kilted Kaiju release.")]] [[nodiscard]]
bool
167 const auto opt_value = get_optional<T>(max_tries);
168 if (opt_value.has_value())
170 value = opt_value.value();
185 std::string interface_name_;
188 struct HandleRTStatistics
190 unsigned int total_counter = 0;
191 unsigned int failed_counter = 0;
192 unsigned int timeout_counter = 0;
194 mutable HandleRTStatistics get_value_statistics_;