I have many variables that are named the same as elements in an engineering specification document so the string version of the name is also useful.
I find myself using a macro like this a lot:
#define MACRO(a) a, #a
Typical usage is:
void someFunction(int a, const char *name);
someFunction(MACRO(meaningfully_named_variable));
My question is threefold:
Edit I should have said that the above is a minimal example. The function might have other parameters and the named entity might be a data member or perhaps even a function itself.
Another extension I'm considering for C++ is a class NamedRef
that could receive the contents of the macro.
template <typename T>
struct NamedRef
{
NamedRef(T *t, const char *name) : t(t), name(name) { }
T *t;
const char *name;
};
template <typename T>
NamedRef<T> namedRef(T &t, const char *name)
{
return NamedRef<T>(&t, name);
}
#define WITH_NAME(a) a, #a
// more sophisticated usage example
void otherFunction(double, NamedRef<int>, bool);
otherFunction(0.0, namedRef(object.WITH_NAME(meaningful_member_name)), false);
A parameter can be either a simple string or a quoted string. It can be passed by using the standard method of putting variables into shared and profile pools (use VPUT in dialogs and VGET in initial macros).
To assign a macro that you pass arguments to a button, shape, image, or any object, you first right-click that object and click Assign Macro and then type the name of the macro and the argument, following the pattern described in the above examples, and then click OK. 'show_msg "I clicked a button!"'
There is no way to convert a macro argument into a character constant. If you want to stringify the result of expansion of a macro argument, you have to use two levels of macros. s is stringified when it is used in str, so it is not macro-expanded first.
The number-sign or "stringizing" operator (#) converts macro parameters to string literals without expanding the parameter definition. It's used only with macros that take arguments.
You could take it a step further:
#define SOMEFUNCTION(a) somefunction(a, #a)
However, this is only useful if you call the same function alot. Otherwise, I don't think there is any better way than your example. Of course, you should change the name of the macro to something more meaningful though, like ARGUMENTS
or something.
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