Macro Laziness

Started by
3 comments, last by Jiia 19 years, 7 months ago
I'm trying to create some lazy macros. My scripting engine must be set up with pointers to variables and have an identification text version of that same variable. Like this..

Script->AddVariable(&MyInt, "MyInt");
Script->AddValue(ENUM_VALUE, "ENUM_VALUE");
As you can see, the names always match. Now imagine about 500 of these. It's ugly, and a maintenance nightmare. Actually, there are only a few of the AddVariable type lines, but many, many values. Are there any ways I can cheat here? At least on the value versions? How can I give the scripting engine the value of an enum and it's text name all at once?
#define ADD_VALUE(_eval_) AddValue(_eval_,"_eval_");
It would be even cooler to go through a loop for the whole slew of enum values of each type, but I'm almost sure the IDE wouldn't allow such hacking.
for(int i=0; i<ENUM_MAX; i++)
  AddValue( GetEnumValue(i), GetEnumLabel(i) );
The scripting engine is of my own creation, so feel free to offer any advice on how to avoid this situation to begin with. Thanks for any advice.
Advertisement
Use Stringification:

#define ADD_VALUE(EXP) AddValue(EXP, #EXP);

#define ADD_VALUE(_eval_) AddValue(_eval_,#_eval_);


EDIT: you beat me!
Quote:Original post by mandrav
#define ADD_VALUE(EXP) AddValue(EXP, #EXP);

But of course, this does not work dynamically, although maybe it doesn't need to.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Ahhh wow, really nice. Thanks. One little operator.

Quote:Original post by swiftcoder
But of course, this does not work dynamically, although maybe it doesn't need to.

To sound like an idiot: What do you mean dynamically?

This topic is closed to new replies.

Advertisement