-
Notifications
You must be signed in to change notification settings - Fork 10
/
names_values.hpp
111 lines (102 loc) · 2.22 KB
/
names_values.hpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#pragma once
#include <cstdint>
#include <sstream>
#include <string>
namespace phosphor
{
namespace power
{
namespace util
{
/**
* @class NamesValues
*
* Builds up a string of name=value pairs for use in
* situations like error log metadata.
*
* Names and values can be added to an instance of this
* class, and then calling get() will return a string
* that appends them all together.
*
* Currently, just uint64_t values that will be displayed
* in hex are supported.
*
* For example:
* NamesValues m;
* uint8_t d = 0xFF;
*
* m.add("STATUS_VOUT", d);
* m.add("STATUS_WORD", 0xABCD);
*
* m.get() //returns: "STATUS_VOUT=0xFF|STATUS_WORD=0xABCD"
*/
class NamesValues
{
public:
NamesValues() = default;
NamesValues(const NamesValues&) = default;
NamesValues& operator=(const NamesValues&) = default;
NamesValues(NamesValues&&) = default;
NamesValues& operator=(NamesValues&&) = default;
/**
* Adds a name/value pair to the object
*
* @param name - the name to add
* @param value - the value to add
*/
void add(const std::string& name, uint64_t value)
{
addDivider();
addName(name);
addValue(value);
}
/**
* Returns a formatted concatenation of all of the names and
* their values.
*
* @return string - "<name1>=<value1>|<name2>=<value2>..etc"
*/
const std::string& get() const
{
return all;
}
private:
/**
* Adds a name to the object
*
* @param name - the name to add
*/
void addName(const std::string& name)
{
all += name + '=';
}
/**
* Adds a value to the object
*
* @param value - the value to add
*/
void addValue(uint64_t value)
{
std::ostringstream stream;
stream << "0x" << std::hex << value;
all += stream.str();
}
/**
* Adds a divider to the summary string to
* break up the name/value pairs
*/
void addDivider()
{
if (!all.empty())
{
all += '|';
}
}
/**
* The string containing all name/value pairs
*/
std::string all;
};
} // namespace util
} // namespace power
} // namespace phosphor