tags:

views:

112

answers:

6

We have a library that provides access to buttons on a device. These buttons are enumerated to the rest of the system 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?

+1  A: 

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;
}
Jerry Coffin
+1  A: 

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

Radu094
Nice... I hadn't considered this, but it fits our application perfectly. It also models what you get from things like `autoconf`.
jheddings
+5  A: 

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;
 }
Michal Čihař
typo: sizeof(buttots)
invariant
So really, I'm looking for a way to dynamically generate that array. Since it varies from device to device, I can't really fix it to a set of static values. I could have a bunch of `#ifdef`'s around various arrays, but that gets ugly in a hurry.
jheddings
A: 

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.

AShelly
This was proposed by a colleague, too... The only pushback I had was that it doesn't scale well if you are trying to do more than simple string lookups (i.e. if a button can have a type and additional data). That could make the config directive pretty nasty.
jheddings
A: 

I would use a table of <button ID, button name> along with a constant declaring the items in the table:

struct Button_Info
{
    const char * button_name;
    unsigned int button_id;
};

const Button_Info button_table[] =
{
  {"power", 0},
  {"reset", 1},
};
const unsigned int NUMBER_NAMED_BUTTONS =
    sizeof(button_table) / sizeof(button_table[0]);

For a name to ID conversion, each button must have a unique name. More than one NULL name will only find the first one (if using a sequential search).

Placing the button ID into the structure, decouples order dependency in the array.

Thomas Matthews
A: 
mjh2007
That's not correct. `names` is an array of `char *` pointers. So, `sizeof names[i];` is the same as `sizeof(char *);` for all `i`.
Alok