Line data Source code
1 : /**
2 : * @file tree_node.h
3 : * @brief Qt-independent metadata tree nodes.
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 : #ifndef TREE_NODE_H
13 : #define TREE_NODE_H
14 :
15 : #include <cstddef>
16 : #include <memory>
17 : #include <optional>
18 : #include <string>
19 : #include <vector>
20 :
21 : #include "definition_key.h"
22 : #include "frame_info.h"
23 : #include "signal_info.h"
24 :
25 : /**
26 : * @brief Type of a tree node.
27 : */
28 : enum class NodeType {
29 : Root,
30 : Frame,
31 : Signal
32 : };
33 :
34 : /**
35 : * @brief Tree node for later use in model/view or other hierarchy consumers.
36 : */
37 : class TreeNode {
38 : public:
39 : /**
40 : * @brief Create root node.
41 : */
42 : TreeNode();
43 :
44 : /**
45 : * @brief Create frame node.
46 : * @param frame Frame payload.
47 : */
48 : explicit TreeNode(const FrameInfo &frame);
49 :
50 : /**
51 : * @brief Create signal node.
52 : * @param signal Signal payload.
53 : */
54 : explicit TreeNode(const SignalInfo &signal);
55 :
56 229 : ~TreeNode() = default;
57 :
58 : TreeNode(const TreeNode &) = delete;
59 : TreeNode &operator=(const TreeNode &) = delete;
60 :
61 : // Node addresses are stable. Transfer unique_ptr ownership instead of nodes.
62 : TreeNode(TreeNode &&) = delete;
63 : TreeNode &operator=(TreeNode &&) = delete;
64 :
65 : /**
66 : * @brief Add child node.
67 : * @param child Child node to add.
68 : * A null child is ignored. Existing children keep their addresses and rows.
69 : * Ownership must be exclusive; attached nodes cannot be reparented.
70 : * @throws std::invalid_argument for self/ancestor insertion or an attached child.
71 : * Rejected ownership remains with the caller.
72 : */
73 : void AddChild(std::unique_ptr<TreeNode> &&child);
74 :
75 : /** Non-owning parent; nullptr for a detached node or the root. */
76 : const TreeNode *GetParent() const;
77 :
78 : /** Zero-based sibling position; zero for a detached node or the root. */
79 : std::size_t GetRow() const;
80 :
81 : /** Frame identity for frame nodes and signals attached to a frame; otherwise empty. */
82 : std::optional<FrameKey> GetFrameKey() const;
83 :
84 : /** Signal identity (owning frame key and name); empty for other/detached nodes. */
85 : std::optional<SignalKey> GetSignalKey() const;
86 :
87 : /**
88 : * @brief Get child count.
89 : * @return Number of children.
90 : */
91 : std::size_t GetChildCount() const;
92 :
93 : /**
94 : * @brief Get child by index.
95 : * @param index Child index.
96 : * @return Child pointer or nullptr if index is invalid.
97 : */
98 : const TreeNode *GetChild(std::size_t index) const;
99 :
100 : /**
101 : * @brief Get mutable child by index.
102 : * @param index Child index.
103 : * @return Child pointer or nullptr if index is invalid.
104 : */
105 : TreeNode *GetChild(std::size_t index);
106 :
107 : /**
108 : * @brief Get node type.
109 : * @return Node type.
110 : */
111 : NodeType GetType() const;
112 :
113 : /**
114 : * @brief Get display name.
115 : * @return Node name.
116 : */
117 : const std::string &GetName() const;
118 :
119 : /**
120 : * @brief Get frame payload if node is frame.
121 : * @return Pointer to frame info or nullptr.
122 : */
123 : const FrameInfo *GetFrame() const;
124 :
125 : /**
126 : * @brief Get signal payload if node is signal.
127 : * @return Pointer to signal info or nullptr.
128 : */
129 : const SignalInfo *GetSignal() const;
130 :
131 : private:
132 : TreeNode *m_parent = nullptr;
133 : std::size_t m_row = 0U;
134 : NodeType m_type;
135 : std::string m_name;
136 : std::vector<std::unique_ptr<TreeNode>> m_children;
137 : std::unique_ptr<FrameInfo> m_frame;
138 : std::unique_ptr<SignalInfo> m_signal;
139 : };
140 :
141 : #endif /* TREE_NODE_H */
|