注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Code@Pig Home

喜欢背着一袋Code傻笑的Pig .. 忧美.欢笑.记忆.忘却 .之. 角落

 
 
 

日志

 
 

[wm4] LibGraphics/ObjectSystem/Wm4Rtti  

2010-01-29 08:56:28|  分类: 3d_WildMagic |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
WildMagic 4 的代码还是相当简明、清晰的,让人看起来很舒服。

RTTI 在 ObjectSystem 中的实现很简洁,也很传统。
每个继承于 Wm4::Object 的 class,都会带上一个 Wm4::Rtti,由其形成继承树。比如:
                 Object.Rtti
                /                \
        Spatial.Rtti     Control.Rtti
               |
        Node.Rtti

用法如下:
class MyFoo {
    WM4_DECLARE_RTTI;
};

// Wm4 - namespace
// Spatial - class name
// Object - base class name
WM4_IMPLEMENT_RTTI(Wm4,Spatial,Object);

下面给个具体的小例子,看看如何运用之。
---------------------------------------------------------------
//            Object
//                 |
//            MyBase
//          /             \
//   MyFoo1         MyBar
//        |
//   MyFoo2
#include <Wm4Object.h>
#include <iostream>

namespace Wm4
{

class MyBase : public Object
{
public:
    WM4_DECLARE_RTTI;
};
WM4_IMPLEMENT_RTTI(Wm4, MyBase, Object);

class MyFoo1 : public MyBase
{
public:
    WM4_DECLARE_RTTI;
};
WM4_IMPLEMENT_RTTI(Wm4, MyFoo1, MyBase);

class MyFoo2 : public MyFoo1
{
public:
    WM4_DECLARE_RTTI;
};
WM4_IMPLEMENT_RTTI(Wm4, MyFoo2, MyFoo1);

class MyBar : public MyBase
{
public:
    WM4_DECLARE_RTTI;
};
WM4_IMPLEMENT_RTTI(Wm4, MyBar, MyBase);

}

int main()
{
    Wm4::Object *base = WM4_NEW Wm4::MyBase;
    Wm4::Object *foo1 = WM4_NEW Wm4::MyFoo1;
    Wm4::Object *foo2 = WM4_NEW Wm4::MyFoo2;
    Wm4::Object *bar  = WM4_NEW Wm4::MyBar;

    std::cout << "MyFoo1.IsExactlyTypeOf(MyBase) = " << foo1->IsExactlyTypeOf(base) << std::endl;
    std::cout << "MyFoo1.IsDerivedTypeOf(MyBase) = " << foo1->IsDerivedTypeOf(base) << std::endl;

    std::cout << "MyFoo2.IsExactlyTypeOf(MyBase) = " << foo2->IsExactlyTypeOf(base) << std::endl;
    std::cout << "MyFoo2.IsDerivedTypeOf(MyBase) = " << foo2->IsDerivedTypeOf(base) << std::endl;

    std::cout << "MyBar.IsExactlyTypeOf(MyBase) = " << bar->IsExactlyTypeOf(base) << std::endl;
    std::cout << "MyBar.IsDerivedTypeOf(MyBase) = " << bar->IsDerivedTypeOf(base) << std::endl;

    std::cout << "MyFoo2.IsExactlyTypeOf(MyFoo1) = " << foo2->IsExactlyTypeOf(foo1) << std::endl;
    std::cout << "MyFoo2.IsDerivedTypeOf(MyFoo1) = " << foo2->IsDerivedTypeOf(foo1) << std::endl;

    std::cout << "MyFoo2.IsExactlyTypeOf(MyBar) = " << foo2->IsExactlyTypeOf(bar) << std::endl;
    std::cout << "MyFoo2.IsDerivedTypeOf(MyBar) = " << foo2->IsDerivedTypeOf(bar) << std::endl;

    return 0;
}
---------------------------------------------------------------
$ ./a.out
MyFoo1.IsExactlyTypeOf(MyBase) = 0
MyFoo1.IsDerivedTypeOf(MyBase) = 1
MyFoo2.IsExactlyTypeOf(MyBase) = 0
MyFoo2.IsDerivedTypeOf(MyBase) = 1
MyBar.IsExactlyTypeOf(MyBase) = 0
MyBar.IsDerivedTypeOf(MyBase) = 1
MyFoo2.IsExactlyTypeOf(MyFoo1) = 0
MyFoo2.IsDerivedTypeOf(MyFoo1) = 1
MyFoo2.IsExactlyTypeOf(MyBar) = 0
MyFoo2.IsDerivedTypeOf(MyBar) = 0
---------------------------------------------------------------

  评论这张
 
阅读(1091)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017