32 using Deleter = std::function<void(
void)>;
42 : state_interface_(state_interface),
43 deleter_(std::forward<Deleter>(deleter)),
44 interface_name_(state_interface.get_name())
53 explicit LoanedStateInterface(StateInterface::ConstSharedPtr state_interface, Deleter && deleter)
54 : state_interface_(*state_interface),
55 interface_name_(state_interface->get_name()),
56 deleter_(std::forward<Deleter>(deleter))
66 auto logger = rclcpp::get_logger(interface_name_);
67 RCLCPP_WARN_EXPRESSION(
69 (get_value_statistics_.failed_counter > 0 || get_value_statistics_.timeout_counter > 0),
70 "LoanedStateInterface %s has %u (%.4f %%) timeouts and %u (%.4f %%) missed calls out of %u "
72 interface_name_.c_str(), get_value_statistics_.timeout_counter,
73 (get_value_statistics_.timeout_counter * 100.0) / get_value_statistics_.total_counter,
74 get_value_statistics_.failed_counter,
75 (get_value_statistics_.failed_counter * 100.0) / get_value_statistics_.total_counter,
76 get_value_statistics_.total_counter);
83 const std::string & get_name()
const {
return state_interface_.get_name(); }
85 const std::string & get_interface_name()
const {
return state_interface_.get_interface_name(); }
88 "Replaced by get_name method, which is semantically more correct")]]
const std::string
91 return state_interface_.get_name();
94 const std::string & get_prefix_name()
const {
return state_interface_.get_prefix_name(); }
97 "Use std::optional<T> get_optional() instead to retrieve the value. This method will be "
98 "removed by the ROS 2 Kilted Kaiju release.")]]
99 double get_value()
const
102 if (opt_value.has_value())
104 return opt_value.value();
108 return std::numeric_limits<double>::quiet_NaN();
126 template <
typename T =
double>
127 [[nodiscard]] std::optional<T>
get_optional(
unsigned int max_tries = 10)
const
129 unsigned int nr_tries = 0;
132 ++get_value_statistics_.total_counter;
133 const std::optional<T> data = state_interface_.
get_optional<T>();
134 if (data.has_value())
138 ++get_value_statistics_.failed_counter;
140 std::this_thread::yield();
141 }
while (nr_tries < max_tries);
143 ++get_value_statistics_.timeout_counter;
162 template <
typename T>
164 "Use std::optional<T> get_optional() instead to retrieve the value. This method will be "
165 "removed by the ROS 2 Kilted Kaiju release.")]] [[nodiscard]]
bool
168 const auto opt_value = get_optional<T>(max_tries);
169 if (opt_value.has_value())
171 value = opt_value.value();
186 std::string interface_name_;
189 struct HandleRTStatistics
191 unsigned int total_counter = 0;
192 unsigned int failed_counter = 0;
193 unsigned int timeout_counter = 0;
195 mutable HandleRTStatistics get_value_statistics_;