__typeid(Class)// 定位一个类的运行时刻类信息
DECLARE_CLASS(Class)// 声明一个类
IMPLEMENT_CLASS(Class, BaseClass)// 注册一个类
SObject * gCreateObject(LPCTSTR szClassName);
// 根据一个类的类名,创建该类的一个实例
const SRuntimeClass* SObject::GetRuntimeClass() const;
// 确定一个对象的RuntimeClass信息
BOOL SObject::IsKindOf(const SRuntimeClass *pRC) const;
// 判断一个对象是否是pRC描述的类或者其派生类的实例
#define __typeid(Class) (&Class::x_theRuntimeClass)
#define DECLARE_CLASS(Class)
const SRuntimeClass *Class::GetRuntimeClass() const;
static const SRuntimeClass x_theRuntimeClass;
static SObject *x_CreateObject();
#define IMPLEMENT_CLASS(Class, BaseClass)
const SRuntimeClass *Class::GetRuntimeClass() const
{
return __typeid(Class);
}
const SRuntimeClass Class::x_theRuntimeClass(
_T(#Class),
__typeid(BaseClass),
Class::x_CreateObject
);
SObject *Class::x_CreateObject()
{
return new Class;
}
const SRuntimeClass *x_lpRuntimeClassListHead = NULL;
SRuntimeClass::SRuntimeClass(
LPCTSTR szClassName,
const SRuntimeClass *lpBaseClass,
FNBUILDER fnCreator)
{
m_szClassName = szClassName;
m_lpBaseClass = lpBaseClass;
m_fnCreator = fnCreator;
m_lpPrevClass = x_lpRuntimeClassListHead;
x_lpRuntimeClassListHead = this;
}
SObject *gCreateObject(LPCTSTR szClassName)
{
const SRuntimeClass *pRC = x_lpRuntimeClassListHead;
for ( ; pRC; pRC = pRC->m_lpPrevClass)
if (!_tcscmp(szClassName, pRC->m_szClassName))
return pRC->fnCreateObject();
return NULL;
}
BOOL SObject::IsKindOf(const SRuntimeClass *pBaseClass) const
{
SRuntimeClass *pRC = this->GetRuntimeClass();
for ( ; pRC; pRC = pRC->m_lpBaseClass)
if (pRC == pBaseClass)
return TRUE;
return FALSE;
}