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