MFC的一些宏的整理 (DECLARE_DYNCREATE/IMPLEMENT_DYNCREATE)_vs如何使用implement_dyncreate(cdib, cobject)时,无法生成dll实-程序员宅基地

技术标签: MFC  

 

代码实现

(注:以下宏及其实现取自MFC)

  • DECLARE_DYNAMIC
Define:
#define DECLARE_DYNAMIC(class_name) "
public: "
    static const AFX_DATA CRuntimeClass class##class_name; "
    virtual CRuntimeClass* GetRuntimeClass() const; "

E.g.
DECLARE_DYNAMIC(RenderView)
(注:RenderView是继承于MFC中CFormView的一个类)

Equals:

public:
    static const AFX_DATA CRuntimeClass classRenderView;
    virtual CRuntimeClass* GetRuntimeClass() const;

即declare了一个static的CRuntimeClass变量和一个虚拟函数GetRuntimeClass()

关于CRuntimeClass,其declaration:

struct CRuntimeClass
{
// Attributes
    LPCSTR m_lpszClassName;
    int m_nObjectSize;
    UINT m_wSchema; // schema number of the loaded class
    CObject* (PASCAL* m_pfnCreateObject)(); // NULL => abstract class
#ifdef _AFXDLL
    CRuntimeClass* (PASCAL* m_pfnGetBaseClass)();
#else
    CRuntimeClass* m_pBaseClass;
#endif

// Operations
    CObject* CreateObject();
    BOOL IsDerivedFrom(const CRuntimeClass* pBaseClass) const;

// Implementation
    void Store(CArchive& ar) const;
    static CRuntimeClass* PASCAL Load(CArchive& ar, UINT* pwSchemaNum);

    // CRuntimeClass objects linked together in simple list
    CRuntimeClass* m_pNextClass;       // linked list of registered classes
};

结构体,6个成员:
m_lpszClassName 类名字
m_nObjectSize 对象大小
m_wSchema schema
m_pfnCreateObject 函数指针 (对象创建方法)
m_pBaseClass/m_pfnGetBaseClass 指向基类对象的指针/获取基类对象函数的指针 (Runtime的关键)
m_pNextClass 指向下一个此类对象
  • DECLARE_DYNCREATE
Define:
// not serializable, but dynamically constructable
#define DECLARE_DYNCREATE(class_name) "
    DECLARE_DYNAMIC(class_name) "
    static CObject* PASCAL CreateObject();


E.g.
DECLARE_DYNCREATE(RenderView)

Equals:

public:
    static const AFX_DATA CRuntimeClass classRenderView;
    virtual CRuntimeClass* GetRuntimeClass() const;
    static CObject* PASCAL CreateObject();

即declare了一个static的CRuntimeClass变量和一个虚拟函数GetRuntimeClass()和一个static的函数CreateObject()

关于CObject,其declaration:

#ifdef _AFXDLL
class CObject
#else
class AFX_NOVTABLE CObject
#endif
{
public:

// Object model (types, destruction, allocation)
    virtual CRuntimeClass* GetRuntimeClass() const;
    virtual ~CObject();  // virtual destructors are necessary

    // Diagnostic allocations
    void* PASCAL operator new(size_t nSize);
    void* PASCAL operator new(size_t, void* p);
    void PASCAL operator delete(void* p);
#if _MSC_VER >= 1200
    void PASCAL operator delete(void* p, void* pPlace);
#endif

#if defined(_DEBUG) && !defined(_AFX_NO_DEBUG_CRT)
    // for file name/line number tracking using DEBUG_NEW
    void* PASCAL operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
#if _MSC_VER >= 1200
    void PASCAL operator delete(void *p, LPCSTR lpszFileName, int nLine);
#endif
#endif

    // Disable the copy constructor and assignment by default so you will get
    //   compiler errors instead of unexpected behaviour if you pass objects
    //   by value or assign objects.
protected:
    CObject();
private:
    CObject(const CObject& objectSrc);              // no implementation
    void operator=(const CObject& objectSrc);       // no implementation

// Attributes
public:
    BOOL IsSerializable() const;
    BOOL IsKindOf(const CRuntimeClass* pClass) const;

// Overridables
    virtual void Serialize(CArchive& ar);

#if defined(_DEBUG) || defined(_AFXDLL)
    // Diagnostic Support
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
#endif

// Implementation
public:
    static const AFX_DATA CRuntimeClass classCObject;
#ifdef _AFXDLL
    static CRuntimeClass* PASCAL _GetBaseClass();
#endif
};

包含:GetRuntimeClass()方法,static变量 CRuntimeClass classCObject,static方法 _GetBaseClass() (为NULL,因为没有Base),IsKindOf()方法等.
  • RUNTIME_CLASS
#define RUNTIME_CLASS(class_name) ((CRuntimeClass*)(&class_name::class##class_name))

E.g.
RUNTIME_CLASS(RenderView)

