duilib学习记录之节点

来源:互联网 发布:linux 本地socket通信 编辑:程序博客网 时间:2024/03/28 22:38
//CNode.h #ifndef CNODE_H #define CNODE_H #include <vector> class Node {     struct NodeData//节点数据     {         int _level;//等级         bool _child_visible;//子节点是否显示         CStdString _text;//节点文本         CListLabelElementUI* _pListElement;//节点标签控件     };     typedef std::vector Children;     Children    _children;//存储子类节点容器(父类存储子节点,子节点指向父类节点,双向链表)     Node*   _parent;//存储父类节点     NodeData _data; private:     void set_parent(Node* parent) { _parent = parent; } public:     Node() : _parent (NULL) {}     explicit Node(NodeData t) : _data (t), _parent (NULL) {}     Node(NodeData t, Node* parent)  : _data (t), _parent (parent) {}     ~Node();     NodeData& data() { return _data; }      int num_children() const { return _children.size(); }     Node* child(int i)  { return _children[i]; }     Node* parent() { return ( _parent); }     bool has_children() const { return num_children() > 0; }     void add_child(Node* child) ;     void remove_child(Node* child);     Node* get_last_child();//递归遍历节点 }; #endif//CNode.cpp #include "stdafx.h" #include "CNode.h" Node::~Node() {     for (int i = 0; i < num_children(); i++)         delete _children[i]; } void Node::add_child(Node* child) {     if(!child)     { return;     }     child->set_parent(this);     _children.push_back(child); } void Node::remove_child(Node* child) {     Children::iterator iter = _children.begin();     for( ; iter < _children.end(); ++iter )     {         if( iter == child )         {             _children.erase(iter);             return;         }     } } Node Node::get_last_child() {     if( has_children() )     {         return child(num_children() - 1)->get_last_child();    }     else return this; }
0 0