当前位置:网站首页>Tar source code analysis 9

Tar source code analysis 9

2022-07-04 06:37:00 Tao song remains the same

How to define classes in dynamic runtime , It's also very intuitive :

#include "util/tc_dyn_object.h"
#include <string.h>

namespace tars
{
/**********************************TC_DYN_RuntimeClass Definition ***********************************/

TC_DYN_RuntimeClass* TC_DYN_RuntimeClass::pFirstClass = NULL;

TC_DYN_Object* TC_DYN_RuntimeClass::createObject()
{
    if (m_pfnCreateObject == NULL)
    {
        return NULL;
    }

    TC_DYN_Object* pObject = (*m_pfnCreateObject)();
    {
        return pObject;
    }
}

TC_DYN_RuntimeClass* TC_DYN_RuntimeClass::load(const char *szClassName)
{
    TC_DYN_RuntimeClass* pClass;

    for (pClass = pFirstClass; pClass != NULL; pClass = pClass->m_pNextClass)
    {
        if (strcmp(szClassName, pClass->m_lpszClassName) == 0)
        return pClass;
    }

    return NULL;
}

/**********************************szTC_DYN_Object Definition ***********************************/

TC_DYN_RuntimeClass TC_DYN_Object::classTC_DYN_Object =
{
    (char*)"TC_DYN_Object", 
    sizeof(TC_DYN_Object), 
    NULL, 
    NULL, 
    NULL 
};

static TC_DYN_Init _init_TC_DYN_Object(&TC_DYN_Object::classTC_DYN_Object);

TC_DYN_RuntimeClass* TC_DYN_Object::GetRuntimeClass() const
{
    return &TC_DYN_Object::classTC_DYN_Object;
}

bool TC_DYN_Object::isKindOf(const TC_DYN_RuntimeClass* pClass) const
{
    TC_DYN_RuntimeClass* pClassThis = GetRuntimeClass();
    while (pClassThis != NULL)
    {
        if (pClassThis == pClass)
        {
            return true;
        }

        pClassThis = pClassThis->m_pBaseClass;
    }
    return false;
}

}

原网站

版权声明
本文为[Tao song remains the same]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207040633075893.html