len3d

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
/*----------------------------------------------------------------------------------------------------
    DESCRIPTION :
        The following code is used to instead C++ virtual function mechanism for efficiency.
        "DECLARE_TYPE" is to be put in base class definitions, the "type" here is used to
        determine the type of derived classes.
        "VIRTUAL" is supposed to instead the "virtual" keyword so that the mechanism presented
        here can work properly without too many changes in the source code.
        "VOID_VIRTUAL_BEGIN" is used to define a virtual function in base classes, the
        "_funcname" must be written with full type, scope and parameter list. What's more,
        a "FUNCCALL" macro must be defined as a call to the derived classes' functions in the
        right form, and after declaring the virtual function, you can #undef it to keep clean.
        "VOID_DERIVED_CLASS" should be placed below the implementation of the base classes'
        virtual functions, and all the derived classes must be included even the derived
        classes of its directly derived classes and their inherited classes and etc, the
        "_class" here is the name of its derived class, and "_enum" is the idiographic value
        of "type" as mentioned above. "VOID_VIRTUAL_END" ends the function definition.
        similarly, "VIRTUAL_BEGIN", "DERIVED_CLASS" and "VIRTUAL_END" are used to define
        a virtual function in base classses but with value return.
        When debugging, the code will use standard C++ virtual function mechanism for ease.
        Spread it free. Len3d. Jan 15, 2006.
----------------------------------------------------------------------------------------------------
*/

#define DECLARE_TYPE(_datatype)                    _datatype    type;
#ifdef _DEBUG
#define VIRTUAL                                    virtual
#define VOID_VIRTUAL_BEGIN(_funcname)            void _funcname {
#define VOID_DERIVED_CLASS(_class,_enum)
#define VOID_VIRTUAL_END                        }
#define VIRTUAL_BEGIN(_funcdef)                    _funcdef {
#define DERIVED_CLASS(_class,_enum)
#define VIRTUAL_END                                }
#else
#define VIRTUAL                                    inline
#define VOID_VIRTUAL_BEGIN(_funcname)            void _funcname { switch(type) { default:
#define VOID_DERIVED_CLASS(_class,_enum)        break; case _enum: ((_class*)this)->##FUNCCALL;
#define VOID_VIRTUAL_END                        break; } }
#define VIRTUAL_BEGIN(_funcdef)                    _funcdef { switch(type) { default:
#define DERIVED_CLASS(_class,_enum)                break; case _enum: return ((_class*)this)->##FUNCCALL;
#define VIRTUAL_END                                } }
#endif
/*----------------------------------------------------------------------------------------------------
    EXAMPLE :
class MyPeople {
public:
    MyPeople() {    type = 0;    }
    virtual ~MyPeople() {}
    VIRTUAL    int    func(int);
public:
    DECLARE_TYPE( int )
};
class MyMan : public MyPeople {
public:
    MyMan() {    type = 1;    }
    virtual ~MyMan() {}
    VIRTUAL int    func(int);
};
class MyWorker : public MyMan {
public:
    MyWorker() {    type = 2;    }
    virtual ~MyWorker() {}
    VIRTUAL int    func(int);
};
#define FUNCCALL    func(i)
VIRTUAL_BEGIN( int MyPeople::func(int i) )
    return i + 3;
    DERIVED_CLASS( MyMan, 1 )
    DERIVED_CLASS( MyWorker, 2 )
VIRTUAL_END
#undef FUNCCALL
#define FUNCCALL    func(i)
VIRTUAL_BEGIN( int MyMan::func(int i) )
    return i - 3;
    DERIVED_CLASS( MyWorker, 2 )
VIRTUAL_END
#undef FUNCCALL
int MyWorker::func(int i) {
    return i * 3;
}
int main(int argc, _TCHAR* argv[]) {
    MyPeople    *p1 = new MyPeople;
    MyPeople    *p2 = new MyMan;
    MyPeople    *p3 = new MyWorker;
    MyMan        *p4 = new MyWorker;
    printf( "%d\n", p1->func(5) );
    printf( "%d\n", p2->func(5) );
    printf( "%d\n", p3->func(5) );
    printf( "%d\n", p4->func(5) );
    getch();
    delete p1;
    delete p2;
    delete p3;
    delete p4;
    return 0;
}
----------------------------------------------------------------------------------------------------
*/
posted on 2006-01-27 15:39  Len3d  阅读(317)  评论(0编辑  收藏  举报