代理删除
[cpp]
/********************************************************************
* 文件名: Server.h
* 文件描述: 网络游戏模拟
* 创建人: 陈泽丹 ,2012年11月16日
* 版本号: 1.0
* 修改记录:
********************************************************************/
#pragma once
#include <set>
using namespace std;
class Delete_Proxy
{
private:
//代理项
struct IDeleteSink
{
virtual ~IDeleteSink(){}
virtual void delete_accessory() = 0;
};
//绑定附件
template<class _T>
struct Die:public IDeleteSink
{
Die(_T* _p_obj):mp_obj(_p_obj){}
void delete_accessory(){ delete mp_obj; }
_T* mp_obj;
};
map< const void*, IDeleteSink* > m_delete_objs;
//删除相关附件
void delete_accessorys()
{
for(map< const void*, IDeleteSink* >::iterator it = m_delete_objs.begin(); m_delete_objs.end() != it; ++it )
{
it->second->delete_accessory();
delete it->second;
}
m_delete_objs.clear();
}
public:
virtual ~Delete_Proxy(){ delete_accessorys(); }
//订阅"删除代理"
template<class _T>
void push(_T* _p_obj)
{
map< const void*, IDeleteSink* >::iterator it = m_delete_objs.find(_p_obj);
if( m_delete_objs.end() == it )
m_delete_objs[_p_obj] = new Die<_T>(_p_obj);
}
//取消"删除代理"
template<class _T>
void pop(const _T* _p_obj)
{
map< const void*, IDeleteSink* >::iterator it = m_delete_objs.find(_p_obj);
if( m_delete_objs.end() != it )
{
delete it->second;
m_delete_objs.erase(it);
}
}
};
补充:软件开发 , C++ ,