xref: /openbmc/phosphor-power/phosphor-regulators/src/id_map.hpp (revision f54021972b91be5058b50e9046bb0dd5a3b22a80)
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 <map>
19  #include <stdexcept>
20  #include <string>
21  
22  namespace phosphor::power::regulators
23  {
24  
25  // Forward declarations to avoid circular dependencies
26  class Device;
27  class Rail;
28  class Rule;
29  
30  /**
31   * @class IDMap
32   *
33   * This class provides a mapping from string IDs to the associated Device, Rail,
34   * and Rule objects.
35   */
36  class 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       * Throws invalid_argument if the device's ID already exists in the map.
51       *
52       * @param device device to add
53       */
54      void addDevice(Device& device);
55  
56      /**
57       * Adds the specified rail to this IDMap.
58       *
59       * Throws invalid_argument if the rail's ID already exists in the map.
60       *
61       * @param rail rail to add
62       */
63      void addRail(Rail& rail);
64  
65      /**
66       * Adds the specified rule to this IDMap.
67       *
68       * Throws invalid_argument if the rule's ID already exists in the map.
69       *
70       * @param rule rule to add
71       */
72      void addRule(Rule& rule);
73  
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       */
getDevice(const std::string & id) const82      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{
88                  "Unable to find device with ID \"" + 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       */
getRail(const std::string & id) const101      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{
107                  "Unable to find rail with ID \"" + id + '"'};
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       */
getRule(const std::string & id) const120      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{
126                  "Unable to find rule with ID \"" + id + '"'};
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  
148  } // namespace phosphor::power::regulators
149