使用宏禁用功能 [英] Disable functions using MACROS

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

问题描述

在网上搜索了不少的解决方案后,我决定在这里问,如果我的解决方案是好的。

After searching quite a bit in the Internet for a solution I decided to ask here if my solution is fine.

我试着写意图是简单一个简单的和模块化的ç日志库禁用和专门帮助博士生和研究人员来调试算法降低多达不可能性日志系统的影响。

I'm trying to write a simple and modular C logging library intended to be simple to disable and specially helping PhD students and researchers to debug an algorithm reducing as much as possibile the impact of the logging system.

我的问题是,我想使可能的库的用户禁止在编译时记录系统生成可执行其中记录器的成本是0。

My problem is that I want make possible for the user of the library to disable the logging system at compile time producing an executable in which the cost of the logger is 0.

的C code是这样的:

The C code would look like this:

...
logger_t * logger;

result = logger_init(logger);
if(result == -1) {...}
...

这只会初始化记录器。寻找一个例子code我已经检查了ASSERT.H头,但在我的情况警告的列表soulution结果。事实上,如果logger_init()由0使用宏取代,这将导致从未使用过该变量记录。

this will simply initialize the logger. Looking for an example code I have checked the assert.h header but that soulution results in a list of warnings in my case. In fact, if logger_init() is substituted by 0 using the macro this will result in the variable logger never used.

有关这个原因,我已经决定使用这种方式:

For this reason I've decided to use this approach:

int logger_init(logger_t *logger);

#ifndef NLOG /* NLOG not defined -> enable logging */
int logger_init(logger_t *logger) {
...
}
#else  /* NLOG defined --> the logging system must be disabled */
#define logger_init(logger)     (int)((logger = NULL) == NULL)
#endif /* NLOG */

这不会导致警告,我也避免调用函数的开销。其实我第一次尝试是这样做:

this does not result in warnings and I also avoid the overhead of calling the function. In fact my first try was to do like this:

int logger_init(logger_t *logger) {
  #ifndef NLOG /* NLOG not defined -> enable logging */
  ...
  #endif
  return 0;
}

保持通话功能,即使我并不需要它。

keep calling the function even if I do not need it.

你觉得我的解决方案,可以考虑一个很好的解决方案?有没有更好的解决办法?

Do you think my solution could be considered a good solution? Is there a better solution?

非常感谢你们!
干杯,
阿曼多

Thanks a lot, guys! Cheers, Armando

推荐答案

标准成语,至少在90年代,是:

The standard idiom for that, at least in the 90s, was:

#ifndef NLOG
void logger_init(logger_t *logger);
void logger_log(logger_t *logger, ...);
#else
#define logger_init (void)sizeof
#define logger_log  (void)sizeof
#endif

记住,sizeof的操作数不评估,虽然他们是语法检查。
这招作品也有可变参数的功能,因为sizeof操作符会看到一个前presion公司与多家运营商逗号:

Remember that sizeof operands are not evaluated, although they are syntax checked. This trick works also with variadic functions, because the sizeof operator will see an expresion with several comma operators:

logger_log(log, 1, 2, 3);

转换为:

(void)sizeof(log, 1, 2, 3);

这些逗号不分离参数(的sizeof 不是一个功能,但操作员),但他们的逗号操作符

Those commas are not separating parameters (sizeof is not a function but an operator), but they are comma operators.

请注意,我改变返回值从 INT 无效。对于没有真正的需要,但是的sizeof 收益将主要毫无意义的。

Note that I changed the return value from int to void. No real need for that, but the sizeof return would be mostly meaningless.

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

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