Quote:Original post by ZahlmanSure, that's all perfectly reasonable.
OK, let's say we have a header file with an enum (I'd rather not touch the problem of converting stupid #define usage into enumeration usage [wink] ):#ifndef AUDIO_H#include AUDIO_Henum AUDIO { AUDIO_U8 = < unique integer value >, AUDIO_S8 = < unique integer value >, AUDIO_U16LSB = < unique integer value >, AUDIO_S16LSB = < unique integer value >, AUDIO_U16MSB = < unique integer value >, AUDIO_S16MSB = < unique integer value >, AUDIO_U16 = AUDIO_U16LSB, AUDIO_S16 = AUDIO_S16LSB,#if ENDIANNESS == LITTLE_ENDIAN AUDIO_U16SYS = AUDIO_U16LSB, AUDIO_S16SYS = AUDIO_S16LSB,#else AUDIO_U16SYS = AUDIO_U16MSB, AUDIO_S16SYS = AUDIO_S16MSB,#endif DEFAULT_FORMAT = AUDIO_S16SYS}#endif
Now, the aliasing problem is one that *can't* be resolved perfectly, for the simple reason that information is lost - a value of type AUDIO with value AUDIO_U16 is identical to a value of type AUDIO with value AUDIO_U16LSB, so there is no way to know which symbol was used in the source code. (After all, we can also create a variable of type AUDIO by reading in an int from a file and doing an explicit cast). However, let's say arbitrarily that we will resolve these problems by always stringizing an enum value according to the *first* enumerant in the enum with the appropriate value. Thus, in effect, the way to deal with aliased values is simply to *ignore* them ;)
(That is, a multimap doesn't help: there is no way to determine which value to select. So pragmatically, we have to just pick one, which takes us back to using a plain map. Incidentally, the scheme I propose automatically causes 'DEFAULT_FORMAT to become "AUDIO_S16MSB" on a PowerPC Mac', since that's the first enumerant with that value, so that's what will be used for the stringization. I suspect this simple heuristic will be best, really.)
We then write our script as follows:- Invoke the preprocessor on the header file, i.e. ask the compiler what the system endianness is ;)- From the preprocessor's output, parse out the enum declaration.- Initialize an empty associative array from integer values to strings.- For each enumerant: - Determine the int value. - If it is not found in the associative array, add it, associating it with the stringized version of the enumerant.- Output code which initializes a std::map<int, const char*> with the contents of our associative array, by iterating over our AA's keys and generating corresponding map.insert() statements. (Better yet, write code which wraps the whole thing up in a class. We can use a single class and create a global static instance of it for each enum, and do the initialization by clever use of operator chaining.)
Since implicit conversion does happen from the enumeration *to* an int, we can use enumeration values to look up the name in the std::map just fine.
The class might look something like this - all completely off the top of my head at 4:30 AM, but damned if it doesn't look good to me right now ;)
*** Source Snippet Removed ***
And we just write that code once; our auto-generated code just has to initialize Enumeration<E>::instance for each typename E that is appropriate (i.e., each enumeration in the program). We just emit something like:Enumeration<AUDIO> Enumeration<AUDIO>::instance("AUDIO_U8")("AUDIO_S8")("AUDIO_U16LSB")("AUDIO_S16LSB")("AUDIO_U16MSB")("AUDIO_S16MSB");
And that should work even at top level, because we're just initializing the variable; no procedural code here, nope, no sir ;) Even if that doesn't work, though, I'm fairly sure that "Enumeration<AUDIO> Enumeration<AUDIO>::instance = Enumeration<AUDIO>(etc....)" will.
In my previous posts I may have inadvertently given the impression that I didn't understand the solutions being presented, but I was really just trying to make the point that they could not be applied to the particular problem in question without modification.
In the case of the SDL_mixer initialization code, it's my intent that the log include the perhaps multiple 'names' of (human-readable strings associated with) the requested and queried audio formats. This means, for example, that if the requested format is AUDIO_S16SYS, the log reflects that this is in fact an alias for AUDIO_S16MSB on the system in question, and also that the requested format is the default format for that system.
Obviously this isn't terribly critical information - it's no great mystery what format will be selected for each system, and in any case it can easily be determined by looking at the corresponding header file. However, I like to log details like this during development, especially when working with multiple platforms, just to make sure I know exactly what's going on under the hood.
(As it happens, I added this detailed logging while trying to track down a bug that was causing the app to crash. It turned out to be a known bug in the current version of SDL_mixer. Although the bug wasn't directly related to the issue of audio format, the specific format requested did in fact cause the bug to manifest in different ways on each platform. For that reason it was useful during the debugging process to know exactly what format was being used, whether it was the default for that system, and so on, hence my particular interest in the aliases.)
As for automation, I don't know that it would be worth the trouble in this particular case (given that, like it or not, the enumerants in question are in fact macros buried in a 3rd-party library header file).
That said, this thread has given me some ideas about how to improve support for enumerated types in my in-game console system, while perhaps addressing the aliasing problem in a more clean and consistent manner, so cheers for that :)