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.

7
  • Try to start here stackoverflow.com/questions/207976/… and here stackoverflow.com/questions/201593/… Commented Dec 10, 2009 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.... Commented Dec 10, 2009 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. Commented Dec 10, 2009 at 21:59
  • @BCS: Do you want information to write something like that, or something already written (as your comment to Goz' answer suggests)? Commented Dec 10, 2009 at 22:10
  • I'm looking for a library I can just use. Commented Dec 10, 2009 at 22:14

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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>

1 Comment

That plus some fun with XSLT and I should be able to code-gen the function I want. Yuck.
0

This may be helpful to you:

The "stabs" debug format

2 Comments

That would be useful for the guy who writes the library I'm looking for.
When you provide a link, please give a one-sentence summary (at least) of what it links to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.