blob: 8f9f8a986654c9466afe8fe63037f86e76f9d49b [file] [log] [blame]
John Wangb754eee2020-02-15 16:10:25 +08001#pragma once
2
3#include <cstring>
4#include <memory>
5#include <type_traits>
6#include <vector>
7
8#include "libpldm/bios_table.h"
9
10namespace pldm
11{
12namespace bios
13{
14namespace utils
15{
16
17using Table = std::vector<uint8_t>;
18
19/** @class BIOSTableIter
20 * @brief Const Iterator of a BIOS Table
21 */
22template <pldm_bios_table_types tableType>
23class BIOSTableIter
24{
25 public:
26 /** @struct EndSentinel
27 * @brief Auxiliary struct to delimit a range
28 */
29 struct EndSentinel
30 {
31 };
32
33 /** @struct iterator
34 * @brief iterator owns the BIOS table
35 */
36 class iterator
37 {
38 public:
39 /** @brief Get entry type by specifying \p tableType
40 */
41 using T = typename std::conditional<
42 tableType == PLDM_BIOS_STRING_TABLE, pldm_bios_string_table_entry,
43 typename std::conditional<
44 tableType == PLDM_BIOS_ATTR_TABLE, pldm_bios_attr_table_entry,
45 typename std::conditional<tableType == PLDM_BIOS_ATTR_VAL_TABLE,
46 pldm_bios_attr_val_table_entry,
47 void>::type>::type>::type;
48 static_assert(!std::is_void<T>::value);
49
50 /** @brief Constructors iterator
51 *
52 * @param[in] data - Pointer to a table
53 * @param[in] length - The length of the table
54 */
55 explicit iterator(const void* data, size_t length) noexcept :
56 iter(pldm_bios_table_iter_create(data, length, tableType),
57 pldm_bios_table_iter_free)
58 {
59 }
60
61 /** @brief Get the entry pointed by the iterator
62 *
63 * @return Poiner to the entry
64 */
65 const T* operator*() const
66 {
67 return reinterpret_cast<const T*>(
68 pldm_bios_table_iter_value(iter.get()));
69 }
70
71 /** @brief Make the iterator point to the next entry
72 *
73 * @return The iterator itself
74 */
75 iterator& operator++()
76 {
77 pldm_bios_table_iter_next(iter.get());
78 return *this;
79 }
80
81 /** @brief Check if the iterator ends
82 *
83 * @return True if the iterator ends
84 */
85 bool operator==(const EndSentinel&) const
86 {
87 return pldm_bios_table_iter_is_end(iter.get());
88 }
89
90 /** @brief Check if the iterator ends
91 *
92 * @return False if the iterator ends
93 */
94 bool operator!=(const EndSentinel& endSentinel) const
95 {
96 return !operator==(endSentinel);
97 }
98
99 private:
100 std::unique_ptr<pldm_bios_table_iter,
101 decltype(&pldm_bios_table_iter_free)>
102 iter;
103 };
104
105 /** @brief Constructors BIOSTableIterator
106 *
107 * @param[in] data - Pointer to a table
108 * @param[in] length - The length of the table
109 */
110 BIOSTableIter(const void* data, size_t length) noexcept :
111 tableData(data), tableSize(length)
112 {
113 }
114
115 /** @brief Get the iterator to the beginning
116 *
117 * @return An iterator to the beginning
118 */
119 iterator begin()
120 {
121 return iterator(tableData, tableSize);
122 }
123
124 /** @brief Get the iterator to the end
125 *
126 * @return An iterator to the end
127 */
128 EndSentinel end()
129 {
130 return {};
131 }
132
133 private:
134 const void* tableData;
135 size_t tableSize;
136};
137
138} // namespace utils
139} // namespace bios
140} // namespace pldm