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

如何软件设计中总有那么几个比较小巧却比较有用的小段代码比如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     (voidsizeof(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  
posted on 2010-02-10 15:17 ccsdu2009 阅读(1435) 评论(3)  编辑 收藏 引用
Comments
  • # re: 盖莫游戏引擎中的便利宏和及几个小类
    ccsdu2009
    Posted @ 2010-02-10 15:18
    引擎只在底层使用boost并没有在头文件中使用之
    一个重要的原因就是为了提高代码的可读性
    引擎是给别人使用的
    保持良好的可读性是十分重要的  回复  更多评论   
  • # re: 盖莫游戏引擎中的便利宏和及几个小类
    陈梓瀚(vczh)
    Posted @ 2010-02-13 17:08
    1,COPY_CLASS定义的东西都是自动生成的,你不用专门去写。
    2,我觉得单态的那个对象的类型如果不能被重复创建,那你就定义成函数。不能重复创建的类型没有定义的必要,这组东西如果太多就写在namespace里面。
    3,ToString太重,你用StrToInt也可以。
    4,GetCpuFreqCnt如果在64位下不行的话,最好#ifdef一下。
    5,Parent应该是一棵树吧,最好重命名。
    6,TypeHolder那样定义仅会让你的代码变长,最好用一个类把所有的设置都装在一起,或者把他们定义成全局变量。全局变量跟单态是一样的,不要因为换一种方法就觉得自己避免了全局变量一样。  回复  更多评论   
  • # re: 盖莫游戏引擎中的便利宏和及几个小类
    ccsdu2009
    Posted @ 2010-02-22 14:56
    @陈梓瀚(vczh)
    StrToInt? 有这个函数?  回复  更多评论   

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