我的一个面向对象的C语言宏定义系统

参考了网上很多人的代码,不一一列举了

/* ========================================================================  */
/* 这一区块定义下面用到的一些名字组成宏 */
/* 类方法名字拼接 */
#define METHOD(class, method)               class##_##method
/* 类方法的类型的名字拼接 */
#define METHOD_TYPE(class, method)          __##class##_##method##_typedef__
/* 类的虚函数表结构的名字拼接 */
#define VTBL_STRU_NAME(class)               __##class##_v_func_tbl__
/* 类的虚函数在虚函数表中的位置的名字拼接 */
#define VTBL_FUNC_PTR_NAME(class, func)     __##class##_fp_##func##_offset__
/* 类的虚函数表实体的名字拼接 */
#define VTBL_ENTITY_NAME(class)             __##class##_vtable_array__
/* 类的虚函数表实体初始化函数的名字拼接 */
#define VTBL_ENTITY_INIT_METHOD(class)      __##class##_class_vtable_init__
/* 类的虚函数表实体是否初始化OK的名字拼接 */
#define VTBL_ENTITY_IS_INIT_FLAG(class)     __##class##_class_vtable_is_init__
/* 类的虚函数表实体初始化结构变量的名字拼接 */
#define VTBL_ENTITY_INIT_STRU_VAR(class)    class##_class_vtable_init_info
/* 获取类的虚函数表初始化函数的指针 */
#define VTBL_ENTITY_INIT_METHOD_PTR(class)  ( VTBL_ENTITY_INIT_STRU_VAR(class) . vtable_init_method)
/* ========================================================================  */

/* ========================================================================  */
/* 创建接口 */
#define new(class)                   METHOD(class, create)()
/* 释放接口 */
#define delete(class, object)        METHOD(class, destory)(object)
/* ========================================================================  */

/* ========================================================================  */
/* 类声明的开始宏 */
#define BEGIN_CLASS(class)                                          \
    struct class;                                                   \
    typedef struct class class;                                     \
    struct VTBL_STRU_NAME(class);                                   \
    extern class_vtable_init VTBL_ENTITY_INIT_STRU_VAR(class);      \
    class * METHOD(class, create)(void);                            \
    int  METHOD(class, constructor)(class* this);                   \
    void METHOD(class, destory)(class* this);                       \
    int  METHOD(class, destructor)(class* this);                    \
    struct class

/* 继承自父类的声明 */
#define INHERITED_PARENT(p_class)                           \
        p_class parent

/* 类中的虚函数声明开始宏 */
#define BEGIN_V_METHOD_DEFINE(class)                        \
        struct VTBL_STRU_NAME(class)

/* 类继承自父类的虚函数信息 */
#define INHERITED_V_METHOD(p_class)                         \
            struct VTBL_STRU_NAME(p_class) parent_vtbl

/* 单个虚函数声明处 */
#define DEFINE_V_METHOD(ret_type, method)                   \
            ret_type (* method )(void* this, ...)

/* 类中的虚函数声明结束宏 */
#define END_V_METHOD_DEFINE(class)

/* 根类的对象声明 */
#define ROOT_CLASS_DECLARE(class)                           \
    struct VTBL_STRU_NAME(class) *vbtl;

/* 类中的声明结束宏 */
#define END_CLASS(class)
/* ========================================================================  */

/* 虚拟函数指针的使用 */
#define GET_V_METHOD_PTR(class, func)                                       \
        ( VTBL_FUNC_PTR_NAME(class, func) )

/* 定义虚拟函数的类型 */
#define DEFINE_V_METHOD_TYPE(ret_type, class, method)                       \
    typedef ret_type (* METHOD_TYPE(class, method) )

/* 定义虚拟函数的指针 */
#define DEFINE_V_METHOD_PTR(class, method)                                  \
    enum { VTBL_FUNC_PTR_NAME(class, method) =                              \
        (((int )(&((struct VTBL_STRU_NAME(class) *)0)-> method ))           \
                / sizeof(std_func_ptr)) + 1 };                              \

/* 定义继承的虚拟函数的指针 */
#define INHERITED_V_METHOD_PTR(class, parent, method)                       \
    enum { VTBL_FUNC_PTR_NAME(class, method) =                              \
                VTBL_FUNC_PTR_NAME(parent, method) };                       \

/* 定义普通的虚拟函数的信息 */
#define DEFINE_V_METHOD_INFO(ret_type, class, method)                       \
    DEFINE_V_METHOD_PTR(class, method);                                     \
    DEFINE_V_METHOD_TYPE(ret_type, class, method)

