Closes #22
This commit is contained in:
1 parent
a202ba12ba
commit
713276d34a
5 files changed
+120
-11
No files matched your search
+23
-1
@@ -10,6 +10,7 @@
|
||||
*/
|
||||
|
||||
#include "tree_node.h"
|
||||
#include <stdexcept>
|
||||
|
||||
TreeNode::TreeNode()
|
||||
: m_type(NodeType::Root), m_name("dbc") {
|
||||
@@ -23,8 +24,14 @@ TreeNode::TreeNode(const SignalInfo &signal)
|
||||
: m_type(NodeType::Signal), m_name(signal.name), m_signal(std::make_unique<SignalInfo>(signal)) {
|
||||
}
|
||||
|
||||
void TreeNode::AddChild(std::unique_ptr<TreeNode> child) {
|
||||
void TreeNode::AddChild(std::unique_ptr<TreeNode> &&child) {
|
||||
if (child) {
|
||||
for (const TreeNode *ancestor = this; ancestor; ancestor = ancestor->m_parent) {
|
||||
if (ancestor == child.get())
|
||||
throw std::invalid_argument("Cannot insert a node into its own subtree");
|
||||
}
|
||||
if (child->m_parent)
|
||||
throw std::invalid_argument("Child is already attached");
|
||||
m_children.push_back(std::move(child));
|
||||
m_children.back()->m_parent = this;
|
||||
m_children.back()->m_row = m_children.size() - 1U;
|
||||
@@ -72,3 +79,18 @@ const TreeNode *TreeNode::GetParent() const {
|
||||
std::size_t TreeNode::GetRow() const {
|
||||
return m_row;
|
||||
}
|
||||
|
||||
std::optional<FrameKey> TreeNode::GetFrameKey() const {
|
||||
if (m_frame)
|
||||
return FrameKey(m_frame->canId, m_frame->isExtended);
|
||||
if (m_signal && m_parent && m_parent->GetType() == NodeType::Frame)
|
||||
return m_parent->GetFrameKey();
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<SignalKey> TreeNode::GetSignalKey() const {
|
||||
const std::optional<FrameKey> frame = GetFrameKey();
|
||||
if (m_signal && frame)
|
||||
return SignalKey(*frame, m_signal->name);
|
||||
return std::nullopt;
|
||||
}
|
||||
+14
-1
@@ -14,9 +14,11 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "definition_key.h"
|
||||
#include "frame_info.h"
|
||||
#include "signal_info.h"
|
||||
|
||||
@@ -56,14 +58,19 @@ public:
|
||||
TreeNode(const TreeNode &) = delete;
|
||||
TreeNode &operator=(const TreeNode &) = delete;
|
||||
|
||||
// Node addresses are stable. Transfer unique_ptr ownership instead of nodes.
|
||||
TreeNode(TreeNode &&) = delete;
|
||||
TreeNode &operator=(TreeNode &&) = delete;
|
||||
|
||||
/**
|
||||
* @brief Add child node.
|
||||
* @param child Child node to add.
|
||||
* A null child is ignored. Existing children keep their addresses and rows.
|
||||
* Ownership must be exclusive; attached nodes cannot be reparented.
|
||||
* @throws std::invalid_argument for self/ancestor insertion or an attached child.
|
||||
* Rejected ownership remains with the caller.
|
||||
*/
|
||||
void AddChild(std::unique_ptr<TreeNode> child);
|
||||
void AddChild(std::unique_ptr<TreeNode> &&child);
|
||||
|
||||
/** Non-owning parent; nullptr for a detached node or the root. */
|
||||
const TreeNode *GetParent() const;
|
||||
@@ -71,6 +78,12 @@ public:
|
||||
/** Zero-based sibling position; zero for a detached node or the root. */
|
||||
std::size_t GetRow() const;
|
||||
|
||||
/** Frame identity for frame nodes and signals attached to a frame; otherwise empty. */
|
||||
std::optional<FrameKey> GetFrameKey() const;
|
||||
|
||||
/** Signal identity (owning frame key and name); empty for other/detached nodes. */
|
||||
std::optional<SignalKey> GetSignalKey() const;
|
||||
|
||||
/**
|
||||
* @brief Get child count.
|
||||
* @return Number of children.
|
||||
|
||||
Reference in new issue
Block a user