Equals:
((CRuntimeClass*)(&RenderView::classRenderView))
即将classRenderView static变量转换成((CRuntimeClass*)指针

  • IMPLEMENT_RUNTIMECLASS
Define:
#define IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, wSchema, pfnNew) "
    AFX_COMDAT const AFX_DATADEF CRuntimeClass class_name::class##class_name = { "
        #class_name, sizeof(class class_name), wSchema, pfnNew, "
            RUNTIME_CLASS(base_class_name), NULL }; "
    CRuntimeClass* class_name::GetRuntimeClass() const "
        { return RUNTIME_CLASS(class_name); } "

E.g.
IMPLEMENT_RUNTIMECLASS(RenderView, CFormView, 0xFFFF, RenderView::CreateObject)

Equals:
AFX_COMDAT const AFX_DATADEF CRuntimeClass RenderView::classRenderView = {
        #RenderView, sizeof(class RenderView), 0xFFFF, RenderView::CreateObject,
            ((CRuntimeClass*)(&CFormView::classCFormView)), NULL };

    CRuntimeClass* RenderView::GetRuntimeClass() const
        { return ((CRuntimeClass*)(&RenderView::classRenderView)); }


(##为连接文本, #RenderView为取RenderView字符串)

即implement了static classRenderView变量和GetRuntimeClass()虚拟函数
  • IMPLEMENT_DYNCREATE
Define:

#define IMPLEMENT_DYNCREATE(class_name, base_class_name) "
    CObject* PASCAL class_name::CreateObject() "
        { return new class_name; } "
    IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, "
        class_name::CreateObject)

E.g.
IMPLEMENT_DYNCREATE(RenderView, CFormView)

Equals:

    CObject* PASCAL RenderView::CreateObject()
        { return new RenderView; }

    AFX_COMDAT const AFX_DATADEF CRuntimeClass RenderView::classRenderView = {
        #RenderView, sizeof(class RenderView), 0xFFFF, RenderView::CreateObject,
            ((CRuntimeClass*)(&CFormView::classCFormView)), NULL };

    CRuntimeClass* RenderView::GetRuntimeClass() const
        { return ((CRuntimeClass*)(&RenderView::classRenderView)); }

即implement了static classRenderView变量和GetRuntimeClass()虚拟函数和CreateObject()函数.


用途

综合来看,这套宏的目的是在目标对象(比如RenderView)里面嵌套了一个CRuntimeClass对象,用来支持类似Runtime类型的查询转换等(用以支持MFC的RTTI?).

支持这些,有什么用呢?一个用处是DYNAMIC_DOWNCAST,即MFC里实现的对象指针在类层次上的从上到下转换:
E.g.
...
pCFormView* pView = ...
pRenderView* pRenderView = DYNAMIC_DOWNCAST(RenderView, pView)
...

其实现如下:
CObject* AFX_CDECL AfxDynamicDownCast(CRuntimeClass* pClass, CObject* pObject)
{
    if (pObject != NULL && pObject->IsKindOf(pClass))
        return pObject;
    else
        return NULL;
}

BOOL CObject::IsKindOf(const CRuntimeClass* pClass) const
{
    ASSERT(this != NULL);
    // it better be in valid memory, at least for CObject size
    ASSERT(AfxIsValidAddress(this, sizeof(CObject)));

    // simple SI case
    CRuntimeClass* pClassThis = GetRuntimeClass();
    return pClassThis->IsDerivedFrom(pClass);
}

BOOL CRuntimeClass::IsDerivedFrom(const CRuntimeClass* pBaseClass) const
{
    ASSERT(this != NULL);
    ASSERT(AfxIsValidAddress(this, sizeof(CRuntimeClass), FALSE));
    ASSERT(pBaseClass != NULL);
    ASSERT(AfxIsValidAddress(pBaseClass, sizeof(CRuntimeClass), FALSE));

    // simple SI case
    const CRuntimeClass* pClassThis = this;
    while (pClassThis != NULL)
    {
        if (pClassThis == pBaseClass)
            return TRUE;
#ifdef _AFXDLL
        pClassThis = (*pClassThis->m_pfnGetBaseClass)();
#else
        pClassThis = pClassThis->m_pBaseClass;
#endif
    }
    return FALSE;       // walked to the top, no match
}

实现原理:RenderView继承自CFormView,后者又继承自CObject,它们本身又嵌套了static CRuntimeClass对象,那么查询一个指向CFormView对象的指针(pCFormView)是不是实际上就是一个指向RenderView对象的指针的功能是通过比较pCFormView指向的对象中的CRuntimeClass对象(或者其BaseRuntimeClass(或BaseRuntimeClass的BaseRuntimeClass...)对象)是不是就是(比较指针值)RenderView类所含的static CRuntimeClass对象(IsDerivedFrom方法)这么简单了?

如果对象一样(即指针值相等)的话则可以转换成功,否则失败.

(关键:嵌入的CRuntimeClass是静态的,可以通过类访问,又可以通过对象的非静态函数调用,这是实现的关键.因为继承于CObject层次上的每个类都有唯一的CRuntimeClass对象与之对应, 所以它可以成为类型的一个标识符,如果表示符一样了,那么肯定类型是一样的,而这个标识符既可以通过类访问又可以在运行时刻通过对象访问,所以取名CRuntimeClass.)

 

 

