Files
dbc/core/definition_key.h
2026-10-11 06:30:18 -04:00

63 lines
1.5 KiB
C++

// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Dim Himro
#ifndef DEFINITION_KEY_H
#define DEFINITION_KEY_H
#include <cstdint>
#include <functional>
#include <string>
#include <utility>
/**
* @brief Stable frame identifier: normalized CAN ID and frame type.
*/
struct FrameKey {
std::uint32_t canId = 0U;
bool isExtended = false;
FrameKey() = default;
FrameKey(std::uint32_t id, bool extended)
: canId(id), isExtended(extended) {
}
bool operator==(const FrameKey &other) const {
return (canId == other.canId) && (isExtended == other.isExtended);
}
};
/**
* @brief Hasher for frame key.
*/
struct FrameKeyHasher {
std::size_t operator()(const FrameKey &key) const {
const std::size_t a = static_cast<std::size_t>(key.canId);
const std::size_t b = key.isExtended ? 1U : 0U;
return (a * 1315423911U) ^ b;
}
};
/** Stable signal identifier within one logical DBC database. */
struct SignalKey {
FrameKey frame;
std::string name;
SignalKey() = default;
SignalKey(FrameKey frameKey, std::string signalName)
: frame(frameKey), name(std::move(signalName)) {
}
bool operator==(const SignalKey &other) const {
return frame == other.frame && name == other.name;
}
};
struct SignalKeyHasher {
std::size_t operator()(const SignalKey &key) const {
return FrameKeyHasher()(key.frame) ^ (std::hash<std::string>()(key.name) << 1U);
}
};
#endif /* DEFINITION_KEY_H */