Skip to content

Commit 654cdba

Browse files
Allow editing the values in status_item
* Add new constructor that takes an extra vector of KeyValue * Introduce the addValue() method to StatusItem. * Refactor addValue and hasKey and move them to the cpp Signed-off-by: Mahmoud Almasri <[email protected]>
1 parent 70eaae5 commit 654cdba

File tree

2 files changed

+69
-10
lines changed

2 files changed

+69
-10
lines changed

diagnostic_aggregator/include/diagnostic_aggregator/status_item.hpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ class StatusItem
197197
const std::string item_name, const std::string message = "Missing",
198198
const DiagnosticLevel level = Level_Stale);
199199

200+
/*!
201+
*\brief Constructed from string of item name and vector of key values
202+
*/
203+
DIAGNOSTIC_AGGREGATOR_PUBLIC
204+
StatusItem(
205+
const std::string item_name, const std::vector<diagnostic_msgs::msg::KeyValue> & values,
206+
const std::string message = "Missing", const DiagnosticLevel level = Level_Stale);
207+
200208
DIAGNOSTIC_AGGREGATOR_PUBLIC
201209
~StatusItem();
202210

@@ -253,16 +261,8 @@ class StatusItem
253261
*
254262
*\return True if has key
255263
*/
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-
}
264+
DIAGNOSTIC_AGGREGATOR_PUBLIC
265+
bool hasKey(const std::string & key) const;
266266

267267
/*!
268268
*\brief Returns value for given key, "" if doens't exist
@@ -280,7 +280,26 @@ class StatusItem
280280
return std::string("");
281281
}
282282

283+
/*!
284+
* \brief Adds key value pair to values_ vector
285+
*
286+
* If key already exists, updates value. Otherwise, adds new key value pair.
287+
*
288+
* \param key : Key to add
289+
* \param value : Value to add
290+
*/
291+
DIAGNOSTIC_AGGREGATOR_PUBLIC
292+
void addValue(const std::string & key, const std::string & value);
293+
283294
private:
295+
/*!
296+
* \brief Returns index of key in values_ vector, values_.size() if not found
297+
*
298+
* \param key : Key to search for
299+
* \return Index of key in values_ vector if key present, values_.size() if not
300+
*/
301+
std::size_t findKey(const std::string & key) const;
302+
284303
rclcpp::Time update_time_;
285304
rclcpp::Clock::SharedPtr clock_;
286305

diagnostic_aggregator/src/status_item.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ StatusItem::StatusItem(const string item_name, const string message, const Diagn
7474
RCLCPP_DEBUG(rclcpp::get_logger("StatusItem"), "StatusItem constructor from string");
7575
}
7676

77+
StatusItem::StatusItem(
78+
const std::string item_name, const std::vector<diagnostic_msgs::msg::KeyValue> & values,
79+
const std::string message, const DiagnosticLevel level)
80+
{
81+
StatusItem(item_name, message, level);
82+
values_ = values;
83+
}
84+
7785
StatusItem::~StatusItem() {}
7886

7987
bool StatusItem::update(const diagnostic_msgs::msg::DiagnosticStatus * status)
@@ -126,4 +134,36 @@ std::shared_ptr<diagnostic_msgs::msg::DiagnosticStatus> StatusItem::toStatusMsg(
126134
return status;
127135
}
128136

137+
bool StatusItem::hasKey(const std::string & key) const {return findKey(key) != values_.size();}
138+
139+
void StatusItem::addValue(const std::string & key, const std::string & value)
140+
{
141+
std::size_t index = findKey(key);
142+
if (index != values_.size()) {
143+
// the key already exists, update the value
144+
values_[index].value = value;
145+
return;
146+
}
147+
148+
diagnostic_msgs::msg::KeyValue kv;
149+
kv.key = key;
150+
kv.value = value;
151+
values_.push_back(kv);
152+
}
153+
154+
std::size_t StatusItem::findKey(const std::string & key) const
155+
{
156+
auto it = std::find_if(
157+
values_.begin(), values_.end(),
158+
[&key = std::as_const(key)](const diagnostic_msgs::msg::KeyValue & kv) {
159+
return kv.key == key;
160+
});
161+
162+
if (it != values_.end()) {
163+
return std::distance(values_.begin(), it);
164+
}
165+
166+
return values_.size();
167+
}
168+
129169
} // namespace diagnostic_aggregator

0 commit comments

Comments
 (0)