對于像游戲引擎的程序設計而言
引擎的體系結構的重要性質是不言而喻的
其中對象基類的設計也是很重要的
主要的功能應該包含以下方面:
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& base) const;
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