c++组合对象管理(一)

2014-11-23 22:30:48 · 作者: · 浏览: 17
有一些业务逻辑,需要管理多个同样类型的对象,并对外提供查询,删除等接口,在这些场合中,可以将被管理的对象称为Entity,管理Entity的类自然就叫做Entity_Manager,当以这样的方式组织层级对象时,很直观,而且项目的风格统一,每个人一旦熟悉了这种方式,理解别人写的Entity_Manager就很轻松。根据以往的项目经验,我自己实现了Entity和Entity_Manager类,代码如下:
#ifndef __ENTITY_H__  
#define __ENTITY_H__  
  
#include   
#include   
  
typedef unsigned int uint32;  
  
class Entity  
{  
protected:  
    Entity()  
    {  
        m_id = 0;  
    }  
  
public:  
    virtual ~Entity()  
    {  
    }  
  
    void id(uint32 _id)  
    {  
        m_id = _id;  
    }  
  
    uint32 id() const  
    {  
        return m_id;  
    }  
  
    void name(std::string _name)  
    {  
        m_name = _name;  
    }  
  
    std::string name() const  
    {  
        return m_name;  
    }  
      
private:  
    uint32 m_id;  
    std::string m_name;  
};  
  
//对所有子元素进行回调  
template  
class Entity_Exec  
{  
public:  
    Entity_Exec()  
    {  
    }  
  
    virtual ~Entity_Exec()  
    {  
    }  
  
    virtual bool exec(Concrete_Entity *entity) = 0;  
      
    virtual bool can_delete(Concrete_Entity *entity) const  
    {  
        return true;          
    }      
};  
  
class Guard_Ref  
{  
public:  
    Guard_Ref(bool &val) : m_val(val)  
    {  
        m_val = true;          
    }  
  
    ~Guard_Ref()  
    {  
        m_val = false;  
    }  
  
    bool &m_val;  
};  
  
template  
class Entity_Map  
{  
protected:  
    Entity_Map()  
    {  
        m_in_iteration = false;          
    }  
  
    ~Entity_Map()  
    {  
    }  
  
    //是否正在迭代中, 用于禁止在迭代map时进行删除迭代器等操作  
    bool in_iteration() const  
    {  
        return m_in_iteration;  
    }  
  
    bool add_entity(Key key, Entity *entity)  
    {  
        if(in_iteration())  
        {  
            return false;  
        }  
          
        if(m_entity_map.find(key) != m_entity_map.end())  
        {  
            return false;  
        }  
          
        m_entity_map[key] = entity;  
  
        return true;          
    }  
  
    Entity* get_entity(Key key) const  
    {  
        typename std::map::const_iterator iter = m_entity_map.find(key);  
  
        if(iter == m_entity_map.end())  
        {  
            return NULL;  
        }  
  
        return iter->second;  
    }  
  
    uint32 size() const  
    {  
        return m_entity_map.size();  
    }  
  
    bool empty() const  
    {  
        return m_entity_map.empty();  
    }  
  
    void clear()  
    {  
        if(!in_iteration())  
        {  
            m_entity_map.clear();  
        }          
    }  
  
    template
void exec_all(Entity_Exec &cb) { Guard_Ref guard(m_in_iteration); for(typename std::map::iterator iter = m_entity_map.begin(); iter != m_entity_map.end(); ++iter) { cb.exec(static_cast(iter->second)); } } template bool exec_until(Entity_Exec &cb) { Guard_Ref guard(m_in_iteration); for(typename std::map::iterator iter = m_entity_map.begin(); iter != m_entity_map.end(); ++iter) { if(cb.exec(static_cast(iter->second))) { return true; } } return false; } template bool exec_if(Entity_Exec &cb) { bool ret = false; Guard_Ref guard(m_in_iteration); for(typename std::map::iterator iter = m_entity_map.begin(); iter != m_entity_map.end(); ++iter) { Concrete_Entity *concrete_entity = static_cast(iter->second); if(cb.exec(concrete_entity)) { ret = true; } } return ret; } void delete_entity(Key key) { if(!in_iteration()) { m_entity_map.erase(key); } } template bool delete_if(Entity_Exec &cb, std::vector &del_vec) { if(in_iteration()) { return false; } Guard_Ref guard(m_in_iteration); bool ret = false; for(typename std::map::iterator iter = m_entity_map.begin(); iter != m_entity_map.end(); ++iter) { Concrete_Entity *concrete_entity = static_cast(iter->second); if(cb.can_delete(concrete_entity)) { ret = true; del_vec.push_back