Matt Spinler | 15ee6ae | 2019-07-08 16:50:06 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <map> |
| 3 | #include <optional> |
| 4 | #include <string> |
| 5 | #include <vector> |
| 6 | |
| 7 | namespace openpower |
| 8 | { |
| 9 | namespace pels |
| 10 | { |
| 11 | |
| 12 | /** |
| 13 | * @class AdditionalData |
| 14 | * |
| 15 | * This class takes in the contents of the AdditionalData OpenBMC |
| 16 | * event log property, and provides access to its values based on |
| 17 | * their keys. |
| 18 | * |
| 19 | * The property is a vector of strings of the form: "KEY=VALUE", |
| 20 | * and this class provides a getValue("KEY") API that would return |
| 21 | * "VALUE". |
| 22 | */ |
| 23 | class AdditionalData |
| 24 | { |
| 25 | public: |
| 26 | AdditionalData() = delete; |
| 27 | ~AdditionalData() = default; |
| 28 | AdditionalData(const AdditionalData&) = default; |
| 29 | AdditionalData& operator=(const AdditionalData&) = default; |
| 30 | AdditionalData(AdditionalData&&) = default; |
| 31 | AdditionalData& operator=(AdditionalData&&) = default; |
| 32 | |
| 33 | /** |
| 34 | * @brief constructor |
| 35 | * |
| 36 | * @param[in] ad - the AdditionalData property vector with |
| 37 | * entries of "KEY=VALUE" |
| 38 | */ |
| 39 | AdditionalData(const std::vector<std::string>& ad) |
| 40 | { |
| 41 | for (auto& item : ad) |
| 42 | { |
| 43 | auto pos = item.find_first_of('='); |
| 44 | if (pos == std::string::npos || pos == 0) |
| 45 | { |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | _data[item.substr(0, pos)] = std::move(item.substr(pos + 1)); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @brief Returns the value of the AdditionalData item for the |
| 55 | * key passed in. |
| 56 | * @param[in] key - the key to search for |
| 57 | * |
| 58 | * @return optional<string> - the value, if found |
| 59 | */ |
| 60 | std::optional<std::string> getValue(const std::string& key) |
| 61 | { |
| 62 | auto entry = _data.find(key); |
| 63 | if (entry != _data.end()) |
| 64 | { |
| 65 | return entry->second; |
| 66 | } |
| 67 | return std::nullopt; |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | /** |
| 72 | * @brief a map of keys to values |
| 73 | */ |
| 74 | std::map<std::string, std::string> _data; |
| 75 | }; |
| 76 | } // namespace pels |
| 77 | } // namespace openpower |