Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿

对于像游戏引擎的程序设计而言
引擎的体系结构的重要性质是不言而喻的
其中对象基类的设计也是很重要的
主要的功能应该包含以下方面:
1.对象类型识别
2.对象的继承关系识别
3.获取对象名字
4.获取对象编号
5.附带有智能指针
6.当然必要的序列话作用也是必要的
最基本的是对象引用计数类:

 1 ////////////////////////////////////////////////////////////
 2 /// 定义引擎引用计数类
 3 ////////////////////////////////////////////////////////////     
 4 class RefCount
 5 {
 6 public:
 7     ////////////////////////////////////////////////////////////
 8     /// 引用计数构造函数
 9     ////////////////////////////////////////////////////////////
10     RefCount(): refcnt(1),auto_del(true){}
11 
12     ////////////////////////////////////////////////////////////
13     /// 引用计数析构函数
14     ////////////////////////////////////////////////////////////
15     virtual ~RefCount(){}
16     
17     ////////////////////////////////////////////////////////////
18     /// 使用自删除机制
19     ////////////////////////////////////////////////////////////    
20     void SetAutoDel(bool able = true){auto_del = able;}
21     
22     ////////////////////////////////////////////////////////////
23     /// 检测是否为自删除机制
24     ////////////////////////////////////////////////////////////
25     const bool GetAutoDel()const{return auto_del;}
26     
27     ////////////////////////////////////////////////////////////
28     /// 增加对象引用计数
29     ////////////////////////////////////////////////////////////
30     void Grab()const{++refcnt;}; 
31  
32     ////////////////////////////////////////////////////////////
33     /// 减少对象引用计数
34     ////////////////////////////////////////////////////////////
35     bool Drop() const
36     {
37         ASSERT(refcnt>0 && "bad refcnt number!");
38 
39         --refcnt;
40         if (refcnt == 0 && auto_del == true)
41         {
42             delete this;
43             return true;
44         }
45 
46         return false;
47     }
48 
49     ////////////////////////////////////////////////////////////
50     /// 获取引用计数
51     ////////////////////////////////////////////////////////////
52     int GetRefCnt()const{return refcnt;}; 
53  
54 private:
55     mutable int  refcnt;
56     bool    auto_del; 
57 };

该类的作用是对象计数的增加和减少以及获取对象计数,是否调用对象自动删除功能

下面这个是rtti类
 1 ////////////////////////////////////////////////////////////
 2 /// 定义引擎实时对象识别类RTTI
 3 ////////////////////////////////////////////////////////////
 4 class G_DLL_API Rtti
 5 {
 6 public:
 7     ////////////////////////////////////////////////////////
 8     /// 必须确保引擎中对象的名字都是唯一的:)
 9     ////////////////////////////////////////////////////////
10     Rtti(const std::string &name, const Rtti* parent);
11     
12     ////////////////////////////////////////////////////////
13     /// 析构函数
14     ////////////////////////////////////////////////////////    
15     ~Rtti();
16 
17     ////////////////////////////////////////////////////////
18     /// 获取Rtti指定对象名字
19     ////////////////////////////////////////////////////////    
20     std::string GetName() const;
21 
22     ////////////////////////////////////////////////////////
23     /// 获取Rtti指定对象使用的磁盘大小 
24     ////////////////////////////////////////////////////////    
25     int GetDiskUsed() const;
26 
27     ////////////////////////////////////////////////////////
28     /// 检测2个rtti是不是表示同意对象
29     ////////////////////////////////////////////////////////    
30     bool IsExactly(const Rtti& type) const;    
31     
32     ////////////////////////////////////////////////////////
33     /// 查询Rtti指定对象的关系
34     ////////////////////////////////////////////////////////    
35     bool IsDerived(const Rtti& type) const;
36 
37 private:
38     const std::string  name;
39     const Rtti*  parent;
40 };
该类并不直接调用
他的作用是辅助获取对象名字以及检测对象是不是同一类的,对象的继承关系
 1 ////////////////////////////////////////////////////////////
 2 /// 引擎大部分对象的基类(提供对象id,名称,rtti信息)
 3 ////////////////////////////////////////////////////////////
 4 class G_DLL_API Object: public RefCount
 5 /*#ifdef SLOT_EXT_SIGCPLUSPLUS
 6 ,public sigc::trackable
 7 #elif defined(SLOT_EXT_SIGSLOT)
 8 ,public SlotHolder
 9 #endif*/  
