CMExamples/datatype/TreeTest.cpp

87 lines
1.9 KiB
C++
Raw Normal View History

#include<iostream>
#include<string>
2025-04-27 01:40:52 +08:00
#include<hgl/type/TreeNode.h>
using namespace hgl;
class Folder
{
std::string path;
public:
Folder()
{
path.clear();
}
~Folder()
{
std::cout<<"~Folder(\""<<path<<"\")"<<std::endl;
}
void SetPath(const std::string &p)
{
path=p;
std::cout<<"Folder:\""<<path<<"\""<<std::endl;
}
const std::string &GetPath()const
{
return path;
}
};
class FolderNode:public TreeNode<Folder>
{
public:
using TreeNode<Folder>::TreeNode;
2025-05-04 19:29:00 +08:00
~FolderNode()=default;
2025-05-04 19:29:00 +08:00
void OnAttachParent(TreeNode *node) override
{
2025-05-04 19:29:00 +08:00
std::cout<<(*node)->GetPath()<<"\\"<<(*this)->GetPath()<<std::endl;
}
2025-05-04 19:29:00 +08:00
void OnDetachParent() override
{
2025-05-04 19:29:00 +08:00
TreeNode *node=GetParent();
2025-05-04 19:29:00 +08:00
std::cout<<"remove "<<(*this)->GetPath()<<" from "<<(*node)->GetPath()<<std::endl;
}
};
int main(int,char **)
{
2025-05-04 19:29:00 +08:00
using FolderManager=DataNodeManager<FolderNode>;
FolderManager manager;
2025-05-04 19:29:00 +08:00
FolderNode *root=manager.Create();
(*root)->SetPath("root");
{
auto *win=manager.Create();
(*win)->SetPath("win");
root->AttachChild(win);
}
{
auto *linux=manager.Create();
(*linux)->SetPath("linux");
root->AttachChild(linux);
2025-04-29 22:04:30 +08:00
2025-05-04 19:29:00 +08:00
delete linux; //手动释放,这里不会产生FolderNode::OnDetachParent是正常现像。
//因为在完成FolderNode::~FolderNode后虚拟函数的关系已回退到上一级所以这一级的已经无效所以无法产生调用。
//如果要实现调用必须在FolderNode::~FolderNode调用DetachAll()
//这个问题我们可能需要调研如要完全解决可能需要屏蔽delete操作符的使用只允许使用Destory
}
//自动释放到可以了,但是释放顺序需要处理下
2025-04-27 01:40:52 +08:00
}