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 | |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <unordered_map> |
| 8 | |
Deepak Kodihalli | 35c7fb1 | 2016-11-21 04:32:44 -0600 | [diff] [blame] | 9 | namespace openpower |
| 10 | { |
| 11 | namespace 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 Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 16 | using Parsed = std::unordered_map<std::string, |
| 17 | std::unordered_map<std::string, std::string>>; |
Deepak Kodihalli | 35c7fb1 | 2016-11-21 04:32:44 -0600 | [diff] [blame] | 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 | */ |
| 26 | class Store final |
| 27 | { |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 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; |
Deepak Kodihalli | 35c7fb1 | 2016-11-21 04:32:44 -0600 | [diff] [blame] | 35 | |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 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 | } |
Deepak Kodihalli | 35c7fb1 | 2016-11-21 04:32:44 -0600 | [diff] [blame] | 43 | |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 44 | /** @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 Kodihalli | 35c7fb1 | 2016-11-21 04:32:44 -0600 | [diff] [blame] | 52 | |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 53 | /** @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 Kodihalli | 7e7821c | 2017-09-11 23:32:01 -0500 | [diff] [blame] | 66 | |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 67 | private: |
| 68 | /** @brief The store for parsed VPD */ |
| 69 | Parsed vpd; |
Deepak Kodihalli | 35c7fb1 | 2016-11-21 04:32:44 -0600 | [diff] [blame] | 70 | }; |
| 71 | |
Patrick Venture | c83c4dc | 2018-11-01 16:29:18 -0700 | [diff] [blame] | 72 | template <Record R, record::Keyword K> |
Deepak Kodihalli | 158c046 | 2016-11-21 21:33:28 -0600 | [diff] [blame] | 73 | inline 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 Kodihalli | 35c7fb1 | 2016-11-21 04:32:44 -0600 | [diff] [blame] | 90 | } // namespace vpd |
| 91 | } // namespace openpower |