Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ macro - capitalize string

Tags:

c++

macros

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.

like image 480
bfops Avatar asked Oct 15 '10 21:10

bfops


2 Answers

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;
like image 198
Mark Ransom Avatar answered Oct 04 '22 03:10

Mark Ransom


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.

like image 34
Alexander Rafferty Avatar answered Oct 04 '22 02:10

Alexander Rafferty