大家好,今天读了一段代码,读了半天也没读明白,所以在这里想请教大家帮忙把以下的宏展开一下:现在有如下宏定义于一个.h文件中:#define ConsoleType( typeName, type, size ) \
class ConsoleType##type : public ConsoleBaseType \
{ \
public: \
ConsoleType##type (const S32 aSize, S32 *idPtr, const char *aTypeName) : ConsoleBaseType(aSize, idPtr, aTypeName) { } \
virtual void setData(void *dptr, S32 argc, const char **argv, EnumTable *tbl, BitSet32 flag); \
virtual const char *getData(void *dptr, EnumTable *tbl, BitSet32 flag ); \
virtual const char *getTypeClassName() { return #typeName ; } \
}; \
S32 type = -1; \
ConsoleType##type gConsoleType##type##Instance(size,&type,#type); \#define ConsolePrepType( typeName, type, size ) \
class ConsoleType##type : public ConsoleBaseType \
{ \
public: \
ConsoleType##type (const S32 aSize, S32 *idPtr, const char *aTypeName) : ConsoleBaseType(aSize, idPtr, aTypeName) { } \
virtual void setData(void *dptr, S32 argc, const char **argv, EnumTable *tbl, BitSet32 flag); \
virtual const char *getData(void *dptr, EnumTable *tbl, BitSet32 flag ); \
virtual const char *getTypeClassName() { return #typeName; }; \
virtual const char *prepData(const char *data, char *buffer, U32 bufferLen); \
}; \
S32 type = -1; \
ConsoleType##type gConsoleType##type##Instance(size,&type,#type); \#define ConsoleSetType( type ) \
void ConsoleType##type::setData(void *dptr, S32 argc, const char **argv, EnumTable *tbl, BitSet32 flag)#define ConsoleGetType( type ) \
const char *ConsoleType##type::getData(void *dptr, EnumTable *tbl, BitSet32 flag)#define ConsoleProcessData( type ) \
const char *ConsoleType##type::prepData(const char *data, char *buffer, U32 bufferSz)#define DatablockConsoleType( typeName, type, size, className ) \
class ConsoleType##type : public ConsoleBaseType \
{ \
public: \
ConsoleType##type (const S32 aSize, S32 *idPtr, const char *aTypeName) : ConsoleBaseType(aSize, idPtr, aTypeName) { } \
virtual void setData(void *dptr, S32 argc, const char **argv, EnumTable *tbl, BitSet32 flag); \
virtual const char *getData(void *dptr, EnumTable *tbl, BitSet32 flag ); \
virtual const char *getTypeClassName() { return #className; }; \
virtual const bool isDatablock() { return true; }; \
}; \
S32 type = -1; \
ConsoleType##type gConsoleType##type##Instance(size,&type,#type); \//////////////////////////////////////在一个.cpp代码中有如下的语句:
(1)
ConsoleType( bool, TypeBool, sizeof(bool) )(2)
ConsoleType( float, TypeF32, sizeof(typedef float) )我想请教各位的是以上两条语句展开后应该是个什么结果,由于我对##和#的理解都不深,所以还请各位帮忙!
class ConsoleType##type : public ConsoleBaseType \
{ \
public: \
ConsoleType##type (const S32 aSize, S32 *idPtr, const char *aTypeName) : ConsoleBaseType(aSize, idPtr, aTypeName) { } \
virtual void setData(void *dptr, S32 argc, const char **argv, EnumTable *tbl, BitSet32 flag); \
virtual const char *getData(void *dptr, EnumTable *tbl, BitSet32 flag ); \
virtual const char *getTypeClassName() { return #typeName ; } \
}; \
S32 type = -1; \
ConsoleType##type gConsoleType##type##Instance(size,&type,#type); \#define ConsolePrepType( typeName, type, size ) \
class ConsoleType##type : public ConsoleBaseType \
{ \
public: \
ConsoleType##type (const S32 aSize, S32 *idPtr, const char *aTypeName) : ConsoleBaseType(aSize, idPtr, aTypeName) { } \
virtual void setData(void *dptr, S32 argc, const char **argv, EnumTable *tbl, BitSet32 flag); \
virtual const char *getData(void *dptr, EnumTable *tbl, BitSet32 flag ); \
virtual const char *getTypeClassName() { return #typeName; }; \
virtual const char *prepData(const char *data, char *buffer, U32 bufferLen); \
}; \
S32 type = -1; \
ConsoleType##type gConsoleType##type##Instance(size,&type,#type); \#define ConsoleSetType( type ) \
void ConsoleType##type::setData(void *dptr, S32 argc, const char **argv, EnumTable *tbl, BitSet32 flag)#define ConsoleGetType( type ) \
const char *ConsoleType##type::getData(void *dptr, EnumTable *tbl, BitSet32 flag)#define ConsoleProcessData( type ) \
const char *ConsoleType##type::prepData(const char *data, char *buffer, U32 bufferSz)#define DatablockConsoleType( typeName, type, size, className ) \
class ConsoleType##type : public ConsoleBaseType \
{ \
public: \
ConsoleType##type (const S32 aSize, S32 *idPtr, const char *aTypeName) : ConsoleBaseType(aSize, idPtr, aTypeName) { } \
virtual void setData(void *dptr, S32 argc, const char **argv, EnumTable *tbl, BitSet32 flag); \
virtual const char *getData(void *dptr, EnumTable *tbl, BitSet32 flag ); \
virtual const char *getTypeClassName() { return #className; }; \
virtual const bool isDatablock() { return true; }; \
}; \
S32 type = -1; \
ConsoleType##type gConsoleType##type##Instance(size,&type,#type); \//////////////////////////////////////在一个.cpp代码中有如下的语句:
(1)
ConsoleType( bool, TypeBool, sizeof(bool) )(2)
ConsoleType( float, TypeF32, sizeof(typedef float) )我想请教各位的是以上两条语句展开后应该是个什么结果,由于我对##和#的理解都不深,所以还请各位帮忙!
#define MACRO1( x ) macro##x
那么
MACRO1( abcd )
就相当于
macroabcd#define MACRO2( x ) #x
那么
MACRO2( abcd )
就相当于
"abcd"
根据上面所说,你例子种的几个宏就是定义并实现一个类,名字根据typeName而定其实完全可以用模板