宏如何包含大括号 [英] how a macro can include a curly bracket

查看:372
本文介绍了宏如何包含大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将整个代码块简化为一系列宏语句,但是我不确定如何使宏实际包含大括号.我有一个名为entity的类和一个名为GetParams的成员函数,但是我想创建一个开始该函数的宏,以便随后可以在函数内编写以下宏.例如:

 #define DATA_LAYOUT_START(type){void type :: GetParams(ifstream& in){}
 #define DATA_LAYOUT_END(){}}  


然后我会为每种类型的参数制作一堆宏,以使它看起来像这样.


 DATA_LAYOUT_START(实体);
   DEFINE_FLOAT(m_floatExample);
   DEFINE_STRING(m_stringExample);
   DEFINE_INT(m_intExample);
   //  ...以及该特定类需要的其他任何变量.
DATA_LAYOUT_END()



所以我的问题是,是否可以用大括号将宏本身替换为大括号以启动函数?

感谢您的快速响应,每个类的实际功能最后应该看起来像这样.

  void 类型:: GetParams(ifstream&  in ){
     >>中的m_floatExample;
     in  .getline(m_stringExample, 100 ' ');
     >>中的m_intExample;
    //  ...等,除了每个类的变量不同.
} 


我想在这里使用宏的主要原因是,因此没有诱因来改变周围的功能. GetParams是从基类继承的函数,更改参数或返回值确实会使事情搞砸.

解决方案

如果提供了一段代码,它将对我有帮助您希望将其作为最终代码,并可能将其标记为粗体,以便我提供更具体的帮助.

通常,您可以使用预处理器执行几乎任何操作,另一个问题是是否应该执行.

如果您想将函数声明为MACRO的函数原型,可以执行以下操作:

(修复了下面的MACRO定义,最初是复制粘贴错误,其中两个宏都定义为DATA_LAYOUT_START)

DATA_LAYOUT_START(type) void type::GetParams(ifstream& in) {
DATA_LAYOUT_END() }



如果您希望宏扩展为多行,请在该行的最后一个字符结尾,并以\
继续 宏将在没有连续字符的第一行结束.
例如:

