What's the best way to get "numbered" config options from the C preprocessor?
We have a library that provides access to buttons on a device. These buttons are enumerated to the rest of the sy开发者_开发技巧stem as things like "power button" and so on. This way, the rest of the applications don't have to worry about how the "power button" is implemented.
We build this library using a few configuration directives that look like this (simplified):
#define BUTTON_COUNT 2
#define BUTTON_0_NAME "power"
#define BUTTON_1_NAME "reset"
#define BUTTON_2_NAME NULL
The question is, at some point I want to fetch the name of button 1. Ideally, I would do this by creating a function like get_button_name(int index)
. The problem is, I can't find a good way to get at the numbered config options in that function. The current implementation looks like:
const char* get_button_name(int index) {
switch (index) {
case 0: return BUTTON_0_NAME;
case 1: return BUTTON_1_NAME;
case 2: return BUTTON_2_NAME;
}
return NULL;
}
The trouble is, this code is shared for many devices, so the number of supported buttons will continue to increase. That means changing this function and all the devices that pull it in have to add #define
's for the new buttons.
What I'd really prefer (although I'm don't think it's possible with the pre-processor) is something like:
const char* get_button_name(int index) {
if (index < BUTTON_COUNT) {
return BUTTON_ ## index ## _NAME;
}
return NULL;
}
Any ideas for a better solution?
How about just having an array?
const char *buttons[] = {
"power",
"reset"};
const char* get_button_name(int index) {
if (index < sizeof(buttons)/sizeof(buttons[0])) {
return buttons[index];
}
return NULL;
}
You can't return at compile time (preproc const) something you ask for at runtime (parameter in a function).
You need to find a way to move all these names to an array of some sort (lookup codegen, T4, etc) then do a simple lookup
I'd probably write the code something like this:
char const *names[] = {"power", "reset"};
#define elements(array) (sizeof(array)/sizeof(array[0]))
char const *get_button_name(size_t index) {
if (index < elements(names))
return names[index];
return NULL;
}
Can you change your configuration directives? Or text-process them into the following form:
Buttons.h:
#define MAKEBUTTONS() const char* gButtonNames[\
"power",\
"reset",\
"macroize",\
]
extern const char* gButtonNames[];
#define GET_BUTTON_NAME(n) gButtonNames[n]
Then only one source file should contain MAKEBUTTONS();
at the global scope, and any client code can call GET_BUTTON_NAME()
instead of a function.
I would use an array. If you define the array as extern then you can declare it in an external module separate from your get_button_name() function. For example:
extern char const *names[] = {"power", "reset", "etc."};
#define num_names 3
char const *get_button_name(int index){
if(index < num_names)
return names[index];
return NULL;
}
I don't think you can use sizeof(names[0]) especially if your names will be different sizes.
精彩评论