将枚举值映射到C ++中的字符串 [英] Mapping enum values to strings in C++

查看:103
本文介绍了将枚举值映射到C ++中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在运行时将枚举的值映射到名称? (我正在与GCC建立)



我知道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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