File tree Expand file tree Collapse file tree 2 files changed +53
-10
lines changed
include/diagnostic_aggregator Expand file tree Collapse file tree 2 files changed +53
-10
lines changed Original file line number Diff line number Diff line change @@ -253,16 +253,8 @@ class StatusItem
253253 *
254254 *\return True if has key
255255 */
256- bool hasKey (const std::string & key) const
257- {
258- for (unsigned int i = 0 ; i < values_.size (); ++i) {
259- if (values_[i].key == key) {
260- return true ;
261- }
262- }
263-
264- return false ;
265- }
256+ DIAGNOSTIC_AGGREGATOR_PUBLIC
257+ bool hasKey (const std::string & key) const ;
266258
267259 /* !
268260 *\brief Returns value for given key, "" if doens't exist
@@ -280,7 +272,26 @@ class StatusItem
280272 return std::string (" " );
281273 }
282274
275+ /* !
276+ * \brief Adds key value pair to values_ vector
277+ *
278+ * If key already exists, updates value. Otherwise, adds new key value pair.
279+ *
280+ * \param key : Key to add
281+ * \param value : Value to add
282+ */
283+ DIAGNOSTIC_AGGREGATOR_PUBLIC
284+ void addValue (const std::string & key, const std::string & value);
285+
283286private:
287+ /* !
288+ * \brief Returns index of key in values_ vector, values_.size() if not found
289+ *
290+ * \param key : Key to search for
291+ * \return Index of key in values_ vector if key present, values_.size() if not
292+ */
293+ std::size_t findKey (const std::string & key) const ;
294+
284295 rclcpp::Time update_time_;
285296 rclcpp::Clock::SharedPtr clock_;
286297
Original file line number Diff line number Diff line change @@ -126,4 +126,36 @@ std::shared_ptr<diagnostic_msgs::msg::DiagnosticStatus> StatusItem::toStatusMsg(
126126 return status;
127127}
128128
129+ bool StatusItem::hasKey (const std::string & key) const {return findKey (key) != values_.size ();}
130+
131+ void StatusItem::addValue (const std::string & key, const std::string & value)
132+ {
133+ std::size_t index = findKey (key);
134+ if (index != values_.size ()) {
135+ // the key already exists, update the value
136+ values_[index].value = value;
137+ return ;
138+ }
139+
140+ diagnostic_msgs::msg::KeyValue kv;
141+ kv.key = key;
142+ kv.value = value;
143+ values_.push_back (kv);
144+ }
145+
146+ std::size_t StatusItem::findKey (const std::string & key) const
147+ {
148+ auto it = std::find_if (
149+ values_.begin (), values_.end (),
150+ [&key = std::as_const (key)](const diagnostic_msgs::msg::KeyValue & kv) {
151+ return kv.key == key;
152+ });
153+
154+ if (it != values_.end ()) {
155+ return std::distance (values_.begin (), it);
156+ }
157+
158+ return values_.size ();
159+ }
160+
129161} // namespace diagnostic_aggregator
You can’t perform that action at this time.
0 commit comments