如何有条件地确定在编译时调用哪些函数?

pbe*_*ean 6 c function dynamic c-preprocessor

我正在用C实现一个非常非常基本的组件系统,但现在我想要"动态"调用某些函数.设置非常简单:主程序只是一个无限循环,其中检查一些条件,并为每个启用的组件调用"进程"函数.

例如,现在它的工作原理如下:

while (1) {
  input_process();
  network_process();
  graphics_process();
}
Run Code Online (Sandbox Code Playgroud)

但我想将它分成单独的组件,并以某种方式在中心位置定义使用哪些部件.这可以通过简单的定义来完成,如下所示:

#define HAS_NETWORK
...
while (1) {
  input_process();
#ifdef HAS_NETWORK
  network_process();
#endif
  graphics_process();
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的那样,1或者只有少数组件是正常的,但是如果我想在未来对所有这些(输入,网络和图形)和其他组件这样做,我将不得不将#ifdefs单独放入他们每个人都有,这很乏味.

在伪代码中,我想要完成的是以下内容:

components = {'input', 'network', 'graphics'}
...
foreach component in components
  execute component_process()
Run Code Online (Sandbox Code Playgroud)

这样,组件可以在将来轻松添加.我不介意检查是在编译时还是在运行时完成(虽然我显然更喜欢编译时,但我可以想象运行时更容易实现).我不知道怎么开始.

Ark*_*nez 11

你需要指向函数的指针,创建一个指向函数的指针数组并动态索引它.

这里是关于函数指针的链接.

  • 这是IMO的最佳解决方案.这至少是我用函数式语言处理这种情况的方式. (2认同)

Cat*_*lus 5

编译时解决方案:预构建步骤和包含该循环内部的指令,例如

while (1) {
#include "components.generated.c"
}
Run Code Online (Sandbox Code Playgroud)

生成该文件的基本脚本可能看起来像(Python):

components = ('input', 'networking', 'graphics')
# this could also be e.g. a glob on a directory or a config file

with open('components.generated.c', 'w') as fp:
    for component in components:
        print >>fp, '%s_process();' % component
Run Code Online (Sandbox Code Playgroud)

任何体面的构建系统都可以让你这样做.


fa.*_*fa. 1

在编译时使用 X 宏:

component.x 是一个包含以下内容的文件:

COMPONENT( graphic , "3D graphics rendering" )
COMPONENT( network , "Network" )
COMPONENT( other , "usefull stuff" )
#undef COMPONENT
Run Code Online (Sandbox Code Playgroud)

使用它:

#define COMPONENT( what , description ) what ## _process();
while (1)
{
#include "components.x"
}
Run Code Online (Sandbox Code Playgroud)

例如在另一个地方:

std::cout << "We use :\n" ;
#define COMPONENT( what , description )\
std::cout << #what << " : " << description << "\n" ;
#include "components.x"
Run Code Online (Sandbox Code Playgroud)

这样你就可以将 HAS_ 定义放在 component.x 中的一个位置:

#ifdef HAS_GRAPHIC
COMPONENT( graphic , "3D graphics rendering" )
#endif
#ifdef HAS_NETWORK
COMPONENT( network , "Network" )
#endif
#ifdef HAS_OTHER
COMPONENT( other , "usefull stuff" )
#endif
#undef COMPONENT
Run Code Online (Sandbox Code Playgroud)