/* 定义继承的虚拟函数的信息 */
#define INHERITED_V_METHOD_INFO(ret_type, class, parent, method)            \
    INHERITED_V_METHOD_PTR(class, parent, method);                          \
    DEFINE_V_METHOD_TYPE(ret_type, class, method)

/* 定义普通的成员函数的信息 */
#define DEFINE_C_METHOD_INFO(ret_type, class, method)                       \
    extern ret_type METHOD(class, method)
/*
#define DEFINE_C_METHOD_INFO(ret_type, class, method)                       \
    extern common_func_ptr __##class##_##method##_ptr__;                    \
    typedef ret_type (* VTBL_FUNC_PTR_NAME(class, func) )
*/

/* 调用对象的虚拟函数 */
#define CALL_V_METHOD(object, class, method)                                \
    (*(((*( METHOD_TYPE(class, method) **) object ))[ GET_V_METHOD_PTR(class,method) - 1 ]))

/* 调用对象的普通成员函数 */
#define CALL_C_METHOD(object, class, method)                                \
            METHOD(class, method)
/*
#define DECLARE_V_METHOD(ret_type, class, method)                           \
    ret_type METHOD(class, method)

#define DECLARE_C_METHOD(ret_type, class, method)                           \
    common_func_ptr __##class##_##method##_ptr__ = METHOD(class, method);   \
    ret_type METHOD(class, method)

#define CALL_C_METHOD(ret_type, class, method)                              \
    *(VTBL_FUNC_PTR_NAME(class, method)__##class##_##method##_ptr__)
*/

/* ========================================================================  */
/* 实现类,主要是绑定类的虚拟函数指针 */
#define BEGIN_DECLARE_CLASS_VTABLE(class)                                   \
    static std_func_ptr VTBL_ENTITY_NAME(class) [                           \
        sizeof(struct VTBL_STRU_NAME(class) )/sizeof(std_func_ptr)] = {0};  \
    static char VTBL_ENTITY_IS_INIT_FLAG(class) = 0;                        \
    static int VTBL_ENTITY_INIT_METHOD(class) (std_func_ptr *vtbl)          \
    {                                                                       \
        if (0 != VTBL_ENTITY_IS_INIT_FLAG(class) ) return 1;                \
        if (0 == vtbl) return 0;                                            \
        do

/* 继承父类的VTABLE */
#define INHERITED_VTABLE_INIT(parent)                                       \
        if (0 == VTBL_ENTITY_INIT_METHOD_PTR(parent) (vtbl)) return 0

/* 继承的虚函数表项初始化 */
#define INHERITED_VTABLE_METHOD_INIT(class, parent, method)                 \
        vtbl[((int )(&((struct VTBL_STRU_NAME(parent) * )0)-> method ))     \
                    / sizeof(std_func_ptr)]                                 \
            = (std_func_ptr) METHOD(class, method)

/* 自身的虚函数表项初始化 */
#define SELF_VTABLE_METHOD_INIT(class, method)                              \
            INHERITED_VTABLE_METHOD_INIT(class, class, method)

/* 实现类的结束宏 */
#define END_DECLARE_CLASS_VTABLE(class)                                     \
        while(0);                                                           \
        VTBL_ENTITY_IS_INIT_FLAG(class) = 1;                                \
        return 1;                                                           \
    }                                                                       \
    class_vtable_init VTBL_ENTITY_INIT_STRU_VAR(class) =                    \
              {VTBL_ENTITY_INIT_METHOD(class), VTBL_ENTITY_NAME(class)};

/* ========================================================================  */

/* ========================================================================  */
/* 类的构造函数,主要提供两个接口:
   一个是create,以支持new,
   一个是constructor,就是构造并初始化的函数 */
#define BEGIN_CONSTRUCTOR(class, parent)                                    \
    extern int METHOD(class, constructor) (class * this);                   \
    class* METHOD(class, create) (void)                                     \
    {                                                                       \
        return common_create_object(sizeof( class ),                        \
            (std_func_ptr) METHOD(class, constructor));                     \
    }                                                                       \
                                                                            \
    int METHOD(class, constructor) (class * this)                           \
    {                                                                       \
        VTBL_ENTITY_INIT_METHOD(class) ( VTBL_ENTITY_NAME(class) );         \
        if (0 != METHOD(parent, constructor) (( parent *)(this)))           \
            return 1;                                                       \
        *(struct  VTBL_STRU_NAME(class) **)this =                           \
            (struct VTBL_STRU_NAME(class) *) VTBL_ENTITY_NAME(class) ;      \
        do                                                                  \

/* 类构造函数的结束宏 */
#define END_CONSTRUCTOR(class, parent)                                      \
        while(0);                                                           \
        return 0;                                                           \
    }
