31 lines
1.0 KiB
C++
31 lines
1.0 KiB
C++
// SPDX-License-Identifier: MIT
|
|
#ifndef VALUE_DESCRIPTIONS_H
|
|
#define VALUE_DESCRIPTIONS_H
|
|
|
|
#include <cstdint>
|
|
#include <map>
|
|
#include <string>
|
|
#include <variant>
|
|
|
|
using RawSignalValue = std::variant<std::int64_t, std::uint64_t>;
|
|
|
|
/** Canonical keys: negative int64_t, nonnegative uint64_t. */
|
|
inline RawSignalValue NormalizeValueKey(const RawSignalValue &value) {
|
|
if (const std::int64_t *number = std::get_if<std::int64_t>(&value)) {
|
|
if (*number >= 0)
|
|
return static_cast<std::uint64_t>(*number);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
using ValueDescriptions = std::map<RawSignalValue, std::string>;
|
|
|
|
/** Returns a borrowed string, or nullptr; never converts integers to double. */
|
|
inline const std::string *FindValueDescription(const ValueDescriptions &descriptions,
|
|
const RawSignalValue &rawValue) {
|
|
const ValueDescriptions::const_iterator found = descriptions.find(NormalizeValueKey(rawValue));
|
|
return found == descriptions.end() ? nullptr : &found->second;
|
|
}
|
|
|
|
#endif
|