1 /** 2 * Copyright © 2019 IBM Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #pragma once 17 18 #include "device.hpp" 19 #include "rail.hpp" 20 #include "rule.hpp" 21 22 #include <map> 23 #include <stdexcept> 24 #include <string> 25 26 namespace phosphor 27 { 28 namespace power 29 { 30 namespace regulators 31 { 32 33 /** 34 * @class IDMap 35 * 36 * This class provides a mapping from string IDs to the associated Device, Rail, 37 * and Rule objects. 38 */ 39 class IDMap 40 { 41 public: 42 // Specify which compiler-generated methods we want 43 IDMap() = default; 44 IDMap(const IDMap&) = delete; 45 IDMap(IDMap&&) = delete; 46 IDMap& operator=(const IDMap&) = delete; 47 IDMap& operator=(IDMap&&) = delete; 48 ~IDMap() = default; 49 50 /** 51 * Adds the specified device to this IDMap. 52 * 53 * @param device device to add 54 */ 55 void addDevice(Device& device) 56 { 57 deviceMap[device.getID()] = &device; 58 } 59 60 /** 61 * Adds the specified rail to this IDMap. 62 * 63 * @param rail rail to add 64 */ 65 void addRail(Rail& rail) 66 { 67 railMap[rail.getID()] = &rail; 68 } 69 70 /** 71 * Adds the specified rule to this IDMap. 72 * 73 * @param rule rule to add 74 */ 75 void addRule(Rule& rule) 76 { 77 ruleMap[rule.getID()] = &rule; 78 } 79 80 /** 81 * Returns the device with the specified ID. 82 * 83 * Throws invalid_argument if no device is found with specified ID. 84 * 85 * @param id device ID 86 * @return device with specified ID 87 */ 88 Device& getDevice(const std::string& id) const 89 { 90 auto it = deviceMap.find(id); 91 if (it == deviceMap.end()) 92 { 93 throw std::invalid_argument{"Unable to find device with ID \"" + 94 id + '"'}; 95 } 96 return *(it->second); 97 } 98 99 /** 100 * Returns the rail with the specified ID. 101 * 102 * Throws invalid_argument if no rail is found with specified ID. 103 * 104 * @param id rail ID 105 * @return rail with specified ID 106 */ 107 Rail& getRail(const std::string& id) const 108 { 109 auto it = railMap.find(id); 110 if (it == railMap.end()) 111 { 112 throw std::invalid_argument{"Unable to find rail with ID \"" + id + 113 '"'}; 114 } 115 return *(it->second); 116 } 117 118 /** 119 * Returns the rule with the specified ID. 120 * 121 * Throws invalid_argument if no rule is found with specified ID. 122 * 123 * @param id rule ID 124 * @return rule with specified ID 125 */ 126 Rule& getRule(const std::string& id) const 127 { 128 auto it = ruleMap.find(id); 129 if (it == ruleMap.end()) 130 { 131 throw std::invalid_argument{"Unable to find rule with ID \"" + id + 132 '"'}; 133 } 134 return *(it->second); 135 } 136 137 private: 138 /** 139 * Map from device IDs to Device objects. Does not own the objects. 140 */ 141 std::map<std::string, Device*> deviceMap{}; 142 143 /** 144 * Map from rail IDs to Rail objects. Does not own the objects. 145 */ 146 std::map<std::string, Rail*> railMap{}; 147 148 /** 149 * Map from rule IDs to Rule objects. Does not own the objects. 150 */ 151 std::map<std::string, Rule*> ruleMap{}; 152 }; 153 154 } // namespace regulators 155 } // namespace power 156 } // namespace phosphor 157