/* 根类的父类构造函数为0 */
#define void_constructor(this)          (0)
/* ========================================================================  */

/* ========================================================================  */
/* 类的析构函数,主要提供两个接口:
    一个是destory,以支持delete,
    一个是destructor,就是析构函数 */
#define BEGIN_DESTRUCTOR(class, parent)                                     \
    extern int METHOD(class, destructor) (class * this);                    \
    void METHOD(class, destory) ( class * this)                             \
    {                                                                       \
        common_destory_object( this ,                                       \
            (std_func_ptr) METHOD(class, destructor) );                     \
    }                                                                       \
                                                                            \
    int METHOD(class, destructor) (class * this)                            \
    {                                                                       \
        do                                                                  \

/* 类析构函数的结束宏 */
#define END_DESTRUCTOR(class, parent)                                       \
        while(0);                                                           \
        if (0 != METHOD(parent, destructor) (( parent *)(this)))            \
            return 1;                                                       \
        return 0;                                                           \
    }
/* 根类的父类析构函数为0 */
#define void_destructor(this)          (0)

/* ========================================================================  */

typedef int (*std_func_ptr)(void*);
typedef int (*common_func_ptr)(void*, ...);

typedef struct
{
    int (*vtable_init_method)(std_func_ptr*); 
    std_func_ptr vtable_array;
}class_vtable_init;

void* common_create_object(unsigned int size, std_func_ptr pCtor);
void  common_destory_object(void* this, std_func_ptr pDtor);
void  common_destory_object_non_virtual(void* this, std_func_ptr pDtor);

/* Null类,应该为所有类的虚根 */
BEGIN_CLASS(NullClass)
{
    /* 类自身的数据 */
    ROOT_CLASS_DECLARE(NullClass)
    /* 虚拟函数表的定义处 */
    BEGIN_V_METHOD_DEFINE(NullClass)
    {
        /* 自身的虚函数 */
        DEFINE_V_METHOD(int, destructor);
    }
    END_V_METHOD_DEFINE(NullClass);
}
END_CLASS(NullClass);

DEFINE_V_METHOD_INFO(int , NullClass, destructor) (NullClass* this);

/* 测试的第一个类 */
BEGIN_CLASS(TestClass)
{
    /* 继承自NullClass */
    INHERITED_PARENT(NullClass);

    /* 虚拟函数表的定义处 */
    BEGIN_V_METHOD_DEFINE(TestClass)
    {
        /* 继承自NullClass */
        INHERITED_V_METHOD(NullClass);
        /* 自身的虚函数 */
        DEFINE_V_METHOD(int, open) ;
        DEFINE_V_METHOD(void, close);
    }
    END_V_METHOD_DEFINE(TestClass);

    /* 类自身的数据 */
    int x_pThis;
}
END_CLASS(TestClass);

INHERITED_V_METHOD_INFO(int , TestClass, NullClass, destructor) (TestClass* this);
DEFINE_V_METHOD_INFO(int,  TestClass, open) (TestClass* this, char* filename);
DEFINE_V_METHOD_INFO(void, TestClass, close) (TestClass* this,  int handle);

/* 继承上一个类 */
BEGIN_CLASS(InheritClass)
{
    /* TestClass */
    INHERITED_PARENT(TestClass);

    /* 虚拟函数表的定义处 */
    BEGIN_V_METHOD_DEFINE(InheritClass)
    {
        /* 继承自TestClass */
        INHERITED_V_METHOD(TestClass);
        /* 自身的虚函数 */
        DEFINE_V_METHOD(int, seek) ;
    }
    END_V_METHOD_DEFINE(InheritClass);

    /* 类自身的数据 */
    int x_nOffset;
}
END_CLASS(InheritClass);

INHERITED_V_METHOD_INFO(void, InheritClass, TestClass, destructor)(InheritClass* this);
INHERITED_V_METHOD_INFO(void, InheritClass, TestClass, close)(InheritClass* this,  int handle);
DEFINE_V_METHOD_INFO(int,     InheritClass, seek)(InheritClass* this, int handle, int offset);

 

#include "ooc.h"
#include <stdio.h>
#include <stdlib.h>

void* common_create_object(unsigned int size, std_func_ptr pCtor)
{
    /* 分配对象 */
    void* object = malloc(size);
    /* 如果分配失败,返回0 */
    if (0 == object)
        return 0;
    /* 调用构造函数 */
    if (0 != (*pCtor)(object))
    {
        /* 构造函数执行失败,释放对象 */
        free(object);
        return 0;
    }
    /* 成功,返回对象 */
    return object;
}

