6

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.

bfops
  • 5,348
  • 5
  • 36
  • 48

2 Answers2

5

The macro preprocessor doesn't have the ability to take substrings or capitalize a letter. Sorry.

If you could change your naming scheme you might have more success. For example:

QuitCallbackType _QuitCallback;

Edit: I've been warned not to use leading underscores, but the idea still applies:

QuitCallbackType callbackQuit;
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 1
    +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 Oct 15 '10 at 21:11
  • 7
    @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, – Cheers and hth. - Alf Oct 15 '10 at 21:18
  • @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 Oct 15 '10 at 21:22
  • @Matteo: I just get this uncomfortable feeling when someone walking in front of me is going exactly where I'm going. Cheers, – Cheers and hth. - Alf Oct 15 '10 at 21:28
  • @Alf: you're not alone, it often happens to me too. For the previous comment, just joking of course. :) – Matteo Italia Oct 15 '10 at 21:33
  • @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 Oct 15 '10 at 21:35
2

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
  • 6,134
  • 4
  • 33
  • 55
  • How about using a union instead of a struct? A struct leads to excessive wasted memory, when all the data members are stored, and only one of them is used at any given time. – Galaxy Oct 15 '19 at 05:17