将枚举值映射到C ++中的字符串

编程入门 行业动态 更新时间:2024-10-27 22:19:42
本文介绍了将枚举值映射到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 ++中的字符串

本文发布于:2023-10-23 03:33:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519630.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!