Files
dbc/core/tree_node.h
T
2026-10-20 13:38:21 -04:00

142 lines
3.5 KiB
C++

/**
* @file tree_node.h
* @brief Qt-independent metadata tree nodes.
*
* Created: 2026-03-13
* Author: Deeaitch (Dim. Himro)
*
* Licensed under the MIT License.
* See LICENSE file in the project root for full license text.
*/
#ifndef TREE_NODE_H
#define TREE_NODE_H
#include <cstddef>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "definition_key.h"
#include "frame_info.h"
#include "signal_info.h"
/**
* @brief Type of a tree node.
*/
enum class NodeType {
Root,
Frame,
Signal
};
/**
* @brief Tree node for later use in model/view or other hierarchy consumers.
*/
class TreeNode {
public:
/**
* @brief Create root node.
*/
TreeNode();
/**
* @brief Create frame node.
* @param frame Frame payload.
*/
explicit TreeNode(const FrameInfo &frame);
/**
* @brief Create signal node.
* @param signal Signal payload.
*/
explicit TreeNode(const SignalInfo &signal);
~TreeNode() = default;
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);
/** Non-owning parent; nullptr for a detached node or the root. */
const TreeNode *GetParent() const;
/** 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.
*/
std::size_t GetChildCount() const;
/**
* @brief Get child by index.
* @param index Child index.
* @return Child pointer or nullptr if index is invalid.
*/
const TreeNode *GetChild(std::size_t index) const;
/**
* @brief Get mutable child by index.
* @param index Child index.
* @return Child pointer or nullptr if index is invalid.
*/
TreeNode *GetChild(std::size_t index);
/**
* @brief Get node type.
* @return Node type.
*/
NodeType GetType() const;
/**
* @brief Get display name.
* @return Node name.
*/
const std::string &GetName() const;
/**
* @brief Get frame payload if node is frame.
* @return Pointer to frame info or nullptr.
*/
const FrameInfo *GetFrame() const;
/**
* @brief Get signal payload if node is signal.
* @return Pointer to signal info or nullptr.
*/
const SignalInfo *GetSignal() const;
private:
TreeNode *m_parent = nullptr;
std::size_t m_row = 0U;
NodeType m_type;
std::string m_name;
std::vector<std::unique_ptr<TreeNode>> m_children;
std::unique_ptr<FrameInfo> m_frame;
std::unique_ptr<SignalInfo> m_signal;
};
#endif /* TREE_NODE_H */