blob: 289c7a97a6637f48d0e32d8ef9617663d8925bf5 [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 *
50 * @param device device to add
51 */
Shawn McCarney494ef032019-11-07 15:56:50 -060052 void addDevice(Device& device);
Shawn McCarney3daeb912019-10-28 15:04:17 -050053
54 /**
55 * Adds the specified rail to this IDMap.
56 *
57 * @param rail rail to add
58 */
Shawn McCarney494ef032019-11-07 15:56:50 -060059 void addRail(Rail& rail);
Shawn McCarney3daeb912019-10-28 15:04:17 -050060
61 /**
62 * Adds the specified rule to this IDMap.
63 *
64 * @param rule rule to add
65 */
Shawn McCarney494ef032019-11-07 15:56:50 -060066 void addRule(Rule& rule);
Shawn McCarney3daeb912019-10-28 15:04:17 -050067
68 /**
69 * Returns the device with the specified ID.
70 *
71 * Throws invalid_argument if no device is found with specified ID.
72 *
73 * @param id device ID
74 * @return device with specified ID
75 */
76 Device& getDevice(const std::string& id) const
77 {
78 auto it = deviceMap.find(id);
79 if (it == deviceMap.end())
80 {
81 throw std::invalid_argument{"Unable to find device with ID \"" +
82 id + '"'};
83 }
84 return *(it->second);
85 }
86
87 /**
88 * Returns the rail with the specified ID.
89 *
90 * Throws invalid_argument if no rail is found with specified ID.
91 *
92 * @param id rail ID
93 * @return rail with specified ID
94 */
95 Rail& getRail(const std::string& id) const
96 {
97 auto it = railMap.find(id);
98 if (it == railMap.end())
99 {
100 throw std::invalid_argument{"Unable to find rail with ID \"" + id +
101 '"'};
102 }
103 return *(it->second);
104 }
105
106 /**
107 * Returns the rule with the specified ID.
108 *
109 * Throws invalid_argument if no rule is found with specified ID.
110 *
111 * @param id rule ID
112 * @return rule with specified ID
113 */
114 Rule& getRule(const std::string& id) const
115 {
116 auto it = ruleMap.find(id);
117 if (it == ruleMap.end())
118 {
119 throw std::invalid_argument{"Unable to find rule with ID \"" + id +
120 '"'};
121 }
122 return *(it->second);
123 }
124
125 private:
126 /**
127 * Map from device IDs to Device objects. Does not own the objects.
128 */
129 std::map<std::string, Device*> deviceMap{};
130
131 /**
132 * Map from rail IDs to Rail objects. Does not own the objects.
133 */
134 std::map<std::string, Rail*> railMap{};
135
136 /**
137 * Map from rule IDs to Rule objects. Does not own the objects.
138 */
139 std::map<std::string, Rule*> ruleMap{};
140};
141
Shawn McCarneyea7385b2019-11-07 12:19:32 -0600142} // namespace phosphor::power::regulators