DBC Framework
DBC parsing and CAN signal decoding framework
Loading...
Searching...
No Matches
tree_node.cpp
Go to the documentation of this file.
1
12#include "tree_node.h"
13#include <stdexcept>
14
16 : m_type(NodeType::Root), m_name("dbc") {
17}
18
20 : m_type(NodeType::Frame), m_name(frame.name), m_frame(std::make_unique<FrameInfo>(frame)) {
21}
22
24 : m_type(NodeType::Signal), m_name(signal.name), m_signal(std::make_unique<SignalInfo>(signal)) {
25}
26
27void TreeNode::AddChild(std::unique_ptr<TreeNode> &&child) {
28 if (child) {
29 for (const TreeNode *ancestor = this; ancestor; ancestor = ancestor->m_parent) {
30 if (ancestor == child.get())
31 throw std::invalid_argument("Cannot insert a node into its own subtree");
32 }
33 if (child->m_parent)
34 throw std::invalid_argument("Child is already attached");
35 m_children.push_back(std::move(child));
36 m_children.back()->m_parent = this;
37 m_children.back()->m_row = m_children.size() - 1U;
38 }
39}
40
41std::size_t TreeNode::GetChildCount() const {
42 return m_children.size();
43}
44
45const TreeNode *TreeNode::GetChild(std::size_t index) const {
46 if (index >= m_children.size())
47 return nullptr;
48
49 return m_children[index].get();
50}
51
52TreeNode *TreeNode::GetChild(std::size_t index) {
53 if (index >= m_children.size())
54 return nullptr;
55
56 return m_children[index].get();
57}
58
60 return m_type;
61}
62
63const std::string &TreeNode::GetName() const {
64 return m_name;
65}
66
68 return m_frame.get();
69}
70
72 return m_signal.get();
73}
74
76 return m_parent;
77}
78
79std::size_t TreeNode::GetRow() const {
80 return m_row;
81}
82
83std::optional<FrameKey> TreeNode::GetFrameKey() const {
84 if (m_frame)
85 return FrameKey(m_frame->canId, m_frame->isExtended);
86 if (m_signal && m_parent && m_parent->GetType() == NodeType::Frame)
87 return m_parent->GetFrameKey();
88 return std::nullopt;
89}
90
91std::optional<SignalKey> TreeNode::GetSignalKey() const {
92 const std::optional<FrameKey> frame = GetFrameKey();
93 if (m_signal && frame)
94 return SignalKey(*frame, m_signal->name);
95 return std::nullopt;
96}
Tree node for later use in model/view or other hierarchy consumers.
Definition tree_node.h:37
std::optional< SignalKey > GetSignalKey() const
Definition tree_node.cpp:91
const SignalInfo * GetSignal() const
Get signal payload if node is signal.
Definition tree_node.cpp:71
const TreeNode * GetChild(std::size_t index) const
Get child by index.
Definition tree_node.cpp:45
TreeNode()
Create root node.
Definition tree_node.cpp:15
const TreeNode * GetParent() const
Definition tree_node.cpp:75
const std::string & GetName() const
Get display name.
Definition tree_node.cpp:63
std::size_t GetChildCount() const
Get child count.
Definition tree_node.cpp:41
std::size_t GetRow() const
Definition tree_node.cpp:79
std::optional< FrameKey > GetFrameKey() const
Definition tree_node.cpp:83
void AddChild(std::unique_ptr< TreeNode > &&child)
Add child node.
Definition tree_node.cpp:27
NodeType GetType() const
Get node type.
Definition tree_node.cpp:59
const FrameInfo * GetFrame() const
Get frame payload if node is frame.
Definition tree_node.cpp:67
Describes one CAN frame from a DBC file.
Definition frame_info.h:25
Stable frame identifier: normalized CAN ID and frame type.
Describes one signal inside a DBC frame.
Definition signal_info.h:25
Qt-independent metadata tree nodes.
NodeType
Type of a tree node.
Definition tree_node.h:28