blob: b855054b5112de2c36233cd1f0a3ebc9348774b5 [file] [log] [blame]
Matt Spinler15ee6ae2019-07-08 16:50:06 -05001#pragma once
2#include <map>
3#include <optional>
4#include <string>
5#include <vector>
6
7namespace openpower
8{
9namespace 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 */
23class AdditionalData
24{
25 public:
Matt Spinler935a25e2019-10-14 16:28:08 -050026 AdditionalData() = default;
Matt Spinler15ee6ae2019-07-08 16:50:06 -050027 ~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 */
Matt Spinler935a25e2019-10-14 16:28:08 -050060 std::optional<std::string> getValue(const std::string& key) const
Matt Spinler15ee6ae2019-07-08 16:50:06 -050061 {
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