void  common_destory_object(void* this, std_func_ptr pDtor)
{
    /* 如果对象为空 */
    if (0 == this)  return;
    /* 如果存在虚函数表指针 */
    if (0 != *(void**)this)
    {
        /* 调用虚函数表中的第一个函数指针,默认此函数为析构函数 */
        if (0 != (*((*(std_func_ptr**)this)[0]))((void*)this))
            return;
    }
    else
    {
        /* 不为空,则调用传入的析构函数指针 */
        if (0 != (*pDtor)(this))
            return;
    }
    /* 最后释放对象 */
    free(this);
}

void  common_destory_object_non_virtual(void* this, std_func_ptr pDtor)
{
    if (0 == this)  return;
    if (0 != (*pDtor)(this))
        return;
    /* free */
    free(this);
}

BEGIN_DECLARE_CLASS_VTABLE(NullClass)
{
    SELF_VTABLE_METHOD_INIT(NullClass, destructor);
}
END_DECLARE_CLASS_VTABLE(NullClass)

BEGIN_CONSTRUCTOR(NullClass, void)
{
    /* do something */
    printf("call NullClass_constructor\n");
}
END_CONSTRUCTOR(NullClass, void)

BEGIN_DESTRUCTOR(NullClass, void)
{
    /* do something */
    printf("call NullClass_destructor\n");
}
END_DESTRUCTOR(NullClass, void)

int  METHOD(TestClass, open) (TestClass* this, char* filename)
{
    printf("call TestClass_open\n");
    return 0;
}

void METHOD(TestClass, close) (TestClass* this, int handle)
{
    printf("call TestClass_close\n");
    return;
}

BEGIN_DECLARE_CLASS_VTABLE(TestClass)
{
    INHERITED_VTABLE_INIT(NullClass);
    
    INHERITED_VTABLE_METHOD_INIT(TestClass, NullClass, destructor);
    
    SELF_VTABLE_METHOD_INIT(TestClass, open);
    SELF_VTABLE_METHOD_INIT(TestClass, close);
}
END_DECLARE_CLASS_VTABLE(TestClass)

BEGIN_CONSTRUCTOR(TestClass, NullClass)
{
    /* do something */
    printf("call TestClass_constructor\n");
    this->x_pThis = 0;
}
END_CONSTRUCTOR(TestClass, NullClass)

BEGIN_DESTRUCTOR(TestClass, NullClass)
{
    /* do something */
    printf("call TestClass_destructor\n");
    this->x_pThis = 0;
}
END_DESTRUCTOR(TestClass, NullClass)

int METHOD(InheritClass, seek) (InheritClass* this, int handle, int offset)
{
    printf("call InheritClass_seek\n");
    return 0;
}

void METHOD(InheritClass, close) (InheritClass* this, int handle)
{
    printf("call InheritClass_close\n");
    return;
}

BEGIN_DECLARE_CLASS_VTABLE(InheritClass)
{
    INHERITED_VTABLE_INIT(TestClass);
    
    INHERITED_VTABLE_METHOD_INIT(InheritClass, NullClass, destructor);
    INHERITED_VTABLE_METHOD_INIT(InheritClass, TestClass, close);
    
    SELF_VTABLE_METHOD_INIT(InheritClass, seek);
}
END_DECLARE_CLASS_VTABLE(InheritClass)

BEGIN_CONSTRUCTOR(InheritClass, TestClass)
{
    /* do something */
    printf("call InheritClass_constructor\n");
}
END_CONSTRUCTOR(InheritClass, TestClass)

BEGIN_DESTRUCTOR(InheritClass, TestClass)
{
    /* do something */
    printf("call InheritClass_destructor\n");
}
END_DESTRUCTOR(InheritClass, TestClass)

int main()
{
    TestClass* pTest;
    TestClass* pTest2;

    pTest = new(TestClass);

    //(*(((TestClass_ops*)(pTest->parent.vbtl))->open))(pTest, "");
    CALL_V_METHOD(pTest, TestClass, open)(pTest, "");
    //(*(((TestClass_ops*)(pTest->parent.vbtl))->close))(pTest, 1);
    CALL_V_METHOD(pTest, TestClass, close)(pTest, 1);

    delete(TestClass, pTest);
    printf("==========================================\n");

    pTest2 = new(InheritClass);
    //(*(((TestClass_ops*)(pTest2->parent.vbtl))->close))(pTest2, 1);
    CALL_V_METHOD(pTest2, TestClass, close)(pTest2, 1);
    //or
    CALL_V_METHOD(pTest2, InheritClass, close)(pTest2, 1);
    delete(InheritClass, pTest2);

    return 0;
}

 

posted @ 2013-05-16 22:19  日月王  阅读(552)  评论(0编辑  收藏  举报