regulators: Enhance IDMap to detect duplicate IDs
The IDMap class provides a mapping from unique string IDs to the
corresponding Device, Rail, and Rule objects.
Enhance IDMap to detect duplicate IDs. Throw an exception if the caller
tries to add a Device, Rail, or Rule whose ID already exists in the map.
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I81176fae1415cd5a89dc4ff47f80d1bc70e0e004
diff --git a/phosphor-regulators/src/id_map.cpp b/phosphor-regulators/src/id_map.cpp
index d39fa4f..0f7d3ac 100644
--- a/phosphor-regulators/src/id_map.cpp
+++ b/phosphor-regulators/src/id_map.cpp
@@ -25,17 +25,35 @@
void IDMap::addDevice(Device& device)
{
- deviceMap[device.getID()] = &device;
+ const std::string& id = device.getID();
+ if (deviceMap.count(id) != 0)
+ {
+ throw std::invalid_argument{"Unable to add device: Duplicate ID \"" +
+ id + '"'};
+ }
+ deviceMap[id] = &device;
}
void IDMap::addRail(Rail& rail)
{
- railMap[rail.getID()] = &rail;
+ const std::string& id = rail.getID();
+ if (railMap.count(id) != 0)
+ {
+ throw std::invalid_argument{"Unable to add rail: Duplicate ID \"" + id +
+ '"'};
+ }
+ railMap[id] = &rail;
}
void IDMap::addRule(Rule& rule)
{
- ruleMap[rule.getID()] = &rule;
+ const std::string& id = rule.getID();
+ if (ruleMap.count(id) != 0)
+ {
+ throw std::invalid_argument{"Unable to add rule: Duplicate ID \"" + id +
+ '"'};
+ }
+ ruleMap[id] = &rule;
}
} // namespace phosphor::power::regulators