13daeb919SShawn McCarney /** 23daeb919SShawn McCarney * Copyright © 2019 IBM Corporation 33daeb919SShawn McCarney * 43daeb919SShawn McCarney * Licensed under the Apache License, Version 2.0 (the "License"); 53daeb919SShawn McCarney * you may not use this file except in compliance with the License. 63daeb919SShawn McCarney * You may obtain a copy of the License at 73daeb919SShawn McCarney * 83daeb919SShawn McCarney * http://www.apache.org/licenses/LICENSE-2.0 93daeb919SShawn McCarney * 103daeb919SShawn McCarney * Unless required by applicable law or agreed to in writing, software 113daeb919SShawn McCarney * distributed under the License is distributed on an "AS IS" BASIS, 123daeb919SShawn McCarney * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133daeb919SShawn McCarney * See the License for the specific language governing permissions and 143daeb919SShawn McCarney * limitations under the License. 153daeb919SShawn McCarney */ 163daeb919SShawn McCarney #pragma once 173daeb919SShawn McCarney 183daeb919SShawn McCarney #include <map> 193daeb919SShawn McCarney #include <stdexcept> 203daeb919SShawn McCarney #include <string> 213daeb919SShawn McCarney 22ea7385b8SShawn McCarney namespace phosphor::power::regulators 233daeb919SShawn McCarney { 243daeb919SShawn McCarney 25494ef03aSShawn McCarney // Forward declarations to avoid circular dependencies 26494ef03aSShawn McCarney class Device; 27494ef03aSShawn McCarney class Rail; 28494ef03aSShawn McCarney class Rule; 29494ef03aSShawn McCarney 303daeb919SShawn McCarney /** 313daeb919SShawn McCarney * @class IDMap 323daeb919SShawn McCarney * 333daeb919SShawn McCarney * This class provides a mapping from string IDs to the associated Device, Rail, 343daeb919SShawn McCarney * and Rule objects. 353daeb919SShawn McCarney */ 363daeb919SShawn McCarney class IDMap 373daeb919SShawn McCarney { 383daeb919SShawn McCarney public: 393daeb919SShawn McCarney // Specify which compiler-generated methods we want 403daeb919SShawn McCarney IDMap() = default; 413daeb919SShawn McCarney IDMap(const IDMap&) = delete; 423daeb919SShawn McCarney IDMap(IDMap&&) = delete; 433daeb919SShawn McCarney IDMap& operator=(const IDMap&) = delete; 443daeb919SShawn McCarney IDMap& operator=(IDMap&&) = delete; 453daeb919SShawn McCarney ~IDMap() = default; 463daeb919SShawn McCarney 473daeb919SShawn McCarney /** 483daeb919SShawn McCarney * Adds the specified device to this IDMap. 493daeb919SShawn McCarney * 50e38f8a21SShawn McCarney * Throws invalid_argument if the device's ID already exists in the map. 51e38f8a21SShawn McCarney * 523daeb919SShawn McCarney * @param device device to add 533daeb919SShawn McCarney */ 54494ef03aSShawn McCarney void addDevice(Device& device); 553daeb919SShawn McCarney 563daeb919SShawn McCarney /** 573daeb919SShawn McCarney * Adds the specified rail to this IDMap. 583daeb919SShawn McCarney * 59e38f8a21SShawn McCarney * Throws invalid_argument if the rail's ID already exists in the map. 60e38f8a21SShawn McCarney * 613daeb919SShawn McCarney * @param rail rail to add 623daeb919SShawn McCarney */ 63494ef03aSShawn McCarney void addRail(Rail& rail); 643daeb919SShawn McCarney 653daeb919SShawn McCarney /** 663daeb919SShawn McCarney * Adds the specified rule to this IDMap. 673daeb919SShawn McCarney * 68e38f8a21SShawn McCarney * Throws invalid_argument if the rule's ID already exists in the map. 69e38f8a21SShawn McCarney * 703daeb919SShawn McCarney * @param rule rule to add 713daeb919SShawn McCarney */ 72494ef03aSShawn McCarney void addRule(Rule& rule); 733daeb919SShawn McCarney 743daeb919SShawn McCarney /** 753daeb919SShawn McCarney * Returns the device with the specified ID. 763daeb919SShawn McCarney * 773daeb919SShawn McCarney * Throws invalid_argument if no device is found with specified ID. 783daeb919SShawn McCarney * 793daeb919SShawn McCarney * @param id device ID 803daeb919SShawn McCarney * @return device with specified ID 813daeb919SShawn McCarney */ getDevice(const std::string & id) const823daeb919SShawn McCarney Device& getDevice(const std::string& id) const 833daeb919SShawn McCarney { 843daeb919SShawn McCarney auto it = deviceMap.find(id); 853daeb919SShawn McCarney if (it == deviceMap.end()) 863daeb919SShawn McCarney { 87*f5402197SPatrick Williams throw std::invalid_argument{ 88*f5402197SPatrick Williams "Unable to find device with ID \"" + id + '"'}; 893daeb919SShawn McCarney } 903daeb919SShawn McCarney return *(it->second); 913daeb919SShawn McCarney } 923daeb919SShawn McCarney 933daeb919SShawn McCarney /** 943daeb919SShawn McCarney * Returns the rail with the specified ID. 953daeb919SShawn McCarney * 963daeb919SShawn McCarney * Throws invalid_argument if no rail is found with specified ID. 973daeb919SShawn McCarney * 983daeb919SShawn McCarney * @param id rail ID 993daeb919SShawn McCarney * @return rail with specified ID 1003daeb919SShawn McCarney */ getRail(const std::string & id) const1013daeb919SShawn McCarney Rail& getRail(const std::string& id) const 1023daeb919SShawn McCarney { 1033daeb919SShawn McCarney auto it = railMap.find(id); 1043daeb919SShawn McCarney if (it == railMap.end()) 1053daeb919SShawn McCarney { 106*f5402197SPatrick Williams throw std::invalid_argument{ 107*f5402197SPatrick Williams "Unable to find rail with ID \"" + id + '"'}; 1083daeb919SShawn McCarney } 1093daeb919SShawn McCarney return *(it->second); 1103daeb919SShawn McCarney } 1113daeb919SShawn McCarney 1123daeb919SShawn McCarney /** 1133daeb919SShawn McCarney * Returns the rule with the specified ID. 1143daeb919SShawn McCarney * 1153daeb919SShawn McCarney * Throws invalid_argument if no rule is found with specified ID. 1163daeb919SShawn McCarney * 1173daeb919SShawn McCarney * @param id rule ID 1183daeb919SShawn McCarney * @return rule with specified ID 1193daeb919SShawn McCarney */ getRule(const std::string & id) const1203daeb919SShawn McCarney Rule& getRule(const std::string& id) const 1213daeb919SShawn McCarney { 1223daeb919SShawn McCarney auto it = ruleMap.find(id); 1233daeb919SShawn McCarney if (it == ruleMap.end()) 1243daeb919SShawn McCarney { 125*f5402197SPatrick Williams throw std::invalid_argument{ 126*f5402197SPatrick Williams "Unable to find rule with ID \"" + id + '"'}; 1273daeb919SShawn McCarney } 1283daeb919SShawn McCarney return *(it->second); 1293daeb919SShawn McCarney } 1303daeb919SShawn McCarney 1313daeb919SShawn McCarney private: 1323daeb919SShawn McCarney /** 1333daeb919SShawn McCarney * Map from device IDs to Device objects. Does not own the objects. 1343daeb919SShawn McCarney */ 1353daeb919SShawn McCarney std::map<std::string, Device*> deviceMap{}; 1363daeb919SShawn McCarney 1373daeb919SShawn McCarney /** 1383daeb919SShawn McCarney * Map from rail IDs to Rail objects. Does not own the objects. 1393daeb919SShawn McCarney */ 1403daeb919SShawn McCarney std::map<std::string, Rail*> railMap{}; 1413daeb919SShawn McCarney 1423daeb919SShawn McCarney /** 1433daeb919SShawn McCarney * Map from rule IDs to Rule objects. Does not own the objects. 1443daeb919SShawn McCarney */ 1453daeb919SShawn McCarney std::map<std::string, Rule*> ruleMap{}; 1463daeb919SShawn McCarney }; 1473daeb919SShawn McCarney 148ea7385b8SShawn McCarney } // namespace phosphor::power::regulators 149