blob: d5e0d7268af1fb2c546023521330c5f7866041cd [file] [log] [blame]
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -06001#pragma once
2
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -06003#include "defines.hpp"
4#include "types.hpp"
5
Patrick Venturec83c4dc2018-11-01 16:29:18 -07006#include <string>
7#include <unordered_map>
8
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -06009namespace openpower
10{
11namespace vpd
12{
13
14/** @brief Parsed VPD is represented as a dictionary of records, where
15 * each record in itself is a dictionary of keywords */
Patrick Venturec83c4dc2018-11-01 16:29:18 -070016using Parsed = std::unordered_map<std::string,
17 std::unordered_map<std::string, std::string>>;
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060018
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{
Patrick Venturec83c4dc2018-11-01 16:29:18 -070028 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;
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060035
Patrick Venturec83c4dc2018-11-01 16:29:18 -070036 /** @brief Construct a Store
37 *
38 * @param[in] vpdBuffer - A parsed VPD object
39 */
40 explicit Store(Parsed&& vpdBuffer) : vpd(std::move(vpdBuffer))
41 {
42 }
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060043
Patrick Venturec83c4dc2018-11-01 16:29:18 -070044 /** @brief Retrieves VPD from Store
45 *
46 * @tparam R - VPD record
47 * @tparam K - VPD keyword
48 * @returns VPD stored in input record:keyword
49 */
50 template <Record R, record::Keyword K>
51 inline const std::string& get() const;
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060052
Patrick Venturec83c4dc2018-11-01 16:29:18 -070053 /** @brief Checks if VPD exists in store
54 *
55 * @tparam R - VPD record
56 * @tparam K - VPD keyword
57 * @returns true if {R,K} exists
58 */
59 template <Record R, record::Keyword K>
60 bool exists() const
61 {
62 static const std::string record = getRecord<R>();
63 static const std::string keyword = record::getKeyword<K>();
64 return vpd.count(record) && vpd.at(record).count(keyword);
65 }
Deepak Kodihalli7e7821c2017-09-11 23:32:01 -050066
Patrick Venturec83c4dc2018-11-01 16:29:18 -070067 private:
68 /** @brief The store for parsed VPD */
69 Parsed vpd;
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060070};
71
Patrick Venturec83c4dc2018-11-01 16:29:18 -070072template <Record R, record::Keyword K>
Deepak Kodihalli158c0462016-11-21 21:33:28 -060073inline const std::string& Store::get() const
74{
75 static const std::string record = getRecord<R>();
76 static const std::string keyword = record::getKeyword<K>();
77 static const std::string empty = "";
78 auto kw = vpd.find(record);
79 if (vpd.end() != kw)
80 {
81 auto value = (kw->second).find(keyword);
82 if ((kw->second).end() != value)
83 {
84 return value->second;
85 }
86 }
87 return empty;
88}
89
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060090} // namespace vpd
91} // namespace openpower