blob: c466f23c2bea6d002a4b1029f03b57716e2534bc [file] [log] [blame]
Shawn McCarney3daeb912019-10-28 15:04:17 -05001/**
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
Shawn McCarney3daeb912019-10-28 15:04:17 -050018#include <map>
19#include <stdexcept>
20#include <string>
21
Shawn McCarneyea7385b2019-11-07 12:19:32 -060022namespace phosphor::power::regulators
Shawn McCarney3daeb912019-10-28 15:04:17 -050023{
24
Shawn McCarney494ef032019-11-07 15:56:50 -060025// Forward declarations to avoid circular dependencies
26class Device;
27class Rail;
28class Rule;
29
Shawn McCarney3daeb912019-10-28 15:04:17 -050030/**
31 * @class IDMap
32 *
33 * This class provides a mapping from string IDs to the associated Device, Rail,
34 * and Rule objects.
35 */
36class IDMap
37{
38 public:
39 // Specify which compiler-generated methods we want
40 IDMap() = default;
41 IDMap(const IDMap&) = delete;
42 IDMap(IDMap&&) = delete;
43 IDMap& operator=(const IDMap&) = delete;
44 IDMap& operator=(IDMap&&) = delete;
45 ~IDMap() = default;
46
47 /**
48 * Adds the specified device to this IDMap.
49 *
Shawn McCarneye38f8a22020-04-05 15:39:14 -050050 * Throws invalid_argument if the device's ID already exists in the map.
51 *
Shawn McCarney3daeb912019-10-28 15:04:17 -050052 * @param device device to add
53 */
Shawn McCarney494ef032019-11-07 15:56:50 -060054 void addDevice(Device& device);
Shawn McCarney3daeb912019-10-28 15:04:17 -050055
56 /**
57 * Adds the specified rail to this IDMap.
58 *
Shawn McCarneye38f8a22020-04-05 15:39:14 -050059 * Throws invalid_argument if the rail's ID already exists in the map.
60 *
Shawn McCarney3daeb912019-10-28 15:04:17 -050061 * @param rail rail to add
62 */
Shawn McCarney494ef032019-11-07 15:56:50 -060063 void addRail(Rail& rail);
Shawn McCarney3daeb912019-10-28 15:04:17 -050064
65 /**
66 * Adds the specified rule to this IDMap.
67 *
Shawn McCarneye38f8a22020-04-05 15:39:14 -050068 * Throws invalid_argument if the rule's ID already exists in the map.
69 *
Shawn McCarney3daeb912019-10-28 15:04:17 -050070 * @param rule rule to add
71 */
Shawn McCarney494ef032019-11-07 15:56:50 -060072 void addRule(Rule& rule);
Shawn McCarney3daeb912019-10-28 15:04:17 -050073
74 /**
75 * Returns the device with the specified ID.
76 *
77 * Throws invalid_argument if no device is found with specified ID.
78 *
79 * @param id device ID
80 * @return device with specified ID
81 */
82 Device& getDevice(const std::string& id) const
83 {
84 auto it = deviceMap.find(id);
85 if (it == deviceMap.end())
86 {
87 throw std::invalid_argument{"Unable to find device with ID \"" +
88 id + '"'};
89 }
90 return *(it->second);
91 }
92
93 /**
94 * Returns the rail with the specified ID.
95 *
96 * Throws invalid_argument if no rail is found with specified ID.
97 *
98 * @param id rail ID
99 * @return rail with specified ID
100 */
101 Rail& getRail(const std::string& id) const
102 {
103 auto it = railMap.find(id);
104 if (it == railMap.end())
105 {
106 throw std::invalid_argument{"Unable to find rail with ID \"" + id +
107 '"'};
108 }
109 return *(it->second);
110 }
111
112 /**
113 * Returns the rule with the specified ID.
114 *
115 * Throws invalid_argument if no rule is found with specified ID.
116 *
117 * @param id rule ID
118 * @return rule with specified ID
119 */
120 Rule& getRule(const std::string& id) const
121 {
122 auto it = ruleMap.find(id);
123 if (it == ruleMap.end())
124 {
125 throw std::invalid_argument{"Unable to find rule with ID \"" + id +
126 '"'};
127 }
128 return *(it->second);
129 }
130
131 private:
132 /**
133 * Map from device IDs to Device objects. Does not own the objects.
134 */
135 std::map<std::string, Device*> deviceMap{};
136
137 /**
138 * Map from rail IDs to Rail objects. Does not own the objects.
139 */
140 std::map<std::string, Rail*> railMap{};
141
142 /**
143 * Map from rule IDs to Rule objects. Does not own the objects.
144 */
145 std::map<std::string, Rule*> ruleMap{};
146};
147
Shawn McCarneyea7385b2019-11-07 12:19:32 -0600148} // namespace phosphor::power::regulators