将枚举值映射到C ++中的字符串 [英] Mapping enum values to strings in C++
问题描述
我知道GDB可以做到这一点,我愿意使用一些不可移植的东西,并使用调试数据进行处理。
编辑:我正在寻找一种不需要修改原始枚举声明或手动的解决方案在映射函数中复制所有值。我已经知道如何做这两个。
有效地;我想要一个功能,当它格式化运行时枚举值时,可以做任何GDB。
如果你不想投资时间使用GCC的符号信息, gcc-xml 以可重用的XML格式提供有关C ++源的信息,包括枚举名称。
简化示例...此源:
枚举E {
e1 = 1,
e2 = 42
};
成为:
code>< GCC_XML>
<! - ... - >
<枚举名称=E>
&EnumValue name =e1init =1/>
&EnumValue name =e2init =42/>
< /枚举>
<! - ... - >
< / GCC_XML>
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.
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>
这篇关于将枚举值映射到C ++中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!