Nebula2探秘05-RTTI
happykevins文
Nebula2的RTTI支持都是在nObject这一层实现的,每个继承自nObject的类都有IsA()和IsInstanceof()两个方法,前者用于判断继承关系,后者则用于判断一个对象是否为指定类的实例,下面的代码说明了他们的用法。
/****************************************************************************/
/*Nebula2-Tutorial05*/
/*Nebula2RTTI*/
/*author:happykevins*/
/****************************************************************************/
///----------------------------------------------------------------------------
///+必要头文件
#include"kernel/nkernelserver.h"
#include"kernel/ntimeserver.h"
#include"kernel/nfileserver2.h"
///-必要头文件
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
///+链接库
#pragmacomment(lib,"wsock32.lib")
#pragmacomment(lib,"d_nkernel.lib")
///-链接库
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
///+Application
intmain(intargc,constchar**argv)
{
///创建KernelServer
nKernelServer*ks=n_new(nKernelServer);
///----------------------------------------------------------------------------
///+Nebula2RTTITest
///@note:
///Nebula2RTTIimplementmostlydependsonnClass&&nObject
///firstly:wegotsomeinstance(allinheritedfromnObject)
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:TestRTTIInfo.
///use'IsA'&'IsInstanceOf'methods
//1.nrootisanroot?
n_printf("[%s]is%s[%s] ",
nroot->GetClass()->GetName(),
nroot->IsA("nroot")?"\b":"not",
"nroot");
//2.nrootisanobject?
n_printf("[%s]is%s[%s] ",
nroot->GetClass()->GetName(),
nroot->IsA(ks->FindClass("nobject"))?"\b":"not",
"nobject");
//3.nobjectisanroot?
n_printf("[%s]is%s[%s] ",
nobject->GetClass()->GetName(),
nobject->IsA("nroot")?"\b":"not",
"nroot");
//4.ntimerserverisanroot?
n_printf("[%s]is%s[%s] ",
ntimeserver->GetClass()->GetName(),
ntimeserver->IsA("nroot")?"\b":"not",
"nroot");
//5.nrootisinstanceofnroot?
n_printf("[%s]is%sinstanceof[%s] ",
nroot->GetClass()->GetName(),
nroot->IsInstanceOf(ks->FindClass("nroot"))?"\b":"not",
"nroot");
//6.nrootisinstanceofnobject?
n_printf("[%s]is%sinstanceof[%s] ",
nroot->GetClass()->GetName(),
nroot->IsInstanceOf(ks->FindClass("nobject"))?"\b":"not",
"nobject");
//7.nfileserver2isanobject?
n_printf("[%s]is%s[%s] ",
nfileserver2->GetClass()->GetName(),
nfileserver2->IsA("nobject")?"\b":"not",
"nobject");
//8.nfileserver2isantimeserver?
n_printf("[%s]is%s[%s] ",
nfileserver2->GetClass()->GetName(),
nfileserver2->IsA("ntimeserver")?"\b":"not",
"ntimeserver");
///+Nebula2RTTITest
///----------------------------------------------------------------------------
///销毁KernelServer
n_delete(ks);
getchar();
return0;
}
///-Application
///----------------------------------------------------------------------------
/*Nebula2-Tutorial05*/
/*Nebula2RTTI*/
/*author:happykevins*/
/****************************************************************************/
///----------------------------------------------------------------------------
///+必要头文件
#include"kernel/nkernelserver.h"
#include"kernel/ntimeserver.h"
#include"kernel/nfileserver2.h"
///-必要头文件
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
///+链接库
#pragmacomment(lib,"wsock32.lib")
#pragmacomment(lib,"d_nkernel.lib")
///-链接库
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
///+Application
intmain(intargc,constchar**argv)
{
///创建KernelServer
nKernelServer*ks=n_new(nKernelServer);
///----------------------------------------------------------------------------
///+Nebula2RTTITest
///@note:
///Nebula2RTTIimplementmostlydependsonnClass&&nObject
///firstly:wegotsomeinstance(allinheritedfromnObject)
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:TestRTTIInfo.
///use'IsA'&'IsInstanceOf'methods
//1.nrootisanroot?
n_printf("[%s]is%s[%s] ",
nroot->GetClass()->GetName(),
nroot->IsA("nroot")?"\b":"not",
"nroot");
//2.nrootisanobject?
n_printf("[%s]is%s[%s] ",
nroot->GetClass()->GetName(),
nroot->IsA(ks->FindClass("nobject"))?"\b":"not",
"nobject");
//3.nobjectisanroot?
n_printf("[%s]is%s[%s] ",
nobject->GetClass()->GetName(),
nobject->IsA("nroot")?"\b":"not",
"nroot");
//4.ntimerserverisanroot?
n_printf("[%s]is%s[%s] ",
ntimeserver->GetClass()->GetName(),
ntimeserver->IsA("nroot")?"\b":"not",
"nroot");
//5.nrootisinstanceofnroot?
n_printf("[%s]is%sinstanceof[%s] ",
nroot->GetClass()->GetName(),
nroot->IsInstanceOf(ks->FindClass("nroot"))?"\b":"not",
"nroot");
//6.nrootisinstanceofnobject?
n_printf("[%s]is%sinstanceof[%s] ",
nroot->GetClass()->GetName(),
nroot->IsInstanceOf(ks->FindClass("nobject"))?"\b":"not",
"nobject");
//7.nfileserver2isanobject?
n_printf("[%s]is%s[%s] ",
nfileserver2->GetClass()->GetName(),
nfileserver2->IsA("nobject")?"\b":"not",
"nobject");
//8.nfileserver2isantimeserver?
n_printf("[%s]is%s[%s] ",
nfileserver2->GetClass()->GetName(),
nfileserver2->IsA("ntimeserver")?"\b":"not",
"ntimeserver");
///+Nebula2RTTITest
///----------------------------------------------------------------------------
///销毁KernelServer
n_delete(ks);
getchar();
return0;
}
///-Application
///----------------------------------------------------------------------------
本文通过示例代码介绍了Nebula2中RTTI(运行时类型识别)的实现方式,主要依赖于nClass和nObject,演示了如何使用IsA()和IsInstanceOf()方法来判断类的继承关系及对象的实例关系。
332

被折叠的 条评论
为什么被折叠?