10 {
11 public:
12     ////////////////////////////////////////////////////////
13     /// 对象构造函数
14     ////////////////////////////////////////////////////////    
15     Object(); 
16     
17 protected:
18     ////////////////////////////////////////////////////////
19     /// 基类对象的纯析构函数
20     ////////////////////////////////////////////////////////
21     virtual ~Object() = 0;    
22     
23 public:    
24 
25     ////////////////////////////////////////////////////////
26     ///实时对象宏
27     ////////////////////////////////////////////////////////    
28     DECLARE_OBJECT(Object);
29     
30     ////////////////////////////////////////////////////////
31     /// 检测是否为同一对象类型
32     ////////////////////////////////////////////////////////    
33     bool  IsExactly(const Rtti& type) const;
34 
35     ////////////////////////////////////////////////////////
36     /// 检测是否有继承关系
37     ////////////////////////////////////////////////////////    
38     bool  IsDerived(const Rtti& baseconst;
39     
40     ////////////////////////////////////////////////////////
41     /// 检测是否为指定对象实例
42     ////////////////////////////////////////////////////////    
43     bool  IsExactlyTypeOf(const Object* obj) const;
44     
45     ////////////////////////////////////////////////////////
46     /// 检测是否为指定对象的子类实例
47     ////////////////////////////////////////////////////////    
48     bool  IsDerivedTypeOf(const Object* obj) const;    
49     
50 public:    
51 
52     ////////////////////////////////////////////////////////
53     /// 获取对象实例名字
54     ////////////////////////////////////////////////////////    
55     std::string GetName() const;
56 
57     ////////////////////////////////////////////////////////
58     /// 设置对象实例名字
59     ////////////////////////////////////////////////////////    
60     void SetName(const std::string &name);
61     
62     ////////////////////////////////////////////////////////
63     /// 设置对象实例名字
64     ////////////////////////////////////////////////////////    
65     const uint GetObjectID()const{return id;}    
66     
67     ////////////////////////////////////////////////////////
68     /// 获取下一个ID索引
69     ////////////////////////////////////////////////////////    
70     static uint GetNextObjectID(){return nextid;}  
71 
72 public:  
73     ////////////////////////////////////////////////////////
74     /// 类型转化
75     ////////////////////////////////////////////////////////
76     template<class T>
77     T* CastTo() 
78     {   
79         return dynamic_cast<T*>(this); 
80     }
81     
82     template<class T>
83     const T* CastTo() const 
84     {   
85         return dynamic_cast<const T*>(this); 
86     }
87 
88 protected:
89     ////////////////////////////////////////////////////////
90     /// 对象的对象实例名字
91     ////////////////////////////////////////////////////////     
92     std::string name;
93     uint   id;
94     static uint nextid;     
95 };
对象Object是引擎对象的一般基类
该类提供了一般该有的功能同时提供了2个函数模板提供对象指针向上和向下转换的功能

同时配套的智能指针也是必不可少的
如下所示:
 1 ////////////////////////////////////////////////////////
 2 /// 模板RefPtr用于基于计数的对象管理
 3 /// 所有对象都必须要从RefCount继承之
 4 ////////////////////////////////////////////////////////
 5 template<class T>
 6 class RefPtr
 7 {
 8 public:
 9     RefPtr(T* object = NULL)
10     {
11         this->object = object;
12         if(this->object)
13            object->Grab();
14     }
15     
16     RefPtr(const RefPtr<T>& other)
17     {
18         object = NULL;
19         *this = other;
20     }
21     
22     ~RefPtr() 
23     {
24         if(object)
25            object->Drop();
26         object = NULL;
27     }
28  
29     RefPtr<T>& operator=(const RefPtr<T>& other)
30     {
31         if (other)
32            other->Grab();
33         if (object)
34            object->Drop();
35         object = other.get();
36         return *this;
37     }
38     
39     RefPtr& operator=(T* other)
40     {
41         if (other)
42             other->Grab(); 
43          if (object)
44             object->Drop();
45         object = other;
46         return *this;
47     }
48     
49     void swap(RefPtr<T>& other)
50     {
51         T* tmp = other.get();
52         other = object
53         object = tmp;
54     }
55     
56     void reset(T* other){*this = other;}
57     
58     T* get() const {return object;}
59     
60     T* operator->() const 
61     {   
62         ASSERT(object && "bad object ptr");
63         return object
64     }
65     
66     T& operator*() const 
67     {   
68         ASSERT(object && "bad object ptr"); 
69         return *object
70     }
71     
72     bool operator<(const RefPtr<T>& other) const 
73     {   
74         return object < other.get(); 
75     }
76   
77     ////////////////////////////////////////////////////////
78     /// 检测指针是否为空
79     ////////////////////////////////////////////////////////
80     operator bool() const {return object != NULL;}
81 
82 protected:
83     T*   object;
84 };
这里的编码并没有按照引擎函数的命名习惯并是采用一般的智能指针函数的写法

同时为了简化编码采取了类似mfc的宏写法
 1 //////////////////////////////////////////////////////// 
 2 /// 把对象中的实时类对象转化为指针的宏定义
 3 ////////////////////////////////////////////////////////
 4 #define RUNTIME_RTTI(object)\
 5     (core::Rtti*)(&object::rtti##object)    
 6     
 7 //////////////////////////////////////////////////////// 
 8 /// 声明对象的宏定义在每个引擎对象中加入
 9 //////////////////////////////////////////////////////// 
10 #define DECLARE_OBJECT(object)\
11 public: \
12     static  const core::Rtti rtti##object;;\
13     virtual const core::Rtti* GetRtti()const;\
14     virtual std::string GetObjectName()const
15     
16 //////////////////////////////////////////////////////// 
17 /// 实现对象的宏定义 
18 //////////////////////////////////////////////////////// 
19 #define IMPLEMENT_BASE_OBJECT(object)\
20     const core::Rtti  object::rtti##object(#object,NULL);\
21     inline const core::Rtti*  object::GetRtti()const\
22     {\
23         return  RUNTIME_RTTI(object);\
24     }\
25     inline std::string object::GetObjectName()const\
26     {\
27         return GetRtti()->GetName();\
28     }    
29     
30 //////////////////////////////////////////////////////// 
31 /// 实现对象的宏定义在每个引擎对象实现中加入
32 //////////////////////////////////////////////////////// 
33 #define IMPLEMENT_OBJECT(object,parent)\
34     const core::Rtti   object::rtti##object(#object,RUNTIME_RTTI(parent));\
35     const core::Rtti*  object::GetRtti()const\
36     {\
37         return  RUNTIME_RTTI(object);\
38     }\
39     std::string  object::GetObjectName()const\
40     {\
41         return  GetRtti()->GetName();\
42     }     

当然当前这里并没有加入对象系列化的功能.

当前盖莫引擎对象基类设计主要参考了mfc,irr object,vgl以及g3d engine.
如果设计有什么问题请提示我ccsdu2009@126.com
posted on 2010-03-21 16:46 ccsdu2009 阅读(1476) 评论(7)  编辑 收藏 引用 所属分类: Game引擎
Comments
  • # re: 盖莫游戏引擎的对象基类设计
    陈梓瀚(vczh)
    Posted @ 2010-03-21 17:11
    我认为C++标准的rtti实现非常好,你用不找自己搞一个,反正gcc和VC++都支持。  回复  更多评论   
  • # re: 盖莫游戏引擎的对象基类设计
    ccsdu2009
    Posted @ 2010-03-21 17:15
    @陈梓瀚(vczh)
    那对象的动态生成呢?  回复  更多评论   
  • # re: 盖莫游戏引擎的对象基类设计
    陈梓瀚(vczh)
    Posted @ 2010-03-21 17:18
    @ccsdu2009
    游戏引擎应该避免这种事情  回复  更多评论   
  • # re: 盖莫游戏引擎的对象基类设计[未登录]
    ccsdu2009
    Posted @ 2010-03-21 17:22
    @陈梓瀚(vczh)
    hehe  回复  更多评论   
  • # re: 盖莫游戏引擎的对象基类设计
    Davy.xu
    Posted @ 2010-03-21 23:56
    @陈梓瀚(vczh)
    虚幻,Gamebryo都是使用对象动态生成。而且这类反射系统对于代码结构优化有很多好处  回复  更多评论   
  • # re: 盖莫游戏引擎的对象基类设计
    陈梓瀚(vczh)
    Posted @ 2010-03-24 12:54
    @Davy.xu
    既然每一个rtti对象都是你在代码里面指定的,那跟直接写一个类名还是factory.CreateXXX在语义上都是没有区别的。就算你可能写creater=factory.GetXXXCreater();creater.CreateObject()等,这也不需要创建真实的rtti对象。所以这其实只是风格问题,但是这种风格让你的类被侵入了一些静态对象,这经常会令VC++内置的内存泄漏检查输出一些噪音,非常不利于调试。  回复  更多评论   
  • # re: 盖莫游戏引擎的对象基类设计
    Davy.xu
    Posted @ 2010-03-24 14:18
    @陈梓瀚(vczh)
    呵呵,我的RTTI跟虚幻的一样,虚幻在一定程度上用静态当singleton用,其实也没什么。至于内存泄漏检查这块,核心模块自己检查下,避免出问题,核心外代码还是用内存检查,没什么不妥。
    虚幻里的创建对象类似于 UObject::CreateInstance 没有creator之类的东西,可以在引擎任何地方创建出对象  回复  更多评论   

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