tags:

views:

91

answers:

2

I'm using preprocessor macros to declare some repetitive variables, specifically:

QuitCallbackType quitCallback;
LossCallbackType lossCallback;
PauseCallbackType pauseCallback;
KeyCallbackType keyCallback;
MouseCallbackType mouseCallback;

I'd like to use a preprocessor macro to do it, a la


CREATE_CALLBACK_STORAGE(quit)
CREATE_CALLBACK_STORAGE(loss)
CREATE_CALLBACK_STORAGE(pause)
CREATE_CALLBACK_STORAGE(key)
CREATE_CALLBACK_STORAGE(mouse)

where it would essentially be like this:

#define CREATE_CALLBACK_STORAGE(x) capitalize(x)##CallbackType x##CallBack;

Is there a way to do this, so that I don't have to pass in both the capitalized AND lowercase versions of each name?

I realize it's not much less typing to use macros, but the problem itself began intriguing me.

+2  A: 
Mark Ransom
+1 for the alternate scheme. In fact, if the OP is using a macro to define the variables, why not use another for accessing them too, say `#define CALLBACK(x) _##x##Callback`
casablanca
@Mark: Names starting with underscore followed by uppercase letter, like `_QuitCallback` in your example, are reserved for the implementation, anywhere. That also goes for names with two successive underscores. By the way, since I commented on another answer of yours just half-hour or so ago, no I'm not "following" you. :-) I'll just delete this comment after ur edit. Cheers,
Alf P. Steinbach
@Alf P. Steinbach: "By the way, since I commented on another answer of yours just half-hour or so ago, no I'm not "following" you." Excusatio non petita, accusatio manifesta. ;)
Matteo Italia
@Matteo: I just get this uncomfortable feeling when someone walking in front of me is going exactly where I'm going. Cheers,
Alf P. Steinbach
@Alf: you're not alone, it often happens to me too. For the previous comment, just joking of course. :)
Matteo Italia
@Alf, I thought the reservation for underscores was only for two, not one. I see that the rules are more complicated than I remembered: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier - thanks again.
Mark Ransom
+1  A: 

I think you should ditch the idea of macros altogether. A better solution would be to create a simple data structure, such as:

struct CallBacks {
  QuitCallbackType quit;
  LossCallbackType loss;
  PauseCallbackType pause;
  KeyCallbackType key;
  MouseCallbackType mouse;
};

And use this instead:

CallBacks callback;

You can only use the members you want:

callback.quit = GetCallback(...);
someFunc(callback.quit);
// ect..

It also makes the variable names (in my opinion) a little clearer.

Alexander Rafferty