Brandon Wyman | 83abbd6 | 2017-09-25 16:41:39 -0500 | [diff] [blame] | 1 | #pragma once |
Matt Spinler | 3b090d1 | 2017-09-08 15:07:05 -0500 | [diff] [blame] | 2 | #include <sstream> |
| 3 | #include <string> |
| 4 | |
| 5 | namespace witherspoon |
| 6 | { |
| 7 | namespace power |
| 8 | { |
| 9 | namespace util |
| 10 | { |
| 11 | |
| 12 | /** |
| 13 | * @class NamesValues |
| 14 | * |
| 15 | * Builds up a string of name=value pairs for use in |
| 16 | * situations like error log metadata. |
| 17 | * |
| 18 | * Names and values can be added to an instance of this |
| 19 | * class, and then calling get() will return a string |
| 20 | * that appends them all together. |
| 21 | * |
| 22 | * Currently, just uint64_t values that will be displayed |
| 23 | * in hex are supported. |
| 24 | * |
| 25 | * For example: |
| 26 | * NamesValues m; |
| 27 | * uint8_t d = 0xFF; |
| 28 | * |
| 29 | * m.add("STATUS_VOUT", d); |
| 30 | * m.add("STATUS_WORD", 0xABCD); |
| 31 | * |
| 32 | * m.get() //returns: "STATUS_VOUT=0xFF|STATUS_WORD=0xABCD" |
| 33 | */ |
| 34 | class NamesValues |
| 35 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 36 | public: |
| 37 | NamesValues() = default; |
| 38 | NamesValues(const NamesValues&) = default; |
| 39 | NamesValues& operator=(const NamesValues&) = default; |
| 40 | NamesValues(NamesValues&&) = default; |
| 41 | NamesValues& operator=(NamesValues&&) = default; |
Matt Spinler | 3b090d1 | 2017-09-08 15:07:05 -0500 | [diff] [blame] | 42 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 43 | /** |
| 44 | * Adds a name/value pair to the object |
| 45 | * |
| 46 | * @param name - the name to add |
| 47 | * @param value - the value to add |
| 48 | */ |
| 49 | void add(const std::string& name, uint64_t value) |
| 50 | { |
| 51 | addDivider(); |
| 52 | addName(name); |
| 53 | addValue(value); |
| 54 | } |
Matt Spinler | 3b090d1 | 2017-09-08 15:07:05 -0500 | [diff] [blame] | 55 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 56 | /** |
| 57 | * Returns a formatted concatenation of all of the names and |
| 58 | * their values. |
| 59 | * |
| 60 | * @return string - "<name1>=<value1>|<name2>=<value2>..etc" |
| 61 | */ |
| 62 | const std::string& get() const |
| 63 | { |
| 64 | return all; |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | /** |
| 69 | * Adds a name to the object |
| 70 | * |
| 71 | * @param name - the name to add |
| 72 | */ |
| 73 | void addName(const std::string& name) |
| 74 | { |
| 75 | all += name + '='; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Adds a value to the object |
| 80 | * |
| 81 | * @param value - the value to add |
| 82 | */ |
| 83 | void addValue(uint64_t value) |
| 84 | { |
| 85 | std::ostringstream stream; |
| 86 | stream << "0x" << std::hex << value; |
| 87 | all += stream.str(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Adds a divider to the summary string to |
| 92 | * break up the name/value pairs |
| 93 | */ |
| 94 | void addDivider() |
| 95 | { |
| 96 | if (!all.empty()) |
Matt Spinler | 3b090d1 | 2017-09-08 15:07:05 -0500 | [diff] [blame] | 97 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 98 | all += '|'; |
Matt Spinler | 3b090d1 | 2017-09-08 15:07:05 -0500 | [diff] [blame] | 99 | } |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 100 | } |
Matt Spinler | 3b090d1 | 2017-09-08 15:07:05 -0500 | [diff] [blame] | 101 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 102 | /** |
| 103 | * The string containing all name/value pairs |
| 104 | */ |
| 105 | std::string all; |
Matt Spinler | 3b090d1 | 2017-09-08 15:07:05 -0500 | [diff] [blame] | 106 | }; |
| 107 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 108 | } // namespace util |
| 109 | } // namespace power |
| 110 | } // namespace witherspoon |