初始化函数指针的全局数组在编译时或运行时主前() [英] Initialize global array of function pointers at either compile-time, or run-time before main()
问题描述
我想初始化函数指针在编译时一个全局数组,用C或C ++。事情是这样的:
I'm trying to initialize a global array of function pointers at compile-time, in either C or C++. Something like this:
module.h中
typedef int16_t (*myfunc_t)(void);
extern myfunc_array[];
module.cpp
#include "module.h"
int16_t myfunc_1();
int16_t myfunc_2();
...
int16_t myfunc_N();
// the ordering of functions is not that important
myfunc_array[] = { myfunc_1, myfunc_2, ... , myfunc_N };
func1.cpp,func2.cpp,... funcN.cpp (符号链接到一个func.cpp文件,使不同的目标文件被创建:func1.o,func2.o ,func3.o,...,funcN.o。NUMBER是使用定义 G ++ -DNUMBER = N
)
func1.cpp, func2.cpp, ... funcN.cpp (symbolic links to a single func.cpp file, so that different object files are created: func1.o, func2.o, func3.o, ... , funcN.o. NUMBER is defined using g++ -DNUMBER=N
)
#include "module.h"
#define CONCAT2(x, y) x ## y
#define CONCAT(x, y) CONCAT2(x, y)
int16_t CONCAT(myfunc_, NUMBER)() { ... }
在自定义g ++编译-DNUMBER = N,之后preprocessing变为:
When compiled using g++ -DNUMBER=N, after preprocessing becomes:
func1.cpp
...
int16_t myfunc_1() { ... }
func2.cpp
...
int16_t myfunc_2() { ... }
等。
myfunc_N的声明()
和初始化 myfunc_array []
是不冷静,由于N经常变化并且可以是10至200。我preFER不使用脚本或生成文件要么生成它们。功能的顺序并不重要,我可以解决的。有一个整齐/更聪明的方式做到这一点?
The declarations of myfunc_N()
and the initialization of myfunc_array[]
are not cool, since N changes often and could be between 10 to 200. I prefer not to use a script or Makefile to generate them either. The ordering of functions is not that important, i can work around that. Is there a neater/smarter way to do this?
推荐答案
我要建议这个问题更多的是C,但转念一想,你要的是函数指针的一个全球性的容器,注册可用的功能进去。我相信,这就是所谓的的辛格尔顿的(战栗)。
I was going to suggest this question is more about C, but on second thoughts, what you want is a global container of function pointers, and to register available functions into it. I believe this is called a Singleton (shudder).
您可以让 myfunc_array
载体,或包裹了等价的C,并提供一个函数来推 MYFUNC
s转换它。现在终于,你可以创建一个类(同样,你可以在C做到这一点),这需要一个 MYFUNC
,并将其推入全球阵列。这都将出现被称为前夕为主。下面是一些code片段,让你想:
You could make myfunc_array
a vector, or wrap up a C equivalent, and provide a function to push myfunc
s into it. Now finally, you can create a class (again you can do this in C), that takes a myfunc
and pushes it into the global array. This will all occur immediately prior to main being called. Here are some code snippets to get you thinking:
// a header
extern vector<myfunc> myfunc_array;
struct _register_myfunc {
_register_myfunc(myfunc lolz0rs) {
myfunc_array.push_back(lolz0rs);
}
}
#define register_myfunc(lolz0rs) static _register_myfunc _unique_name(lolz0rs);
// a source
vector<myfunc> myfunc_array;
// another source
int16_t myfunc_1() { ... }
register_myfunc(myfunc_1);
// another source
int16_t myfunc_2() { ... }
register_myfunc(myfunc_2);
请记住以下几点:
Keep in mind the following:
- 您可以控制功能通过操纵你的链接步骤注册的顺序。
- 您的翻译单元范围的变量的初始化发生main被调用之前,即登记将完成。
- 您可以使用一些魔法的宏观和
__ __ COUNTER
生成唯一的名称。可能有其他欺骗性的方式,我不知道。看到这些有用的问题:
- You can control the order the functions are registered by manipulating your link step.
- The initialization of your translation unit-scoped variables occurs before main is called, i.e. the registering will be completed.
- You can generate unique names using some macro magic and
__COUNTER__
. There may be other sneaky ways that I don't know about. See these useful questions:- Unnamed parameters in C
- Unexpected predefined macro behaviour when pasting tokens
- How to generate random variable names in C++ using macros?
这篇关于初始化函数指针的全局数组在编译时或运行时主前()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!