blob: 9da07f40d93c2de4f82aa941874412f4e0ee80e2 [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
42 /** @brief Retrives VPD from Store
43 *
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
51 private:
52 /** @brief The store for parsed VPD */
53 Parsed vpd;
54};
55
Deepak Kodihalli158c0462016-11-21 21:33:28 -060056template<Record R, record::Keyword K>
57inline const std::string& Store::get() const
58{
59 static const std::string record = getRecord<R>();
60 static const std::string keyword = record::getKeyword<K>();
61 static const std::string empty = "";
62 auto kw = vpd.find(record);
63 if (vpd.end() != kw)
64 {
65 auto value = (kw->second).find(keyword);
66 if ((kw->second).end() != value)
67 {
68 return value->second;
69 }
70 }
71 return empty;
72}
73
Deepak Kodihalli35c7fb12016-11-21 04:32:44 -060074} // namespace vpd
75} // namespace openpower