Branch data Line data Source code
1 : : // SPDX-License-Identifier: MIT
2 : : #ifndef VALUE_DESCRIPTIONS_H
3 : : #define VALUE_DESCRIPTIONS_H
4 : :
5 : : #include <cstdint>
6 : : #include <map>
7 : : #include <string>
8 : : #include <variant>
9 : :
10 : : using RawSignalValue = std::variant<std::int64_t, std::uint64_t>;
11 : :
12 : : /** Canonical keys: negative int64_t, nonnegative uint64_t. */
13 : 87 : inline RawSignalValue NormalizeValueKey(const RawSignalValue &value) {
14 [ + + ]: 87 : if (const std::int64_t *number = std::get_if<std::int64_t>(&value)) {
15 [ + + ]: 31 : if (*number >= 0)
16 : 11 : return static_cast<std::uint64_t>(*number);
17 : : }
18 : 76 : return value;
19 : : }
20 : :
21 : : using ValueDescriptions = std::map<RawSignalValue, std::string>;
22 : :
23 : : /** Returns a borrowed string, or nullptr; never converts integers to double. */
24 : 71 : inline const std::string *FindValueDescription(const ValueDescriptions &descriptions,
25 : : const RawSignalValue &rawValue) {
26 [ + - ]: 71 : const ValueDescriptions::const_iterator found = descriptions.find(NormalizeValueKey(rawValue));
27 [ + + ]: 71 : return found == descriptions.end() ? nullptr : &found->second;
28 : : }
29 : :
30 : : #endif
|