blob: 340446f331d5b176a99f49608adb408aa5498439 [file] [log] [blame]
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -06001#pragma once
2
3#include <string>
4#include <unordered_map>
5#include "defines.hpp"
6#include "types.hpp"
7
8namespace openpower
9{
10namespace vpd
11{
12
13/** @brief Parsed VPD is represented as a dictionary of records, where
14 * each record in itself is a dictionary of keywords */
15using Parsed =
16 std::unordered_map<std::string,
17 std::unordered_map<std::string, std::string>>;
18
19/** @class Store
20 * @brief Store for parsed OpenPOWER VPD
21 *
22 * A Store object stores parsed OpenPOWER VPD, and provides access
23 * to the VPD, specified by record and keyword. Parsed VPD is typically
24 * provided by the Parser class.
25 */
26class Store final
27{
28 public:
29 Store() = delete;
30 Store(const Store&) = delete;
31 Store& operator=(const Store&) = delete;
32 Store(Store&&) = default;
33 Store& operator=(Store&&) = default;
34 ~Store() = default;
35
36 /** @brief Construct a Store
37 *
38 * @param[in] vpdBuffer - A parsed VPD object
39 */
40 explicit Store(Parsed&& vpdBuffer): vpd(std::move(vpdBuffer)) {}
41
Gunnar Mills7b9c2052018-04-08 15:02:01 -050042 /** @brief Retrieves VPD from Store
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060043 *
44 * @tparam R - VPD record
45 * @tparam K - VPD keyword
46 * @returns VPD stored in input record:keyword
47 */
48 template<Record R, record::Keyword K>
49 inline const std::string& get() const;
50
Deepak Kodihalli7e7821c2017-09-11 23:32:01 -050051 /** @brief Checks if VPD exists in store
52 *
53 * @tparam R - VPD record
54 * @tparam K - VPD keyword
55 * @returns true if {R,K} exists
56 */
57 template<Record R, record::Keyword K>
58 bool exists() const
59 {
60 static const std::string record = getRecord<R>();
61 static const std::string keyword = record::getKeyword<K>();
62 return vpd.count(record) && vpd.at(record).count(keyword);
63 }
64
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060065 private:
66 /** @brief The store for parsed VPD */
67 Parsed vpd;
68};
69
Deepak Kodihalli158c0462016-11-21 21:33:28 -060070template<Record R, record::Keyword K>
71inline const std::string& Store::get() const
72{
73 static const std::string record = getRecord<R>();
74 static const std::string keyword = record::getKeyword<K>();
75 static const std::string empty = "";
76 auto kw = vpd.find(record);
77 if (vpd.end() != kw)
78 {
79 auto value = (kw->second).find(keyword);
80 if ((kw->second).end() != value)
81 {
82 return value->second;
83 }
84 }
85 return empty;
86}
87
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060088} // namespace vpd
89} // namespace openpower