summaryrefslogtreecommitdiffstats
path: root/store.hpp
blob: d5e0d7268af1fb2c546023521330c5f7866041cd (plain)
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
#pragma once

#include "defines.hpp"
#include "types.hpp"

#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
     *
     *  @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);
    }

  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
OpenPOWER on IntegriCloud