Loading

C 语言面向对象宏实现

头文件

#include <stdio.h>
#include <stddef.h>

#define bool int
#define Bool bool
#define BOOL bool
#define BOOLEAN bool
#define true (1)
#define True (1)
#define TRUE (1)
#define false (0)
#define False (0)
#define FALSE (0)

#define OFFSETOF offsetof

#define IMPLEMENTS(_CLASS_) struct _CLASS_ _CLASS_;
#define EXTENDS(_CLASS_) struct _CLASS_ *_CLASS_;
#define IMPLEMENTS_WITH_NAME(_CLASS_, _NAME_) struct _CLASS_ _NAME_;
#define EXTENDS_WITH_NAME(_CLASS_, _NAME_) struct _CLASS_ *_NAME_;

/* declare and define an interface */
#define INTERFACE(_CLASS_)          \
    typedef struct _CLASS_ _CLASS_; \
    struct _CLASS_

/* declare and define an class */
#define CLASS(_CLASS_)                  \
    typedef struct _CLASS_ _CLASS_;     \
    void _CLASS_##_ctor(_CLASS_ *this); \
    bool _CLASS_##_dtor(_CLASS_ *this); \
    const _CLASS_ *malloc_##_CLASS_();  \
    void free_##_CLASS_(_CLASS_ *this); \
    struct _CLASS_

/* initial an class without finalizers */
#define INIT_CLASS(_CLASS_)                                      \
    const _CLASS_ *malloc_##_CLASS_()                            \
    {                                                            \
        struct _CLASS_ *this;                                    \
        this = (struct _CLASS_ *)malloc(sizeof(struct _CLASS_)); \
        if (!this)                                               \
        {                                                        \
            return 0;                                            \
        }                                                        \
        _CLASS_##_ctor(this);                                    \
        return this;                                             \
    };                                                           \
    void free_##_CLASS_(_CLASS_ *this)                           \
    {                                                            \
        if (_CLASS_##_dtor(this))                                \
        {                                                        \
            free(this);                                          \
        }                                                        \
    }                                                            \
    bool _CLASS_##_dtor(_CLASS_ *this) { return true; }          \
    void _CLASS_##_ctor(_CLASS_ *this)

/* initial an class with malloc method and define constructors */
#define INIT_CTOR_CLASS(_CLASS_)                                 \
    const _CLASS_ *malloc_##_CLASS_()                            \
    {                                                            \
        struct _CLASS_ *this;                                    \
        this = (struct _CLASS_ *)malloc(sizeof(struct _CLASS_)); \
        if (!this)                                               \
        {                                                        \
            return 0;                                            \
        }                                                        \
        _CLASS_##_ctor(this);                                    \
        return this;                                             \
    };                                                           \
    void _CLASS_##_ctor(_CLASS_ *this)

/* initial an class with free method and define finalizers */
#define INIT_DTOR_CLASS(_CLASS_)       \
    void free_##_CLASS_(_CLASS_ *this) \
    {                                  \
        if (_CLASS_##_dtor(this))      \
        {                              \
            free(this);                \
        }                              \
    }                                  \
    bool _CLASS_##_dtor(_CLASS_ *this)

/* build an name of method for class */
#define DEFINE_CLASS_FUNCTION(_CLASS_, _NAME_) _CLASS_##_##_NAME_

/* to set properties for constructors or finalizers */
#define SET(_NAME_, _VALUE_) this->_NAME_ = _VALUE_;

/* get an ptr of member in an object class */
#define MEMBER_PTR(_CLASS_, _MEMBER_) ((_MEMBER_ *)(&(_CLASS_->##_MEMBER_)))

/* from member ptr get son class ptr, warnning: the member cannot an ptr member */
#define FROM_MEMBER_PTR(_CLASS_, _MEMBER_, _MEMBER_PTR_) ((_CLASS_ *)((char *)_MEMBER_PTR_ - OFFSETOF(_CLASS_, _MEMBER_)))

#define MEMBER_FIELD(_CLASS_, _MEMBER_, _FIELD_) _CLASS_->_MEMBER._FIELD_

#define DEFINE_CLASS_FN DEFINE_CLASS_FUNCTION
#define DEFINE_CLASS_FUNC DEFINE_CLASS_FUNCTION
#define CLASS_FUNCTION DEFINE_CLASS_FUNCTION
#define CLASS_FN DEFINE_CLASS_FUNCTION
#define CLASS_FUNC DEFINE_CLASS_FUNCTION```

使用 Demo

#include "cs.h"

INTERFACE(animal)
{
    char *name;
    void (*say)();
};

CLASS(dog)
{
    IMPLEMENTS(animal);
    void (*run)();
};

void CLASS_FUNC(dog, say)();
void CLASS_FUNC(dog, run)();

CLASS(mov)
{
    void (*move)();
    int speed;
};

CLASS(cat)
{
    IMPLEMENTS(animal);
    void (*pa)();
    IMPLEMENTS_WITH_NAME(mov, moves);
};

void CLASS_FUNC(cat, pa)();


void CLASS_FUNC(mov, move)();

INIT_CLASS(dog)
{
    SET(animal.name, "dog");
    this->animal.say = CLASS_FUNC(dog, say);
    this->run = CLASS_FUNC(dog, run);
}

void CLASS_FUNC(dog, say)()
{
    printf("dog say.\n");
}

void CLASS_FUNC(dog, run)()
{
    printf("dog run.\n");
}

void CLASS_FUNC(mov, move)()
{
    printf("mov is moveing.\n");
}

void CLASS_FUNC(cat, pa)()
{
    printf("cat is pa.\n");
}

INIT_CLASS(mov)
{
    this->move = CLASS_FUNC(mov, move);
}

INIT_CLASS(cat)
{
    this->animal.name = "cat";
    this->animal.say = CLASS_FUNC(dog, say);
    this->pa = CLASS_FUNC(cat, pa);
    this->moves.move = CLASS_FUNC(mov, move);
    this->moves.speed = 100;
}


int main(int argc, char *argv[])
{
    cat *_cat = malloc_cat();

    _cat->animal.say();
    _cat->pa();
    _cat->moves.move();

    mov *_mov = (&_cat->moves);
    _mov->move();

    int offset = OFFSETOF(cat, moves);


    cat *cat1 = FROM_MEMBER_PTR(cat, moves, _mov);

    cat1->animal.say();
    cat1->pa();
    cat1->moves.move();

    return 1;
}
posted @ 2022-08-08 09:45  microestc  阅读(85)  评论(0编辑  收藏  举报