如何軟件設計中總有那么幾個比較小巧卻比較有用的小段代碼比如boost中的utility
蓋莫游戲引擎也一樣有幾個比較小的代碼片段:
如下這是關于指針的:
1 //! 定義一個檢測指針是否為空的宏
2 /*!
3 當指針為空的話則返回指定值
4 例子如下:
5 e.g: Object* ptr = CreateObj();
6 如果指針為空則返回-1
7 CHECK_PTR(ptr,-1)
8 或者如果指針為空則返回;
9 CHECK_PTR(ptr,;)
10 */
11 #define CHECK_PTR(ptr,result)\
12 if(NULL == ptr)\
13 return result;
14
15 //! 指針銷毀
16 #define CHECK_PTR_AND_DELETE(ptr)\
17 if(NULL!=ptr)\
18 {\
19 delete ptr;\
20 ptr = NULL;\
21 }
22
23 #define CHECK_PTR_ARRAY_AND_DELETE(ptr)\
24 if(NULL!=ptr)\
25 {\
26 delete []ptr;\
27 ptr = NULL;\
28 }
這個比較好理解不說什么。
下面的這個是對象操作符重載相關的手法源于boost operator
但是這個要比Boost中的好理解和容易使用一些
1 ////////////////////////////////////////////////////////////
2 /// 處理運算符重載的宏
3 /// 類似的例子為boost的operators為友元和基類
4 /// 這里采用的是宏實現形式:)
5 /// 歡迎討論:)
6 ////////////////////////////////////////////////////////////
7
8 ////////////////////////////////////////////////////////////
9 /// 可減的
10 ////////////////////////////////////////////////////////////
11 #define SUBTRACTABLE(UDT,T)\
12 UDT& operator-=(const T& t)\
13 {\
14 *this = *this - t;\
15 return *this;\
16 }
17
18 ////////////////////////////////////////////////////////////
19 /// 可加的
20 ////////////////////////////////////////////////////////////
21 #define ADDABLE(UDT,T)\
22 UDT& operator+=(const T& t)\
23 {\
24 *this = *this + t;\
25 return *this;\
26 }
27
28 ////////////////////////////////////////////////////////////
29 /// 可乘的
30 ////////////////////////////////////////////////////////////
31 #define MULTIPLIABLE(UDT,T)\
32 UDT& operator*=(const T& t)\
33 {\
34 *this = *this * t;\
35 return *this;\
36 }
37
38 ////////////////////////////////////////////////////////////
39 /// 可除的
40 ////////////////////////////////////////////////////////////
41 #define DIVISIBLE(UDT,T)\
42 UDT& operator/=(const T& t)\
43 {\
44 *this = *this/t;\
45 return *this;\
46 }
47
48 ////////////////////////////////////////////////////////////
49 /// 相等的
50 ////////////////////////////////////////////////////////////
51 #define IS_EQUAL(T)\
52 bool operator==(const T& t)\
53 {\
54 void *this_address = (void*)this;\
55 const void* other_address = reinterpret_cast<const void*>(&t);\
56 return memcmp(this_address,other_address,sizeof(T)) == 0;\
57 }
58
59 ////////////////////////////////////////////////////////////
60 /// 不相等的
61 ////////////////////////////////////////////////////////////
62 #define NOT_EQUAL(T)\
63 bool operator!=(const T& t)\
64 {\
65 return !(*this == t);\
66 }
67
68 ////////////////////////////////////////////////////////////
69 /// 使用相等和不相等
70 ////////////////////////////////////////////////////////////
71 #define USE_EQUAL(T)\
72 IS_EQUAL(T)\
73 NOT_EQUAL(T)
74
75 ////////////////////////////////////////////////////////////
76 /// 大于的
77 ////////////////////////////////////////////////////////////
78 #define BIG_THAN(T)\
79 bool operator>(const T& t)\
80 {\
81 return !((*this != t) || (*this < t));\
82 }
83
84 ////////////////////////////////////////////////////////////
85 /// 小于的
86 ////////////////////////////////////////////////////////////
87 #define SMALL_THAN(T)\
88 bool operator<(const T& t)\
89 {\
90 return !((*this != t) || (*this > t));\
91 }
92
93 ////////////////////////////////////////////////////////////
94 /// 類賦值
95 ////////////////////////////////////////////////////////////
96 #define COPY_CLASS(Object)\
97 Object(const Object& obj)\
98 {\
99 *this = obj;\
100 }\
101 Object& operator=(const Object& object)\
102 {\
103 void *this_address = (void*)this;\
104 const void* other_address = reinterpret_cast<const void*>(&object);\
105 memcpy(this_address,other_address,sizeof(object));\
106 return *this;\
107 }
108
109 #define COPY_OBJECT(T) COPY_CLASS(T)
接下來這個是個dummy類(中文應該如何表達?)
其用途就是作為占位符使用之
1 ////////////////////////////////////////////////////////////
2 /// 定義一個可選基類作為dummy使用之
3 ////////////////////////////////////////////////////////////
4 struct Base
5 {
6 virtual ~Base(){};
7
8 //! 操作符重載
9 inline bool operator==(const Base& base){return false;}
10 };
下來這個是指針的刪除
1 ////////////////////////////////////////////////////////////
2 /// 檢查性指針清空
3 /// 手法源于Boost庫
4 ////////////////////////////////////////////////////////////
5 template<class T>
6 inline void CheckedDelete(T * x)
7 {
8 typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
9 (void) sizeof(type_must_be_complete);
10 delete x;
11 }
采用了一點c++模板元編程的東東
下面的也是
1 ////////////////////////////////////////////////////////////
2 /// 檢測給定對象是不是同一類型
3 ////////////////////////////////////////////////////////////
4 template<class L, class R>
5 struct IsSameObject
6 {
7 enum{flag = false};
8 };
9
10 template<class T>
11 struct IsSameObject<T,T>
12 {
13 enum{flag= true};
14 };
15
1 ////////////////////////////////////////////////////////////
2 /// 提供基類一個參數
3 ////////////////////////////////////////////////////////////
4 template<class Base,class Arg>
5 struct SingleArg : public Base
6 {
7 SingleArg() : Base((Arg)0) {}
8 };
9
10 template<class Base>
11 struct SingleArg<Base,void> : public Base
12 {
13 SingleArg() : Base() {}
14 };
下面的是不可復制類
1 ////////////////////////////////////////////////////////////
2 //! 定義一個實用的不可使用默認構造函數的基類
3 ////////////////////////////////////////////////////////////
4 class NonCopyable
5 {
6 protected:
7 NonCopyable(){}
8 private:
9 NonCopyable(const NonCopyable&);
10 NonCopyable& operator =(const NonCopyable&);
11 };
當然也可以這樣寫:
1 #define NonCopyable(T)\
2 private:\
3 T(const T& t);\
4 T& operator=(const T&);
當然了其原理是一樣的呵呵
這是單態的實現:
1 ////////////////////////////////////////////////////////////
2 /// 單態模板類(非線程安全的)
3 ////////////////////////////////////////////////////////////
4 template <typename Base, typename T>
5 class Singleton
6 {
7 public:
8
9 //! 獲取對象指針
10 /*!
11 這里暫時不需要線程安全的版本:)
12 */
13 static T* Instance()
14 {
15 if( NULL == instance )
16 instance = new T;
17 assert(instance = 0);
18 return instance;
19 }
20
21 //! 對象指針的析構
22 static void Deinit()
23 {
24 delete instance;
25 instance = NULL;
26 }
27
28 private:
29 static Base* instance;
30 };
31
32 //! 靜態成員初始化
33 template <typename Base, typename T>
34 Base* Singleton<Base, T>::instance = NULL;
35
引擎異常類:
1 ////////////////////////////////////////////////////////////
2 /// 定義引擎異常類
3 ////////////////////////////////////////////////////////////
4 class Exception : public std::exception//!, NonCopyable
5 {
6 public:
7 Exception( const std::string& msg) : why(msg) {}
8 virtual ~Exception() throw() {}
9 public:
10 inline virtual const char* what()const throw() {return why.c_str();}
11 private:
12 std::string why;
13 };
類型轉換
1 ////////////////////////////////////////////////////////////
2 /// 把指定數據類型轉換為字符串類型
3 ////////////////////////////////////////////////////////////
4 template<class T>
5 std::string ToString(T value)
6 {
7 std::ostringstream os;
8 os << value;
9 return os.str();
10 }
11
斷言版本:
1 #define THROW(r) throw core::Exception(r + std::string("filename: ") + std::string(__FILE__) + std::string(" line: ") + ToString(__LINE__))
2
3 #ifndef ASSERT
4 #define ASSERT(b) if(!(b)) THROW(std::string("assertion <") + #b + std::string("> failed"))
5 #endif
cpu freq
1 //! 獲取cpu頻率計數
2 inline uint64 _GetCpuFreqCnt()
3 {
4 #ifdef _MSC_VER
5 _asm _emit 0x0f
6 _asm _emit 0x31
7 #else
8 uint64 high32,low32;
9 __asm("rdtsc":"=a"(low32),"=d"(high32));
10 #endif
11 }
泛型父類模板(在場景和UI系統中會大量使用)
1 ////////////////////////////////////////////////////////////
2 /// 定義泛對象父類
3 ////////////////////////////////////////////////////////////
4 //
5 /// 使用例子如下
6 /// class Object : public Parent<Object>
7 // 











.
8 //
9 template<class Object>
10 class Parent
11 {
12 public:
13
14 inline Parent(Object parent = NULL){object = parent;}
15 virtual ~Parent(){}
16 inline void SetParent(Object parent){object = parent; }
17 inline Object GetParent()const{return object;}
18 inline bool HasParent()const{return (NULL != object);}
19 Object GetTopParent()const
20 {
21 Object ret = object;
22 Object obj = ret;
23 while(obj != NULL)
24 {
25 ret = obj;
26 obj = ret->GetParent();
27 }
28 return ret;
29 }
30
31 private:
32 Object object;
33 };
最后來一個類型持有者(這里少一個構造函數):
1 ////////////////////////////////////////////////////////////
2 /// 定義一個獲取設置對象類型的類
3 ////////////////////////////////////////////////////////////
4 template<class T>
5 class TypeHolder
6 {
7 public:
8 inline void SetType(const T &t){type = t;}
9 inline T GetType()const{return type;}
10 private:
11 T type;
12 };
13