拆解并重命名成独立的Node/NodeMangaer完成
This commit is contained in:
parent
502cb54a76
commit
248d3add5d
63
inc/hgl/type/Node.h
Normal file
63
inc/hgl/type/Node.h
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include<hgl/TypeFunc.h>
|
||||||
|
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
class NodeManager;
|
||||||
|
|
||||||
|
class Node
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
enum class LifePhase
|
||||||
|
{
|
||||||
|
None=0, ///<未知
|
||||||
|
Work, ///<工作中(一般的使用状态)
|
||||||
|
Recycled, ///<已回收(走过了回收流程,但并未释放资源)
|
||||||
|
WaitDestory, ///<等待销毁(已经进入真正的销毁流程)
|
||||||
|
Destory, ///<销毁(已经被销毁,不存这种对像,只在根据ID获取状态时可以得到)
|
||||||
|
|
||||||
|
ENUM_CLASS_RANGE(None,Destory)
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
NodeManager * manager; ///<管理器指针
|
||||||
|
size_t unique_id; ///<唯一ID
|
||||||
|
|
||||||
|
LifePhase life_phase; ///<生命周期状态
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
NodeManager * GetManager () {return manager;} ///<获取管理器指针
|
||||||
|
const NodeManager * GetManager ()const{return manager;} ///<获取管理器指针
|
||||||
|
|
||||||
|
const size_t GetManagerID ()const; ///<获取管理器ID
|
||||||
|
const size_t GetUniqueID ()const{return unique_id;} ///<获取对象唯一ID
|
||||||
|
|
||||||
|
const LifePhase GetLifePhase ()const{return life_phase;} ///<获取生命周期状态
|
||||||
|
|
||||||
|
const bool IsWork ()const{return life_phase==LifePhase::Work;} ///<可以工作
|
||||||
|
const bool IsRecycled ()const{return life_phase==LifePhase::Recycled;} ///<是否已经回收
|
||||||
|
const bool IsWaitDestory ()const{return life_phase==LifePhase::WaitDestory;} ///<是否已经进入等待销毁状态
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
friend class NodeManager;
|
||||||
|
|
||||||
|
virtual void MarkWaitDestory(){life_phase=LifePhase::WaitDestory;} ///<标记为等待销毁状态
|
||||||
|
|
||||||
|
public: //事件
|
||||||
|
|
||||||
|
virtual void OnDestory(){life_phase=LifePhase::Destory;} ///<销毁节点(标记为销毁状态,下步就delete了,所以要做什么这是最后的机会)
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
Node(NodeManager *nm,const size_t uid);
|
||||||
|
virtual ~Node();
|
||||||
|
|
||||||
|
virtual void Destory(); ///<销毁节点(标记为等待销毁状态)
|
||||||
|
|
||||||
|
};//class Node
|
||||||
|
}//namespcae hgl
|
64
inc/hgl/type/NodeManager.h
Normal file
64
inc/hgl/type/NodeManager.h
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include<hgl/type/Node.h>
|
||||||
|
#include<typeinfo>
|
||||||
|
#include<tsl/robin_map.h>
|
||||||
|
#include<tsl/robin_set.h>
|
||||||
|
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
class NodeManager
|
||||||
|
{
|
||||||
|
static const size_t AcquireManagerID(){static size_t id_count=0;return ++id_count;}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
size_t manager_serial;
|
||||||
|
size_t node_serial;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
tsl::robin_map<size_t,Node *> node_map; ///<节点集合
|
||||||
|
|
||||||
|
tsl::robin_set<Node *> wait_destory_node_set; ///<等待销毁的节点集合
|
||||||
|
|
||||||
|
tsl::robin_set<Node *> destored_node_set; ///<已经销毁的节点集合
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
friend class Node;
|
||||||
|
|
||||||
|
const size_t AcquireNodeID (){return ++node_serial;}
|
||||||
|
|
||||||
|
virtual Node * OnCreateNode (const size_t node_id)=0; ///<创建节点时调用
|
||||||
|
virtual void OnDestoryNode (Node *node)=0; ///<销毁节点时调用
|
||||||
|
|
||||||
|
void OnNodeDirectDestory (Node *node); ///<直接销毁,这种情况只在对象被直接delete的情况下,一般不需要
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
const size_t GetMangaerID ()const{return manager_serial;}
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
NodeManager()
|
||||||
|
{
|
||||||
|
manager_serial=AcquireManagerID();
|
||||||
|
node_serial=0;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~NodeManager()=default;
|
||||||
|
|
||||||
|
virtual void ForceClear();
|
||||||
|
|
||||||
|
Node * CreateNode();
|
||||||
|
|
||||||
|
const bool ContainsNode(Node *tn)const;
|
||||||
|
|
||||||
|
bool DestoryNode(Node *node);
|
||||||
|
|
||||||
|
Node * GetNode(const size_t node_id);
|
||||||
|
|
||||||
|
virtual void Update(){}
|
||||||
|
};//class NodeManager
|
||||||
|
}//namespace hgl
|
@ -1,80 +1,21 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include<hgl/TypeFunc.h>
|
#include<hgl/type/NodeManager.h>
|
||||||
#include<typeinfo>
|
|
||||||
#include<tsl/robin_map.h>
|
|
||||||
#include<tsl/robin_set.h>
|
|
||||||
|
|
||||||
namespace hgl
|
namespace hgl
|
||||||
{
|
{
|
||||||
class TreeBaseNodeManager;
|
class TreeBaseNode:public Node
|
||||||
|
|
||||||
template<typename T> class TreeNodeManager;
|
|
||||||
|
|
||||||
enum class TreeNodeLifePhase
|
|
||||||
{
|
|
||||||
None=0, ///<未知
|
|
||||||
Work, ///<工作中(一般的使用状态)
|
|
||||||
Recycled, ///<已回收(走过了回收流程,但并未释放资源)
|
|
||||||
WaitDestory, ///<等待销毁(已经进入真正的销毁流程)
|
|
||||||
Destory, ///<销毁(已经被销毁,不存这种对像,只在根据ID获取状态时可以得到)
|
|
||||||
|
|
||||||
ENUM_CLASS_RANGE(None,Destory)
|
|
||||||
};
|
|
||||||
|
|
||||||
class BaseNode
|
|
||||||
{
|
|
||||||
TreeBaseNodeManager *manager; ///<管理器指针
|
|
||||||
size_t unique_id; ///<唯一ID
|
|
||||||
|
|
||||||
TreeNodeLifePhase life_phase; ///<生命周期状态
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
TreeBaseNodeManager *GetManager() {return manager;} ///<获取管理器指针
|
|
||||||
const TreeBaseNodeManager *GetManager()const{return manager;} ///<获取管理器指针
|
|
||||||
|
|
||||||
const size_t GetManagerID ()const; ///<获取管理器ID
|
|
||||||
const size_t GetUniqueID ()const{return unique_id;} ///<获取对象唯一ID
|
|
||||||
|
|
||||||
const TreeNodeLifePhase GetLifePhase()const{return life_phase;} ///<获取生命周期状态
|
|
||||||
|
|
||||||
const bool IsWork ()const{return life_phase==TreeNodeLifePhase::Work;} ///<可以工作
|
|
||||||
const bool IsRecycled ()const{return life_phase==TreeNodeLifePhase::Recycled;} ///<是否已经回收
|
|
||||||
const bool IsWaitDestory()const{return life_phase==TreeNodeLifePhase::WaitDestory;} ///<是否已经进入等待销毁状态
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
BaseNode(TreeBaseNodeManager *nm,const size_t uid);
|
|
||||||
virtual ~BaseNode();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
|
|
||||||
friend class TreeBaseNodeManager;
|
|
||||||
template<typename T> friend class TreeNodeManager;
|
|
||||||
|
|
||||||
virtual void MarkWaitDestory(){life_phase=TreeNodeLifePhase::WaitDestory;} ///<标记为等待销毁状态
|
|
||||||
|
|
||||||
virtual void OnDestory(); ///<最后的真实销毁调用操作(默认为delete this)
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
virtual void Destory(); ///<销毁节点(标记为等待销毁状态)
|
|
||||||
|
|
||||||
};//class BaseNode
|
|
||||||
|
|
||||||
class TreeBaseNode:public BaseNode
|
|
||||||
{
|
{
|
||||||
TreeBaseNode *parent_node; ///<父节点指针
|
TreeBaseNode *parent_node; ///<父节点指针
|
||||||
tsl::robin_map<size_t,TreeBaseNode *> child_map; ///<子节点集合
|
tsl::robin_map<size_t,TreeBaseNode *> child_map; ///<子节点集合
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
friend class TreeBaseNodeManager;
|
friend class NodeManager;
|
||||||
template<typename T> friend class TreeNode;
|
template<typename T> friend class TreeNode;
|
||||||
template<typename T> friend class TreeNodeManager;
|
template<typename T> friend class TreeNodeManager;
|
||||||
|
|
||||||
TreeBaseNode(TreeBaseNodeManager *nm,const size_t uid);
|
TreeBaseNode(NodeManager *nm,const size_t uid);
|
||||||
virtual ~TreeBaseNode();
|
virtual ~TreeBaseNode();
|
||||||
|
|
||||||
public: //子节点相关
|
public: //子节点相关
|
||||||
@ -93,61 +34,6 @@ namespace hgl
|
|||||||
void DestoryAllChild();
|
void DestoryAllChild();
|
||||||
};//class TreeBaseNode
|
};//class TreeBaseNode
|
||||||
|
|
||||||
class TreeBaseNodeManager
|
|
||||||
{
|
|
||||||
static const size_t AcquireManagerID(){static size_t id_count=0;return ++id_count;}
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
size_t manager_serial;
|
|
||||||
size_t node_serial;
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
tsl::robin_map<size_t,BaseNode *> node_map; ///<节点集合
|
|
||||||
|
|
||||||
tsl::robin_set<BaseNode *> wait_destory_node_set; ///<等待销毁的节点集合
|
|
||||||
|
|
||||||
tsl::robin_set<BaseNode *> destored_node_set; ///<已经销毁的节点集合
|
|
||||||
|
|
||||||
protected:
|
|
||||||
|
|
||||||
friend class BaseNode;
|
|
||||||
|
|
||||||
const size_t AcquireNodeID(){return ++node_serial;}
|
|
||||||
|
|
||||||
virtual BaseNode * OnCreateNode(const size_t node_id)=0; ///<创建节点时调用
|
|
||||||
virtual void OnDestoryNode(BaseNode *node)=0; ///<销毁节点时调用
|
|
||||||
|
|
||||||
void OnNodeDirectDestory(BaseNode *node); ///<直接销毁,这种情况只在对象被直接delete的情况下,一般不需要
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
const size_t GetMangaerID()const{return manager_serial;}
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
TreeBaseNodeManager()
|
|
||||||
{
|
|
||||||
manager_serial=AcquireManagerID();
|
|
||||||
node_serial=0;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~TreeBaseNodeManager(){}
|
|
||||||
|
|
||||||
virtual void ForceClear();
|
|
||||||
|
|
||||||
BaseNode * CreateNode();
|
|
||||||
|
|
||||||
const bool ContainsNode(BaseNode *tn)const;
|
|
||||||
|
|
||||||
bool DestoryNode(BaseNode *node);
|
|
||||||
|
|
||||||
BaseNode * GetNode(const size_t node_id);
|
|
||||||
|
|
||||||
virtual void Update(){}
|
|
||||||
};//class TreeBaseNodeManager
|
|
||||||
|
|
||||||
template<typename T> class TreeNode:public TreeBaseNode
|
template<typename T> class TreeNode:public TreeBaseNode
|
||||||
{
|
{
|
||||||
T node_data;
|
T node_data;
|
||||||
@ -183,7 +69,7 @@ namespace hgl
|
|||||||
}
|
}
|
||||||
};//class TreeNode
|
};//class TreeNode
|
||||||
|
|
||||||
template<typename T> class TreeNodeManager:public TreeBaseNodeManager
|
template<typename T> class TreeNodeManager:public NodeManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -191,21 +77,22 @@ namespace hgl
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
BaseNode *OnCreateNode(const size_t node_id) override
|
Node *OnCreateNode(const size_t node_id) override
|
||||||
{
|
{
|
||||||
return(new NodeType(this,node_id));
|
return(new NodeType(this,node_id));
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnDestoryNode(BaseNode *node)override
|
void OnDestoryNode(Node *node)override
|
||||||
{
|
{
|
||||||
if(!node)return;
|
if(!node)return;
|
||||||
|
|
||||||
node->OnDestory();
|
node->OnDestory();
|
||||||
|
delete node;
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
using TreeBaseNodeManager::TreeBaseNodeManager;
|
using NodeManager::NodeManager;
|
||||||
virtual ~TreeNodeManager()
|
virtual ~TreeNodeManager()
|
||||||
{
|
{
|
||||||
ForceClear();
|
ForceClear();
|
||||||
|
@ -42,11 +42,14 @@ SET(TYPE_DATA_CHAIN_SOURCE ${TYPE_INCLUDE_PATH}/DataChain.h
|
|||||||
|
|
||||||
SOURCE_GROUP("DataType\\DataChain" FILES ${TYPE_DATA_CHAIN_SOURCE})
|
SOURCE_GROUP("DataType\\DataChain" FILES ${TYPE_DATA_CHAIN_SOURCE})
|
||||||
|
|
||||||
SET(TREE_NODE_FILES ${TYPE_INCLUDE_PATH}/TreeNode.h
|
SET(TREE_NODE_FILES ${TYPE_INCLUDE_PATH}/Node.h
|
||||||
Type/TreeNode.cpp
|
${TYPE_INCLUDE_PATH}/NodeManager.h
|
||||||
Type/TreeNodeManager.cpp)
|
${TYPE_INCLUDE_PATH}/TreeNode.h
|
||||||
|
Type/Node.cpp
|
||||||
|
Type/NodeManager.cpp
|
||||||
|
Type/TreeNode.cpp)
|
||||||
|
|
||||||
SOURCE_GROUP("DataType\\Tree" FILES ${TREE_NODE_FILES})
|
SOURCE_GROUP("DataType\\Node" FILES ${TREE_NODE_FILES})
|
||||||
|
|
||||||
SET(ACTIVE_MANAGER_FILES ${TYPE_INCLUDE_PATH}/ActiveIDManager.h
|
SET(ACTIVE_MANAGER_FILES ${TYPE_INCLUDE_PATH}/ActiveIDManager.h
|
||||||
${TYPE_INCLUDE_PATH}/ActiveMemoryBlockManager.h
|
${TYPE_INCLUDE_PATH}/ActiveMemoryBlockManager.h
|
||||||
|
28
src/Type/Node.cpp
Normal file
28
src/Type/Node.cpp
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#include<hgl/type/NodeManager.h>
|
||||||
|
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
Node::Node(NodeManager *nm,const size_t uid)
|
||||||
|
{
|
||||||
|
manager=nm;
|
||||||
|
unique_id=uid;
|
||||||
|
|
||||||
|
life_phase=LifePhase::None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node::~Node()
|
||||||
|
{
|
||||||
|
if(life_phase<LifePhase::WaitDestory) //还没有进入销毁器
|
||||||
|
manager->OnNodeDirectDestory(this); //直接销毁
|
||||||
|
}
|
||||||
|
|
||||||
|
const size_t Node::GetManagerID()const
|
||||||
|
{
|
||||||
|
return manager->GetMangaerID();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Node::Destory()
|
||||||
|
{
|
||||||
|
manager->DestoryNode(this);
|
||||||
|
}
|
||||||
|
}//namespace hgl
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace hgl
|
namespace hgl
|
||||||
{
|
{
|
||||||
void TreeBaseNodeManager::OnNodeDirectDestory(BaseNode *node) ///<直接销毁,这种情况只在对象被直接delete的情况下,一般不需要
|
void NodeManager::OnNodeDirectDestory(Node *node) ///<直接销毁,这种情况只在对象被直接delete的情况下,一般不需要
|
||||||
{
|
{
|
||||||
if(!node)return;
|
if(!node)return;
|
||||||
|
|
||||||
@ -16,7 +16,7 @@ namespace hgl
|
|||||||
node_map.erase(node->GetUniqueID());
|
node_map.erase(node->GetUniqueID());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TreeBaseNodeManager::ForceClear()
|
void NodeManager::ForceClear()
|
||||||
{
|
{
|
||||||
if(!node_map.empty())
|
if(!node_map.empty())
|
||||||
{
|
{
|
||||||
@ -41,11 +41,11 @@ namespace hgl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseNode *TreeBaseNodeManager::CreateNode()
|
Node *NodeManager::CreateNode()
|
||||||
{
|
{
|
||||||
const size_t node_id=AcquireNodeID();
|
const size_t node_id=AcquireNodeID();
|
||||||
|
|
||||||
BaseNode *node=OnCreateNode(node_id);
|
Node *node=OnCreateNode(node_id);
|
||||||
|
|
||||||
if(!node)
|
if(!node)
|
||||||
return(nullptr);
|
return(nullptr);
|
||||||
@ -55,7 +55,7 @@ namespace hgl
|
|||||||
return(node);
|
return(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool TreeBaseNodeManager::ContainsNode(BaseNode *tn)const
|
const bool NodeManager::ContainsNode(Node *tn)const
|
||||||
{
|
{
|
||||||
if(!tn)return(false);
|
if(!tn)return(false);
|
||||||
if(tn->GetManagerID()!=GetMangaerID())return(false);
|
if(tn->GetManagerID()!=GetMangaerID())return(false);
|
||||||
@ -66,7 +66,7 @@ namespace hgl
|
|||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TreeBaseNodeManager::DestoryNode(BaseNode *node)
|
bool NodeManager::DestoryNode(Node *node)
|
||||||
{
|
{
|
||||||
if(!node)return(false);
|
if(!node)return(false);
|
||||||
|
|
||||||
@ -83,7 +83,7 @@ namespace hgl
|
|||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseNode *TreeBaseNodeManager::GetNode(const size_t node_id)
|
Node *NodeManager::GetNode(const size_t node_id)
|
||||||
{
|
{
|
||||||
auto iter=node_map.find(node_id);
|
auto iter=node_map.find(node_id);
|
||||||
|
|
@ -2,40 +2,7 @@
|
|||||||
|
|
||||||
namespace hgl
|
namespace hgl
|
||||||
{
|
{
|
||||||
BaseNode::BaseNode(TreeBaseNodeManager *nm,const size_t uid)
|
TreeBaseNode::TreeBaseNode(NodeManager *nm,const size_t uid):Node(nm,uid)
|
||||||
{
|
|
||||||
manager=nm;
|
|
||||||
unique_id=uid;
|
|
||||||
|
|
||||||
life_phase=TreeNodeLifePhase::None;
|
|
||||||
}
|
|
||||||
|
|
||||||
BaseNode::~BaseNode()
|
|
||||||
{
|
|
||||||
if(life_phase<TreeNodeLifePhase::WaitDestory) //还没有进入销毁器
|
|
||||||
manager->OnNodeDirectDestory(this); //直接销毁
|
|
||||||
}
|
|
||||||
|
|
||||||
const size_t BaseNode::GetManagerID()const
|
|
||||||
{
|
|
||||||
return manager->GetMangaerID();
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseNode::Destory()
|
|
||||||
{
|
|
||||||
manager->DestoryNode(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseNode::OnDestory()
|
|
||||||
{
|
|
||||||
life_phase=TreeNodeLifePhase::Destory; //设置为销毁状态
|
|
||||||
delete this;
|
|
||||||
}
|
|
||||||
}//namespace hgl
|
|
||||||
|
|
||||||
namespace hgl
|
|
||||||
{
|
|
||||||
TreeBaseNode::TreeBaseNode(TreeBaseNodeManager *nm,const size_t uid):BaseNode(nm,uid)
|
|
||||||
{
|
{
|
||||||
parent_node=nullptr;
|
parent_node=nullptr;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user