DATA_LAYOUT_START(type) {\
                          static int count = 0;\
                          count++;



MFC和ATL经常使用宏表.举一个很好的例子,在atlwin.h中查找BEGIN_MSG_MAP.此功能为具有MACRO表定义的ATL窗口实现Windows过程.


此外,宏只是编译时间助手",将它们视为温和的打字员",无论您放置什么内容在宏名称之后被视为文本.在您的示例中,您立即从柯立括号开始,就好像您认为这是define语法的一部分一样.并非如此,该宏的每个扩展都将包含Curley括号.

如Paul所说,有一个连续字符(\)使宏扩展为多行.终止宏定义的唯一一件事是没有延续的行,没有"end curley brace".

Windows/MFC BEGIN_MESSAGE_MAP/END_MESSAGE_MAP宏做类似的事情,在其他一些宏代码的周围有一个"being"和结束包装".您可能想看看这些宏是如何编写的.它们位于afxwin.h中(粘贴在下面),仅忽略消息映射的工作原理,这些包装宏的语法将有助于编写您的消息.

#define BEGIN_MESSAGE_MAP(theClass, baseClass) \
	PTM_WARNING_DISABLE \
	const AFX_MSGMAP* theClass::GetMessageMap() const \
		{ return GetThisMessageMap(); } \
	const AFX_MSGMAP* PASCAL theClass::GetThisMessageMap() \
	{ \
		typedef theClass ThisClass;						   \
		typedef baseClass TheBaseClass;					   \
		static const AFX_MSGMAP_ENTRY _messageEntries[] =  \
		{
#define END_MESSAGE_MAP() \
		{0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } \
	}; \
		static const AFX_MSGMAP messageMap = \
		{ &TheBaseClass::GetThisMessageMap, &_messageEntries[0] }; \
		return &messageMap; \
	}								  \


宏是邪恶的.不要那样做通常可以使用语言抽象而非宏来简化代码.

—SA


i wish to simplify a whole block of code, into a series of macro statements, but i''m unsure of how to make a macro actually include a curly bracket. I have class called entity and a member function called GetParams, but i want to make a macro that begins the function so that following macros can be written afterward within the function. for example:

#define DATA_LAYOUT_START(type) {  void type::GetParams(ifstream & in){  }
#define DATA_LAYOUT_END()   {  }  }


and then I''d make a bunch of macros for each type of parameter so that it would look something like this.


DATA_LAYOUT_START(Entity);
   DEFINE_FLOAT(m_floatExample);
   DEFINE_STRING(m_stringExample);
   DEFINE_INT(m_intExample);
   //... and whatever other variables i need for that specific class.
DATA_LAYOUT_END()



so my question is, is it possible to have the macro replace itself, literally, with a curly bracket to start the function?

EDIT: Thanks for the rapid response, the actual function for each class should look something like this in the end.

void type::GetParams(ifstream & in) {
    in >> m_floatExample;
    in.getline(m_stringExample, 100, ' ');
    in >> m_intExample;
    // ... etc, except those variables are different for each class.
}


the main reason i want to use a macro here, is so there is no tempation to change the functions around. GetParams is a function inherited from a base class, and changing the parameters or return could really mess things up.

解决方案

It would help me if you provided a piece of code that you would like to have as the final code, and possibly mark it as bold so I can give more specific help.

In general, you can do just about anything with the pre-processor, a different issue is whether you should or not.

If you wanted to declare a the function proto-type of your function as a MACRO you can do this:

(edit: Fixed MACRO definition below, originally a copy-paste mistake where both macros were defined as DATA_LAYOUT_START)

DATA_LAYOUT_START(type) void type::GetParams(ifstream& in) {
DATA_LAYOUT_END() }



if you want your macro to expand to multiple lines, end the last character on the line that should be continued with \
The macro will end at the first line that does not have the continuation character.
ex:

DATA_LAYOUT_START(type) {\
                          static int count = 0;\
                          count++;



MFC and ATL use Macro tables quite often. For a nice example Lookup BEGIN_MSG_MAP in atlwin.h. This function implements the windows procedure for an ATL window with a MACRO table definition.


Also, macros are just "compile time helpers", think of them as "mildly smart typists" Whatever you put in after the macro name is considered text. In your example, you immedately start with a curley brace as if you think that''s part of the define syntax. It is not, the curley brace will be included in every expansion of the macro.

As Paul said, there is a continuation character (\) to make macros expand to multiple lines. The only thing that terminates a macro definition is a line without a continuation, there is no "end curley brace".

The Windows/MFC BEGIN_MESSAGE_MAP/END_MESSAGE_MAP macros do a similar thing, having a being and end wrapper around some other macro code. You might want to look at how those macros are written. They are in afxwin.h (pasted below) Ignoring just how the message map works, the syntax for these wrapper macros should be helpful in writing yours.

#define BEGIN_MESSAGE_MAP(theClass, baseClass) \
	PTM_WARNING_DISABLE \
	const AFX_MSGMAP* theClass::GetMessageMap() const \
		{ return GetThisMessageMap(); } \
	const AFX_MSGMAP* PASCAL theClass::GetThisMessageMap() \
	{ \
		typedef theClass ThisClass;						   \
		typedef baseClass TheBaseClass;					   \
		static const AFX_MSGMAP_ENTRY _messageEntries[] =  \
		{
#define END_MESSAGE_MAP() \
		{0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } \
	}; \
		static const AFX_MSGMAP messageMap = \
		{ &TheBaseClass::GetThisMessageMap, &_messageEntries[0] }; \
		return &messageMap; \
	}								  \


Macro is evil. Don''t do that. It''s usually possible to simplify code using language abstractions, not macro.

—SA


这篇关于宏如何包含大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