在编译的时候C函数装饰(包装) [英] C function decorators (wrappers) at compile time

查看:156
本文介绍了在编译的时候C函数装饰(包装)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变一些功能的行为 C 与preprocessor的帮助;并且还添加可进行设置或可选参数关闭...

I'm trying to change the behaviour of some functions in C with help of the preprocessor; and also add optional parameters that can be set on or off...

有关可选参数的基本模式很简单:

The basic pattern for the optional parameters is easy:

#ifdef OPT_PARAM
  #define my_func(a, b, opt) _my_func(a, b, opt)
#else
  #define my_func(a, b, opt) _my_func(a, b)
#endif

/*the rest of the code always calls "my_func" with all the params
and not the underscored version...*/

#ifdef OPT_PARAM
void _my_func(int a, int b, int opt)
#else
void _my_func(int a, int b)
#endif
{
  /*... more #ifdefs surrounding opt uses */
}

为有条件地缠绕一个功能模式是相似的,但问题是,下划线开始增加(一个额外的嵌套每个级别,这可以是一个不同的功能,或只是一个的#define在情况下,下一级它不裹)。

the pattern for wrapping a function conditionally is similar, but the problem is that the underscores start to add up (one extra for each level of nesting, that can be a different function or just a #define for the next level in case it's not wrapped).

那么,如何在这里降低code复杂任何想法?

So, any ideas about how to reduce the code complexity here?

P.S。我会愿意使用Python ...但是这是一个驱动程序: - (

P.S. I'd be willing to use Python... but this is for a driver :-(

推荐答案

在最后我刚添加的均匀处理额外的参数和更具描述性的名称改变了晦涩下划线的新的装饰。

In the end I just added a new decorator that handled uniformly the extra parameters and changed the obscure underscores by more descriptive names.

现在它是一个更正交设计中,我可以插在编译时没有运行时开销拔掉行为。

Now it's a more orthogonal design in which I can plug and unplug behaviour at compile time with no runtime overhead.

这篇关于在编译的时候C函数装饰(包装)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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