The project I’m currently working on required me to make some improvements to gmmproc, the code generator used by the gtkmm project. In my current work it’s occassionally necessary to wrap enumerations that don’t follow the consistent naming scheme of GTK+ we take for granted nowadays. This is how the enumerations look like:
FOO_THIS,BAR_THAT,FUZZ_THERE
That should of course be:
MODULENAME_FOO_THIS,MODULENAME_BAR_THAT,MODULENAME_FUZZ_THERE
Ideally, this would prompt a patch to the upstream code. But it’s not always possible to change upstream API, and even if it is you sometimes just don’t have the time for the bureaucracy involved and only want to get your code to compile for now.
But the code generator didn’t like the idea at all. The Perl script that gathers the enumeration definitions from the C header files even went into an infinite loop. I fixed that, but then it turned out that the enumeration parsing module of gmmproc unconditionally stripped everything up to and including the first underscore in every enumeration constant. The result looked like this:
THIS,THAT,THERE
Ugh. Well, this prompted me to change the behavior of the enumeration parser to only strip the prefix if it’s actually common to all constants defined in an enumeration. It’s been a long time since I last dipped my feet into Perl, but it seems to work now. The new code is available from glibmm CVS, including the earlier branches from glibmm-2-8 onwards. I do hope it doesn’t break anything.
In order to actually prefix the enumeration constants in the C++ code, you can use something like this:
_WRAP_ENUM(SomeType, ModuleSomeType, , s#^\b#MODULE_#)
You could even give the C++ constants the same names as in C, which would in many situations be the most appropriate scheme since we use namespaces instead of module prefixes in C++. The resulting shadowing could lead to problems though if C and C++ code is mixed. Your mileage may vary.
Murray Cumming says:
November 30th, 2006 at 13:13
Excellent. You’re back to your habit of properly fixing the things that I just work/hack around.
danielk says:
November 30th, 2006 at 15:09
Thanks. I figured that if I plan to fix it anyway it’d be best to do it now, so that I don’t have to bother with the hacks at all.