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
diff --git a/phosphor-regulators/src/id_map.hpp b/phosphor-regulators/src/id_map.hpp
index 289c7a9..c466f23 100644
--- a/phosphor-regulators/src/id_map.hpp
+++ b/phosphor-regulators/src/id_map.hpp
@@ -47,6 +47,8 @@
     /**
      * Adds the specified device to this IDMap.
      *
+     * Throws invalid_argument if the device's ID already exists in the map.
+     *
      * @param device device to add
      */
     void addDevice(Device& device);
@@ -54,6 +56,8 @@
     /**
      * Adds the specified rail to this IDMap.
      *
+     * Throws invalid_argument if the rail's ID already exists in the map.
+     *
      * @param rail rail to add
      */
     void addRail(Rail& rail);
@@ -61,6 +65,8 @@
     /**
      * Adds the specified rule to this IDMap.
      *
+     * Throws invalid_argument if the rule's ID already exists in the map.
+     *
      * @param rule rule to add
      */
     void addRule(Rule& rule);