避免邪恶的宏 [英] Avoiding evil macros

查看:79
本文介绍了避免邪恶的宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我知道宏是邪恶的,但我最近遇到了一个问题

,我无法弄清楚如何解决模板。


我想创建一组单例事件对象,并写下

以下宏:


#define GET_SINGLETON_EVENT_FUNCTION(FUNCTION_NAME,MANUAL_ RESET,\

INITIAL_STATE,EVENT_NAME)\

HANDLE FUNCTION_NAME()\

{ \

静态手柄事件; \

if(0 == Event)\

{\

Event = CreateEvent(NULL,MANUAL_RESET,INITIAL_STATE,\

EVENT_NAME); \

} \

返回事件; \\ b
}


这允许我通过宏调用生成单例代码:

GET_SINGLETON_EVENT_FUNCTION(BitBltLock,FALSE,TRUE) ,BitBltLock)

GET_SINGLETON_EVENT_FUNCTION(BlockOnBitBlt,FALSE,TRUE,

" BlockOnBitBlt")

等等。

请帮助我减少邪恶! };>


谢谢,

Jerry

解决方案

< blockquote> Ce **************** @ comcast.net 写道:


我知道宏是邪恶的,但我最近遇到了一个问题

我不能弄清楚如何使用模板解决。


我想创建一组单例事件对象,然后写下

跟随宏:


#define GET_SINGLETON_EVENT_FUNCTION(FUNCTION_NAME,MANUAL_ RESET,\

INITIAL_STATE,EVENT_NAME)\

HANDLE FUNCTION_NAME()\ < br $>
{\

静态HANDLE事件; \

if(0 == Event)\

{\

Event = CreateEvent(NULL,MANUAL_RESET,INITIAL_STATE,\

EVENT_NAME); \

}



测试完全没必要。只需用

初始化你的''事件''这个表达式和''事件''是静态的纯粹事实提供了

,初始化只会进行一次(提供

初始化不会失败)。


return Event; \\ b
}


这允许我通过宏调用生成单例代码:

GET_SINGLETON_EVENT_FUNCTION(BitBltLock,FALSE,TRUE) ,BitBltLock)

GET_SINGLETON_EVENT_FUNCTION(BlockOnBitBlt,FALSE,TRUE,

" BlockOnBitBlt")

等等。

请帮助我减少邪恶! };>



宏是代码生成的工具,就像你在这里写的那样。如何使用模板生成一个函数并给它命名

与函数模板的名称不同?


V

-

请在通过电子邮件回复时删除资金''A'

我没有回应top-发表回复,请不要问




Victor Bazarov写道:

CE **************** @ comcast.net 写道:


我知道宏是邪恶的,但我最近遇到了一个问题

,我无法想象如何解决模板问题。


我想创建一组单例事件对象,并编写

以下宏:


#define GET_SINGLETON_EVENT_FUNCTION(FUNCTION_NAME,MANUAL_ RESET,\

INITIAL_STATE,EVENT_NAME)\

HANDLE FUNCTION_NAME()\

{\

静态HANDLE事件; \

if(0 == Event)\

{\

Event = CreateEvent(NULL,MANUAL_RESET,INITIAL_STATE,\

EVENT_NAME); \

}



测试完全没必要。只需用

初始化你的''事件''这个表达式和''事件''是静态的纯粹事实提供了

,初始化只会进行一次(提供

,初始化不会失败)。



我的代码与其他泄漏的应用程序一起运行

资源耗尽,每隔几个小时,CreateEvent就会失败导致

我的应用程序抛出异常。 CreateSemaphore和CreateMutex

也会定期失败。


如果命名的同步对象已经存在,那么这是真的,那就是提供CreateEvent等,以返回一个句柄,以便将现有对象返回给



在我正在运行的环境中, ,这只是不是
真实。我想将自己限制在一个固定的最大数量的句柄,

和代码,一旦运行,应该永远运行,尽管存在其他不良行为的应用程序。


>


return Event; \\ b
}


这允许我通过宏调用生成单例代码:

GET_SINGLETON_EVENT_FUNCTION(BitBltLock,FALSE,TRUE) ,BitBltLock)

GET_SINGLETON_EVENT_FUNCTION(BlockOnBitBlt,FALSE,TRUE,

" BlockOnBitBlt")

等等。

请帮助我减少邪恶! };>



宏是代码生成的工具,就像你在这里写的那样。如何使用模板来生成函数并给它起一个名字

与函数模板的名称不同?



我无法想办法。所以你说没有。


V

-

请删除资本''A'在通过电子邮件回复时

我没有回复最热门的回复,请不要问



谢谢,

Jerry


Cephalobus_alienus写道:


这允许我通过宏调用生成单例代码:



....