http://www.cnblogs.com/soroman/archive/2009/02/28/1400413.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wishfly/article/details/8691965

智能推荐

前端开发之vue-grid-layout的使用和实例-程序员宅基地

文章浏览阅读1.1w次,点赞7次,收藏34次。vue-grid-layout的使用、实例、遇到的问题和解决方案_vue-grid-layout

Power Apps-上传附件控件_powerapps点击按钮上传附件-程序员宅基地

文章浏览阅读218次。然后连接一个数据源,就会在下面自动产生一个添加附件的组件。把这个控件复制粘贴到页面里,就可以单独使用来上传了。插入一个“编辑”窗体。_powerapps点击按钮上传附件

C++ 面向对象(Object-Oriented)的特征 & 构造函数& 析构函数_"object(cnofd[\"ofdrender\"])十条"-程序员宅基地

文章浏览阅读264次。(1) Abstraction (抽象)(2) Polymorphism (多态)(3) Inheritance (继承)(4) Encapsulation (封装)_"object(cnofd[\"ofdrender\"])十条"

修改node_modules源码,并保存,使用patch-package打补丁,git提交代码后,所有人可以用到修改后的_修改 node_modules-程序员宅基地

文章浏览阅读133次。删除node_modules,重新npm install看是否成功。在 package.json 文件中的 scripts 中加入。修改你的第三方库的bug等。然后目录会多出一个目录文件。_修改 node_modules

【】kali--password:su的 Authentication failure问题,&sudo passwd root输入密码时Sorry, try again._password: su: authentication failure-程序员宅基地

文章浏览阅读883次。【代码】【】kali--password:su的 Authentication failure问题,&sudo passwd root输入密码时Sorry, try again._password: su: authentication failure

整理5个优秀的微信小程序开源项目_微信小程序开源模板-程序员宅基地

文章浏览阅读1w次,点赞13次,收藏97次。整理5个优秀的微信小程序开源项目。收集了微信小程序开发过程中会使用到的资料、问题以及第三方组件库。_微信小程序开源模板

随便推点

Centos7最简搭建NFS服务器_centos7 搭建nfs server-程序员宅基地

文章浏览阅读128次。Centos7最简搭建NFS服务器_centos7 搭建nfs server

Springboot整合Mybatis-Plus使用总结(mybatis 坑补充)_mybaitis-plus ruledataobjectattributemapper' and '-程序员宅基地

文章浏览阅读1.2k次,点赞2次,收藏3次。前言mybatis在持久层框架中还是比较火的,一般项目都是基于ssm。虽然mybatis可以直接在xml中通过SQL语句操作数据库,很是灵活。但正其操作都要通过SQL语句进行,就必须写大量的xml文件,很是麻烦。mybatis-plus就很好的解决了这个问题。..._mybaitis-plus ruledataobjectattributemapper' and 'com.picc.rule.management.d

EECE 1080C / Programming for ECESummer 2022 Laboratory 4: Global Functions Practice_eece1080c-程序员宅基地

文章浏览阅读325次。EECE 1080C / Programming for ECESummer 2022Laboratory 4: Global Functions PracticePlagiarism will not be tolerated:Topics covered:function creation and call statements (emphasis on global functions)Objective:To practice program development b_eece1080c

洛谷p4777 【模板】扩展中国剩余定理-程序员宅基地

文章浏览阅读53次。被同机房早就1年前就学过的东西我现在才学,wtcl。设要求的数为\(x\)。设当前处理到第\(k\)个同余式,设\(M = LCM ^ {k - 1} _ {i - 1}\) ,前\(k - 1\)个的通解就是\(x + i * M\)。那么其实第\(k\)个来说,其实就是求一个\(y\)使得\(x + y * M ≡ a_k(mod b_k)\)转化一下就是\(y * M ...

android 退出应用没有走ondestory方法,[Android基础论]为何Activity退出之后,系统没有调用onDestroy方法?...-程序员宅基地

文章浏览阅读1.3k次。首先,问题是如何出现的?晚上复查代码,发现一个activity没有调用自己的ondestroy方法我表示非常的费解,于是我检查了下代码。发现再finish代码之后接了如下代码finish();System.exit(0);//这就是罪魁祸首为什么这样写会出现问题System.exit(0);////看一下函数的原型public static void exit (int code)//Added ..._android 手动杀死app,activity不执行ondestroy

SylixOS快问快答_select函数 导致堆栈溢出 sylixos-程序员宅基地

文章浏览阅读894次。Q: SylixOS 版权是什么形式, 是否分为<开发版税>和<运行时版税>.A: SylixOS 是开源并免费的操作系统, 支持 BSD/GPL 协议(GPL 版本暂未确定). 没有任何的运行时版税. 您可以用她来做任何 您喜欢做的项目. 也可以修改 SylixOS 的源代码, 不需要支付任何费用. 当然笔者希望您可以将使用 SylixOS 开发的项目 (不需要开源)或对 SylixOS 源码的修改及时告知笔者.需要指出: SylixOS 本身仅是笔者用来提升自己水平而开发的_select函数 导致堆栈溢出 sylixos

推荐文章

热门文章

相关标签