forked from openbmc/openpower-vpd-parser
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathstore.hpp
114 lines (100 loc) · 2.83 KB
/
store.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once
#include "defines.hpp"
#include "types.hpp"
#include <iostream>
#include <string>
#include <unordered_map>
namespace openpower
{
namespace vpd
{
/** @brief Parsed VPD is represented as a dictionary of records, where
* each record in itself is a dictionary of keywords */
using Parsed = std::unordered_map<std::string,
std::unordered_map<std::string, std::string>>;
/** @class Store
* @brief Store for parsed OpenPOWER VPD
*
* A Store object stores parsed OpenPOWER VPD, and provides access
* to the VPD, specified by record and keyword. Parsed VPD is typically
* provided by the Parser class.
*/
class Store final
{
public:
Store() = delete;
Store(const Store&) = delete;
Store& operator=(const Store&) = delete;
Store(Store&&) = default;
Store& operator=(Store&&) = default;
~Store() = default;
/** @brief Construct a Store
*
* @param[in] vpdBuffer - A parsed VPD object
*/
explicit Store(Parsed&& vpdBuffer) : vpd(std::move(vpdBuffer)) {}
/** @brief Retrieves VPD from Store as a Parsed object
*
* @returns VPD as a Parsed object
*/
inline Parsed& getVpdMap()
{
return vpd;
}
/** @brief Retrieves VPD from Store
*
* @tparam R - VPD record
* @tparam K - VPD keyword
* @returns VPD stored in input record:keyword
*/
template <Record R, record::Keyword K>
inline const std::string& get() const;
/** @brief Checks if VPD exists in store
*
* @tparam R - VPD record
* @tparam K - VPD keyword
* @returns true if {R,K} exists
*/
template <Record R, record::Keyword K>
bool exists() const
{
static const std::string record = getRecord<R>();
static const std::string keyword = record::getKeyword<K>();
return vpd.count(record) && vpd.at(record).count(keyword);
}
/** @brief Displays all data in the store to stdout
*/
void dump() const
{
for (const auto& [vpdname, avpd] : vpd)
{
std::cout << vpdname << ": " << std::endl;
for (const auto& [key, val] : avpd)
{
std::cout << "\t" << key << " : " << val << std::endl;
}
}
}
private:
/** @brief The store for parsed VPD */
Parsed vpd;
};
template <Record R, record::Keyword K>
inline const std::string& Store::get() const
{
static const std::string record = getRecord<R>();
static const std::string keyword = record::getKeyword<K>();
static const std::string empty = "";
auto kw = vpd.find(record);
if (vpd.end() != kw)
{
auto value = (kw->second).find(keyword);
if ((kw->second).end() != value)
{
return value->second;
}
}
return empty;
}
} // namespace vpd
} // namespace openpower