bfops
bfops

Reputation: 5678

C++ macro - capitalize string

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.

Upvotes: 6

Views: 7063

Answers (2)

Alexander Rafferty
Alexander Rafferty

Reputation: 6233

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.

Upvotes: 2

Mark Ransom
Mark Ransom

Reputation: 308392

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;

Upvotes: 4

Related Questions