Shawn McCarney | 3daeb91 | 2019-10-28 15:04:17 -0500 | [diff] [blame] | 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 | |
Shawn McCarney | ea7385b | 2019-11-07 12:19:32 -0600 | [diff] [blame^] | 26 | namespace phosphor::power::regulators |
Shawn McCarney | 3daeb91 | 2019-10-28 15:04:17 -0500 | [diff] [blame] | 27 | { |
| 28 | |
| 29 | /** |
| 30 | * @class IDMap |
| 31 | * |
| 32 | * This class provides a mapping from string IDs to the associated Device, Rail, |
| 33 | * and Rule objects. |
| 34 | */ |
| 35 | class IDMap |
| 36 | { |
| 37 | public: |
| 38 | // Specify which compiler-generated methods we want |
| 39 | IDMap() = default; |
| 40 | IDMap(const IDMap&) = delete; |
| 41 | IDMap(IDMap&&) = delete; |
| 42 | IDMap& operator=(const IDMap&) = delete; |
| 43 | IDMap& operator=(IDMap&&) = delete; |
| 44 | ~IDMap() = default; |
| 45 | |
| 46 | /** |
| 47 | * Adds the specified device to this IDMap. |
| 48 | * |
| 49 | * @param device device to add |
| 50 | */ |
| 51 | void addDevice(Device& device) |
| 52 | { |
| 53 | deviceMap[device.getID()] = &device; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Adds the specified rail to this IDMap. |
| 58 | * |
| 59 | * @param rail rail to add |
| 60 | */ |
| 61 | void addRail(Rail& rail) |
| 62 | { |
| 63 | railMap[rail.getID()] = &rail; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Adds the specified rule to this IDMap. |
| 68 | * |
| 69 | * @param rule rule to add |
| 70 | */ |
| 71 | void addRule(Rule& rule) |
| 72 | { |
| 73 | ruleMap[rule.getID()] = &rule; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the device with the specified ID. |
| 78 | * |
| 79 | * Throws invalid_argument if no device is found with specified ID. |
| 80 | * |
| 81 | * @param id device ID |
| 82 | * @return device with specified ID |
| 83 | */ |
| 84 | Device& getDevice(const std::string& id) const |
| 85 | { |
| 86 | auto it = deviceMap.find(id); |
| 87 | if (it == deviceMap.end()) |
| 88 | { |
| 89 | throw std::invalid_argument{"Unable to find device with ID \"" + |
| 90 | id + '"'}; |
| 91 | } |
| 92 | return *(it->second); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the rail with the specified ID. |
| 97 | * |
| 98 | * Throws invalid_argument if no rail is found with specified ID. |
| 99 | * |
| 100 | * @param id rail ID |
| 101 | * @return rail with specified ID |
| 102 | */ |
| 103 | Rail& getRail(const std::string& id) const |
| 104 | { |
| 105 | auto it = railMap.find(id); |
| 106 | if (it == railMap.end()) |
| 107 | { |
| 108 | throw std::invalid_argument{"Unable to find rail with ID \"" + id + |
| 109 | '"'}; |
| 110 | } |
| 111 | return *(it->second); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns the rule with the specified ID. |
| 116 | * |
| 117 | * Throws invalid_argument if no rule is found with specified ID. |
| 118 | * |
| 119 | * @param id rule ID |
| 120 | * @return rule with specified ID |
| 121 | */ |
| 122 | Rule& getRule(const std::string& id) const |
| 123 | { |
| 124 | auto it = ruleMap.find(id); |
| 125 | if (it == ruleMap.end()) |
| 126 | { |
| 127 | throw std::invalid_argument{"Unable to find rule with ID \"" + id + |
| 128 | '"'}; |
| 129 | } |
| 130 | return *(it->second); |
| 131 | } |
| 132 | |
| 133 | private: |
| 134 | /** |
| 135 | * Map from device IDs to Device objects. Does not own the objects. |
| 136 | */ |
| 137 | std::map<std::string, Device*> deviceMap{}; |
| 138 | |
| 139 | /** |
| 140 | * Map from rail IDs to Rail objects. Does not own the objects. |
| 141 | */ |
| 142 | std::map<std::string, Rail*> railMap{}; |
| 143 | |
| 144 | /** |
| 145 | * Map from rule IDs to Rule objects. Does not own the objects. |
| 146 | */ |
| 147 | std::map<std::string, Rule*> ruleMap{}; |
| 148 | }; |
| 149 | |
Shawn McCarney | ea7385b | 2019-11-07 12:19:32 -0600 | [diff] [blame^] | 150 | } // namespace phosphor::power::regulators |