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.
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With