开发者

Mapping enum values to strings in C++

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 ori开发者_运维百科ginal 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.


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?


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>


This may be helpful to you:

The "stabs" debug format

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