Branch data Line data Source code
1 : : /**
2 : : * @file tree_node.cpp
3 : : * @brief Tree node ownership and navigation.
4 : : *
5 : : * Created: 2026-03-13
6 : : * Author: Deeaitch (Dim. Himro)
7 : : *
8 : : * Licensed under the MIT License.
9 : : * See LICENSE file in the project root for full license text.
10 : : */
11 : :
12 : : #include "tree_node.h"
13 : : #include <stdexcept>
14 : :
15 : 22 : TreeNode::TreeNode()
16 [ + - ]: 66 : : m_type(NodeType::Root), m_name("dbc") {
17 : 22 : }
18 : :
19 : 97 : TreeNode::TreeNode(const FrameInfo &frame)
20 [ + - ]: 97 : : m_type(NodeType::Frame), m_name(frame.name), m_frame(std::make_unique<FrameInfo>(frame)) {
21 : 97 : }
22 : :
23 : 110 : TreeNode::TreeNode(const SignalInfo &signal)
24 [ + - ]: 110 : : m_type(NodeType::Signal), m_name(signal.name), m_signal(std::make_unique<SignalInfo>(signal)) {
25 : 110 : }
26 : :
27 : 206 : void TreeNode::AddChild(std::unique_ptr<TreeNode> &&child) {
28 [ + + ]: 206 : if (child) {
29 [ + + ]: 474 : for (const TreeNode *ancestor = this; ancestor; ancestor = ancestor->m_parent) {
30 [ + + ]: 271 : if (ancestor == child.get())
31 [ + - ]: 2 : throw std::invalid_argument("Cannot insert a node into its own subtree");
32 : : }
33 [ - + ]: 203 : if (child->m_parent)
34 [ # # ]: 0 : throw std::invalid_argument("Child is already attached");
35 : 203 : m_children.push_back(std::move(child));
36 : 203 : m_children.back()->m_parent = this;
37 : 203 : m_children.back()->m_row = m_children.size() - 1U;
38 : : }
39 : 204 : }
40 : :
41 : 2120 : std::size_t TreeNode::GetChildCount() const {
42 : 2120 : return m_children.size();
43 : : }
44 : :
45 : 3430 : const TreeNode *TreeNode::GetChild(std::size_t index) const {
46 [ + + ]: 3430 : if (index >= m_children.size())
47 : 1107 : return nullptr;
48 : :
49 : 2323 : return m_children[index].get();
50 : : }
51 : :
52 : 91 : TreeNode *TreeNode::GetChild(std::size_t index) {
53 [ + + ]: 91 : if (index >= m_children.size())
54 : 2 : return nullptr;
55 : :
56 : 89 : return m_children[index].get();
57 : : }
58 : :
59 : 44 : NodeType TreeNode::GetType() const {
60 : 44 : return m_type;
61 : : }
62 : :
63 : 29 : const std::string &TreeNode::GetName() const {
64 : 29 : return m_name;
65 : : }
66 : :
67 : 274 : const FrameInfo *TreeNode::GetFrame() const {
68 : 274 : return m_frame.get();
69 : : }
70 : :
71 : 281 : const SignalInfo *TreeNode::GetSignal() const {
72 : 281 : return m_signal.get();
73 : : }
74 : :
75 : 1875 : const TreeNode *TreeNode::GetParent() const {
76 : 1875 : return m_parent;
77 : : }
78 : :
79 : 1547 : std::size_t TreeNode::GetRow() const {
80 : 1547 : return m_row;
81 : : }
82 : :
83 : 23 : std::optional<FrameKey> TreeNode::GetFrameKey() const {
84 [ + + ]: 23 : if (m_frame)
85 : 13 : return FrameKey(m_frame->canId, m_frame->isExtended);
86 [ + + + + : 10 : if (m_signal && m_parent && m_parent->GetType() == NodeType::Frame)
+ - + + ]
87 : 6 : return m_parent->GetFrameKey();
88 : 4 : return std::nullopt;
89 : : }
90 : :
91 : 9 : std::optional<SignalKey> TreeNode::GetSignalKey() const {
92 [ + - ]: 9 : const std::optional<FrameKey> frame = GetFrameKey();
93 [ + + + + : 9 : if (m_signal && frame)
+ + ]
94 [ + - ]: 5 : return SignalKey(*frame, m_signal->name);
95 : 4 : return std::nullopt;
96 : : }
|