·天新网首页·加入收藏·设为首页
首页|笔记本|手机|数码相机|摄像机|MP3/MP4|主板|内存|显示器|办公|打印机|下载|开发|学院|汽车|业界
硬件|台式机|数码|数字家庭|投影仪|GPS/CPU|显卡|硬盘|服务器|网络|一体机|驱动|源码|考试|游戏|通信
您现在的位置:天新网 > 软件开发 > 游戏开发
Nebula2探秘05-RTTI
http://dev.21tx.com 2007年05月07日 happykevins

Nebula2探秘05-RTTI

happykevins文 

       Nebula2的RTTI支持都是在nObject这一层实现的,每个继承自nObject的类都有IsA()和IsInstanceof()两个方法,前者用于判断继承关系,后者则用于判断一个对象是否为指定类的实例,下面的代码说明了他们的用法。

 

/****************************************************************************/
/*    Nebula2 - Tutorial 05                                                    */
/*  Nebula2 RTTI                                                            */
/*  author: happykevins                                                        */
/****************************************************************************/

///----------------------------------------------------------------------------
/// +必要头文件
#include "kernel/nkernelserver.h"
#include 
"kernel/ntimeserver.h"
#include 
"kernel/nfileserver2.h"
/// -必要头文件
///----------------------------------------------------------------------------

///----------------------------------------------------------------------------
/// +链接库
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "d_nkernel.lib")
/// -链接库
///----------------------------------------------------------------------------

///----------------------------------------------------------------------------
/// +Application
int main(int argc, const char** argv)
{
    
/// 创建KernelServer
    nKernelServer* ks = n_new(nKernelServer);

    
///----------------------------------------------------------------------------
    
/// +Nebula2 RTTI Test
    
/// @note:
    
///        Nebula2 RTTI implement mostly depends on nClass && nObject

    
/// firstly: we got some instance (all inherited from nObject)
    nRoot* nroot = (nRoot*)ks->New("nroot");
    nObject
* nobject = (nObject*)ks->New("nobject");
    nTimeServer
* ntimeserver = (nTimeServer*)ks->Lookup("/sys/servers/time");
    nFileServer2
* nfileserver2 = (nFileServer2*)ks->Lookup("/sys/servers/file2");

    
/// secondly: Test RTTI Info.
    
/// use 'IsA' & 'IsInstanceOf' methods

    
// 1. nroot is a nroot?
    n_printf("[%s] is %s [%s] "
        nroot
->GetClass()->GetName(), 
        nroot
->IsA("nroot"? "\b" : "not"
        
"nroot");
    
// 2. nroot is a nobject?
    n_printf("[%s] is %s [%s] "
        nroot
->GetClass()->GetName(), 
        nroot
->IsA(ks->FindClass("nobject")) ? "\b" : "not"
        
"nobject");
    
// 3. nobject is a nroot?
    n_printf("[%s] is %s [%s] "
        nobject
->GetClass()->GetName(), 
        nobject
->IsA("nroot"? "\b" : "not"
        
"nroot");
    
// 4. ntimerserver is a nroot?
    n_printf("[%s] is %s [%s] "
        ntimeserver
->GetClass()->GetName(), 
        ntimeserver
->IsA("nroot"? "\b" : "not"
        
"nroot");
    
// 5. nroot is instance of nroot?
    n_printf("[%s] is %s instance of [%s] "
        nroot
->GetClass()->GetName(), 
        nroot
->IsInstanceOf(ks->FindClass("nroot")) ? "\b" : "not"
        
"nroot");
    
// 6. nroot is instance of nobject?
    n_printf("[%s] is %s instance of [%s] "
        nroot
->GetClass()->GetName(), 
        nroot
->IsInstanceOf(ks->FindClass("nobject")) ? "\b" : "not"
        
"nobject");
    
// 7. nfileserver2 is a nobject?
    n_printf("[%s] is %s [%s] "
        nfileserver2
->GetClass()->GetName(), 
        nfileserver2
->IsA("nobject"? "\b" : "not"
        
"nobject");
    
// 8. nfileserver2 is a ntimeserver?
    n_printf("[%s] is %s [%s] "
        nfileserver2
->GetClass()->GetName(), 
        nfileserver2
->IsA("ntimeserver"? "\b" : "not"
        
"ntimeserver");
    
    
/// +Nebula2 RTTI Test
    
///----------------------------------------------------------------------------
    

    
/// 销毁KernelServer
    n_delete(ks);

    getchar();
    
return 0;
}
/// -Application
///----------------------------------------------------------------------------

上一篇: Nebula2探秘10-Signal System
下一篇: Nebula2探秘11-Simple Window

25年牵手相伴,2500台经典机型限量特供,800-858-2883手机400-884-9425

热点文章
关于我们 | 联系我们 | 广告服务 | 工作机会 | 版权声明 | 欢迎投稿 | 网站地图
Copyright © 2000-2009 , www.21tx.com , All Rights Reserved .
晨新科技 版权所有 Created by TXSite.net