3

Is there a way to, at runtime, map the value of an enum to the name? (I'm building with GCC.)

I know GDB can do it and I'm willing to use something that's unportable and mucks with debug data.


Edit: I'm looking for a solution that doesn't require modifying the original enum declaration nor hand copying all the values out in a mapping function. I already know how to do both of those.

Effectively; I want a function that does whatever GDB does when it formats runtime enum values.

BCS
  • 75,627
  • 68
  • 187
  • 294
  • Try to start here http://stackoverflow.com/questions/207976/how-to-easily-map-c-enums-to-strings and here http://stackoverflow.com/questions/201593/is-there-a-simple-script-to-convert-c-enum-to-string#201792 – Igor Dec 10 '09 at 21:46
  • Interesting ignoring the stop words and suffixes all but one of the word in the title match that first link and it STILL didn't show up in the auto search.... – BCS Dec 10 '09 at 21:50
  • Igor Oks: the referenced questions/answers seem to revolve around either alternate enum deceleration styles or hand generated functions. I already have a solution thaty works via that sort of solution. – BCS Dec 10 '09 at 21:59
  • @BCS: Do you want information to write something like that, or something already written (as your comment to Goz' answer suggests)? – David Thornley Dec 10 '09 at 22:10
  • I'm looking for a library I can just use. – BCS Dec 10 '09 at 22:14
  • This is a bit problematic because the compilers add the information into their debug versions of the executables. You could write code to extract this information out of the executable, but I find it much easier just to put in conversion code in my program. I don't think takes too much time. – Thomas Matthews Dec 10 '09 at 22:48
  • Thomas Matthews: Just for my self, I'll grant the by-hand solution is faster. But a properly done solution (sort of along these lines: http://www.gnu.org/s/libc/manual/html_node/Backtraces.html) would be faster and easier over all (for the community at large). – BCS Dec 10 '09 at 23:53

3 Answers3

3

If you have tenacity, you could create a tool that will parse source files for enums, generate the translation functions and add them to the source code. With more energy, you could write plugins for editors such as Eclipse and Emacs that will perform this for you.

Perhaps it could be done in a Perl script?

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • 1
    In a previous job we had a fairly thorough system to define all return codes in enums and a Perl script to pull them out and make a compileable file of strings so they could be printed when error occurred. These days I would probably use Python but Perl got the job done. – Steve Fallows Dec 10 '09 at 23:13
1

If you don't want to invest the time to utilize GCCs symbol information, gcc-xml provides you information about C++ sources in a reusable XML format, including enumeration names.

Simplified example... this source:

enum E {
  e1 = 1,
  e2 = 42
};

becomes:

<GCC_XML>
  <!-- ... -->
  <Enumeration name="E">
    <EnumValue name="e1" init="1"/>
    <EnumValue name="e2" init="42"/>
  </Enumeration>
  <!-- ... -->
</GCC_XML>
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • That plus some fun with XSLT and I should be able to code-gen the function I want. Yuck. – BCS Mar 29 '11 at 15:24
0

This may be helpful to you:

The "stabs" debug format

BCS
  • 75,627
  • 68
  • 187
  • 294
Goz
  • 61,365
  • 24
  • 124
  • 204
  • That would be useful for the guy who writes the library I'm looking for. – BCS Dec 10 '09 at 21:56
  • 3
    When you provide a link, please give a one-sentence summary (at least) of what it links to. –  Dec 10 '09 at 23:01