Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
在盖莫游戏引擎的书写过程中先后书写了资源管理器,场景管理器,模型管理器等管理器类.之后感觉很有必要写一个泛型的管理器类了.这个可以少做一些重复性的工作了。
管理器类无非就是获取当前对象个数,对象生成,对象按名索取等等
经过考虑,草料书写如下:
 1 namespace core
 2 {
 3 
 4 ////////////////////////////////////////////////////////////
 5 //! 定义引擎泛型管理器类
 6 //////////////////////////////////////////////////////////// 
 7 template<class Obj = Object, class Type = std::string>
 8 class Manager  
 9 {        
10 public:     
11     typedef Type                                          ThisType;
12     typedef Obj                                           ThisObj;
13     typedef std::map<ThisType,RefPtr<ThisObj> >           Table;
14     //! typedef std::map<ThisType,RefPtr<ThisObj> >::iterator TableItr; 
15     
16     ////////////////////////////////////////////////////////
17     //! 构造,析构场景管理器
18     //////////////////////////////////////////////////////// 
19     Manager(){}
20     virtual ~Manager() = 0
21 public:     
22                                           
23     ////////////////////////////////////////////////////////////
24     /// 获取当前管理器中的对象个数
25     ////////////////////////////////////////////////////////////                                         
26     inline uint32 GetObjectNumber()const{return objects.size();}
27     
28     ////////////////////////////////////////////////////////////
29     /// 检测当前管理器中是否存在对象
30     ////////////////////////////////////////////////////////////      
31     inline bool   HasObject()const{return !objects.empty();}
32     
33     ////////////////////////////////////////////////////////////
34     /// 获取给定索引的对象(如果对象不存在则生成一个新的对象)
35     ////////////////////////////////////////////////////////////       
36     inline RefPtr<Object> GetObject(const Type& name)
37     {
38         if(objects.find(name) != objects.end())
39             return objects[name];
40         return     NULL;
41     }
42     
43     ////////////////////////////////////////////////////////////
44     /// 生成一个新的对象
45     ////////////////////////////////////////////////////////////     
46     virtual RefPtr<ThisObj> CreateObject(const Type& name) = 0
47     
48     ////////////////////////////////////////////////////////////
49     /// 销毁指定名字的对象
50     ////////////////////////////////////////////////////////////
51     inline bool KillObject(const Type& name)
52     {    
53         std::map<std::string,RefPtr<Model> >::iterator itr = objects.find(name);
54         if(itr == objects.end())
55             return false;
56         objects.erase(name);
57         return NULL;         
58     }     
59 
60     ////////////////////////////////////////////////////////////
61     /// 管理器对象清空
62     ////////////////////////////////////////////////////////////    
63     inline void ClearObject(){objects.clear();}
64 protected:
65     Table    objects;     
66 };
67 
68 template<class Obj, class Type>
69 Manager<Obj,Type>::~Manager()
70 {
71     ClearObject();                      
72 
73 
其中使用std::map作为基本的管理器容器
同时其中CreateObject函数是一个虚拟函数需要重载之
然后我们就可以这样写具体的的管理器了.
比如:
1 class ModelManager : public Manager<Model,std::string>
2 {
3 public:
4      RefPtr<Model> CreateObject(const std::string &);    
5 };



posted on 2010-03-08 20:49 ccsdu2009 阅读(1261) 评论(8)  编辑 收藏 引用 所属分类: Game引擎
Comments
  • # re: 盖莫游戏引擎中的范管理器类
    ccsdu2009
    Posted @ 2010-03-08 20:53
    ////////////////////////////////////////////////////////////
    49 /// 销毁指定名字的对象
    50 ////////////////////////////////////////////////////////////
    51 inline bool KillObject(const Type& name)
    52 {
    53 std::map<std::string,RefPtr<Model> >::iterator itr = objects.find(name);
    54 if(itr == objects.end())
    55 return false;
    56 objects.erase(name);
    57 return true;
    58 }   回复  更多评论   
  • # re: 盖莫游戏引擎中的范管理器类
    陈梓瀚(vczh)
    Posted @ 2010-03-09 00:32
    跟一个map没区别……  回复  更多评论   
  • # re: 盖莫游戏引擎中的范管理器类
    东北证券官方网站
    Posted @ 2010-03-09 02:08
    收藏了。。。





    http://www.keybeta.com/quote/  回复  更多评论   
  • # re: 盖莫游戏引擎中的范管理器类
    凡客领带
    Posted @ 2010-03-09 10:00
    很好12363546+6.36552  回复  更多评论   
  • # re: 盖莫游戏引擎中的范管理器类
    cui
    Posted @ 2010-03-09 10:20
    你这效率也太低了吧

    36 inline RefPtr<Object> GetObject(const Type& name)
    37 {
    38 itertor itor = objects.find(name);
    if (itor != objects.end())
    39 return itor->second;
    40 return RefPtr<Object>() ;
    41 }


    inline bool KillObject(const Type& name)
    52 {
    53 return objects.erase (name) > 0;
    58 }   回复  更多评论   
  • # re: 盖莫游戏引擎中的范管理器类
    ccsdu2009
    Posted @ 2010-03-09 10:58
    @cui
    谢谢
    完成功能是第一要务
    效率是其次的  回复  更多评论   
  • # re: 盖莫游戏引擎中的范管理器类
    cui
    Posted @ 2010-03-09 15:20
    @ccsdu2009

    强词夺理! 这种代码需要费神的思考吗?比你的旧代码行数多吗?  回复  更多评论   
  • # re: 盖莫游戏引擎中的范管理器类
    ccsdu2009
    Posted @ 2010-03-09 15:51
    map的erase有三个版本
     1 关于map的erase
     2       void
     3       erase(iterator __position)
     4       { _M_t.erase(__position); }
     5 
     6       /**
     7        *  @brief Erases elements according to the provided key.
     8        *  @param  x  Key of element to be erased.
     9        *  @return  The number of elements erased.
    10        *
    11        *  This function erases all the elements located by the given key from
    12        *  a %map.
    13        *  Note that this function only erases the element, and that if
    14        *  the element is itself a pointer, the pointed-to memory is not touched
    15        *  in any way.  Managing the pointer is the user's responsibilty.
    16        */
    17       size_type
    18       erase(const key_type& __x)
    19       { return _M_t.erase(__x); }
    20 
    21       /**
    22        *  @brief Erases a [first,last) range of elements from a %map.
    23        *  @param  first  Iterator pointing to the start of the range to be
    24        *                 erased.
    25        *  @param  last  Iterator pointing to the end of the range to be erased.
    26        *
    27        *  This function erases a sequence of elements from a %map.
    28        *  Note that this function only erases the element, and that if
    29        *  the element is itself a pointer, the pointed-to memory is not touched
    30        *  in any way.  Managing the pointer is the user's responsibilty.
    31        */
    32       void
    33       erase(iterator __first, iterator __last)
    34       { _M_t.erase(__first, __last); }
    我以前只注意到map.erase(itr);
    谢谢提醒O(∩_∩)O~
      回复  更多评论   

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理