请帮助我减少邪恶! };>



不要使用单身人士。他们不仅仅是一个神圣的全球形式!


-

Phlip
http://www.greencheese.us/ZeekLand < - 不是博客!!!


Hello,

I know that macros are evil, but I recently came across a problem
that I couldn''t figure out how to solve with templates.

I wanted to create a set of singleton event objects, and wrote the
following macro:

#define GET_SINGLETON_EVENT_FUNCTION(FUNCTION_NAME,MANUAL_ RESET,\
INITIAL_STATE,EVENT_NAME) \
HANDLE FUNCTION_NAME() \
{ \
static HANDLE Event; \
if (0 == Event) \
{ \
Event = CreateEvent(NULL, MANUAL_RESET, INITIAL_STATE, \
EVENT_NAME); \
} \
return Event; \
}

This allowed me to generate the singleton code via macro calls:
GET_SINGLETON_EVENT_FUNCTION(BitBltLock, FALSE, TRUE, "BitBltLock" )
GET_SINGLETON_EVENT_FUNCTION(BlockOnBitBlt, FALSE, TRUE,
"BlockOnBitBlt" )
etc. etc.

Please help me to be less evil! };>

Thanks,
Jerry

解决方案

Ce****************@comcast.net wrote:

I know that macros are evil, but I recently came across a problem
that I couldn''t figure out how to solve with templates.

I wanted to create a set of singleton event objects, and wrote the
following macro:

#define GET_SINGLETON_EVENT_FUNCTION(FUNCTION_NAME,MANUAL_ RESET,\
INITIAL_STATE,EVENT_NAME) \
HANDLE FUNCTION_NAME() \
{ \
static HANDLE Event; \
if (0 == Event) \
{ \
Event = CreateEvent(NULL, MANUAL_RESET, INITIAL_STATE, \
EVENT_NAME); \
}

Testing is totally unnecessary. Just initialise your ''Event'' with
this expression and the sheer fact that ''Event'' is static provides
that the initialisation is going to be done only once (provided
that the initialisation does not fail).

return Event; \
}

This allowed me to generate the singleton code via macro calls:
GET_SINGLETON_EVENT_FUNCTION(BitBltLock, FALSE, TRUE, "BitBltLock" )
GET_SINGLETON_EVENT_FUNCTION(BlockOnBitBlt, FALSE, TRUE,
"BlockOnBitBlt" )
etc. etc.

Please help me to be less evil! };>

Macros are tools for code generation like what you wrote here. How
would you use templates to generate a function and give it a name
different from the name of the function template?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



Victor Bazarov wrote:

Ce****************@comcast.net wrote:

I know that macros are evil, but I recently came across a problem
that I couldn''t figure out how to solve with templates.

I wanted to create a set of singleton event objects, and wrote the
following macro:

#define GET_SINGLETON_EVENT_FUNCTION(FUNCTION_NAME,MANUAL_ RESET,\
INITIAL_STATE,EVENT_NAME) \
HANDLE FUNCTION_NAME() \
{ \
static HANDLE Event; \
if (0 == Event) \
{ \
Event = CreateEvent(NULL, MANUAL_RESET, INITIAL_STATE, \
EVENT_NAME); \
}


Testing is totally unnecessary. Just initialise your ''Event'' with
this expression and the sheer fact that ''Event'' is static provides
that the initialisation is going to be done only once (provided
that the initialisation does not fail).

My code is running alongside other applications that are leaky
resource hogs, and every few hours, CreateEvent fails resulting in
my application throwing an exception. CreateSemaphore and CreateMutex
also periodically fail.

It is quite true, that if a named synchronization object already
exists, that CreateEvent, etc. are -supposed- to return a handle to
the already existing object.

In the environment that I''m running under, however, this just isn''t
true. I want to limit myself to a fixed maximum number of handles,
and the code, once running, should run forever despite the presence
of other, ill-behaved applications.

>

return Event; \
}

This allowed me to generate the singleton code via macro calls:
GET_SINGLETON_EVENT_FUNCTION(BitBltLock, FALSE, TRUE, "BitBltLock" )
GET_SINGLETON_EVENT_FUNCTION(BlockOnBitBlt, FALSE, TRUE,
"BlockOnBitBlt" )
etc. etc.

Please help me to be less evil! };>


Macros are tools for code generation like what you wrote here. How
would you use templates to generate a function and give it a name
different from the name of the function template?

I couldn''t figure out any way. So you''re saying there isn''t any.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask

Thanks,
Jerry


Cephalobus_alienus wrote:

This allowed me to generate the singleton code via macro calls:

....

Please help me to be less evil! };>

Don''t use singletons. They are not just a hallowed form of Global!

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


这篇关于避免邪恶的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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