[英]C++ Bitflaged enum to string
I'm trying to do what Intellisense does in visual studio when you hover over a bitwise-enum (or however it's called) variable (while debugging), by taking an enum and converting it to string. 当你将鼠标悬停在一个按位枚举(或者它被称为)变量(调试时)时,我试图通过获取枚举并将其转换为字符串来尝试Intellisense在visual studio中所做的事情。
for example: 例如:
#include <iostream>
enum Color {
White = 0x0000,
Red = 0x0001,
Green = 0x0002,
Blue = 0x0004,
};
int main()
{
Color yellow = Color(Green | Blue);
std::cout << yellow << std::endl;
return 0;
}
If you hover over yellow
you'll see: 如果你将鼠标悬停在yellow
你会看到:
So I want to be able to call something like: 所以我想能够打电话给:
std::cout << BitwiseEnumToString(yellow) << std::endl;
and have the output print: Green | Blue
并输出打印: Green | Blue
Green | Blue
. Green | Blue
I wrote the following which tries to provide a generic way of for printing an enum: 我写了以下内容,试图提供一种打印枚举的通用方法:
#include <string>
#include <functional>
#include <sstream>
const char* ColorToString(Color color)
{
switch (color)
{
case White:
return "White";
case Red:
return "Red";
case Green:
return "Green";
case Blue:
return "Blue";
default:
return "Unknown Color";
}
}
template <typename T>
std::string BitwiseEnumToString(T flags, const std::function<const char*(T)>& singleFlagToString)
{
if (flags == 0)
{
return singleFlagToString(flags);
}
int index = flags;
int mask = 1;
bool isFirst = true;
std::ostringstream oss;
while (index)
{
if (index % 2 != 0)
{
if (!isFirst)
{
oss << " | ";
}
oss << singleFlagToString((T)(flags & mask));
isFirst = false;
}
index = index >> 1;
mask = mask << 1;
}
return oss.str();
}
So now I can call: 所以现在我可以打电话:
int main()
{
Color yellow = Color(Green | Blue);
std::cout << BitwiseEnumToString<Color>(yellow, ColorToString) << std::endl;
return 0;
}
I get the desired output. 我得到了所需的输出。
I'm guessing that I couldn't find anything about it since I don't know how it's called, but anyways - 我猜我找不到任何关于它的东西,因为我不知道它是怎么称呼的,但无论如何 -
Is there something in std or boost that does that or can be used to provide this? std或boost中有什么东西可以用来提供这个吗?
If not, what's the most efficient way to do such a thing? 如果没有,那么做这种事最有效的方法是什么? (or would mine suffic) (或者我的太多了)
EDIT: See below for a generic, template
implementation... Note, though, that this template
implementation tramples ALL OVER ostream
's operator <<()
implemantations for practically everything! 编辑:请参阅下面的通用template
实现...但是请注意,这个template
实现实际上践踏了所有OVER ostream
的operator <<()
实现! It'd be better if the enum
s were full-blown classes, with a base class implementation for the template
. 如果enum
是完整的类,并且template
具有基类实现,那就更好了。 This generic definition is the equivalent of an atom bomb in a china shop... 这个通用定义相当于中国商店的原子弹...
I wrote the following example, with a test function. 我用测试函数编写了以下示例。 It uses C++ overloads to allow you to simply cout
a Color
- if you want to be able to still print the simple numerical value, you would have to cast it into an int
: 它使用C ++重载来让你简单地cout
一个Color
- 如果你想仍然能够打印简单的数值,你必须将它转换为int
:
#include <iostream>
enum Color {
White = 0x0000,
Red = 0x0001,
Green = 0x0002,
Blue = 0x0004,
}; // Color
std::ostream &operator <<(std::ostream &os, Color color) {
static const char *colors[] = { "Red", "Green", "Blue", 0 }; // Synchronise with Color enum!
// For each possible color string...
for (const char * const *ptr = colors;
*ptr != 0;
++ptr) {
// Get whether to print something
bool output = (color & 0x01)!=0;
// Is color bit set?
if (output) {
// Yes! Output that string.
os << *ptr;
} // if
// Next bit in color
color = (Color)(color >> 1);
// All done?
if (color == 0) {
// Yes! Leave
break;
} // if
// No, so show some more...
if (output) {
// If output something, need 'OR'
os << " | ";
} // if
} // for
return os;
} // operator <<(Color)
void PrintColor() {
for (unsigned c = 0; c < 8; ++c) {
Color color = Color(c);
std::cout << color << std::endl;
} // fors
} // PrintColor()
First, a header file: 首先,头文件:
// EnumBitString.h
template <typename ENUM>
const char * const *Strings() {
static const char *strings[] = { "Zero", 0 }; // By default there are no Strings
return strings;
} // Strings<ENUM>()
template <typename ENUM>
std::ostream &operator <<(std::ostream &os, ENUM e) {
const char * const *ptr = Strings<ENUM>();
if (e == 0) {
os.operator <<(*ptr);
return os;
} // if
// For each possible ENUM string...
while (*ptr != 0) {
bool output = (e & 0x01) != 0;
// Is bit set?
if (output) {
// Yes! Output that string.
os.operator <<(*ptr);
} // if
// Next bit in e
e = (ENUM)(e >> 1);
// All done?
if (e == 0) {
// Yes! Leave
break;
} // if
// No, so show some more...
if (output) {
os.operator <<(" | ");
} // if
++ptr;
} // while
return os;
} // operator <<(ENUM)
Next, your example: 接下来,您的示例:
// Colors.h
#include "EnumBitString.h"
enum Colors {
White = 0x0000,
Red = 0x0001,
Green = 0x0002,
Blue = 0x0004,
NumColors = 4
}; // Colors
template <>
const char * const *Strings<Colors>() {
static const char *strings[] { "White", // Zero case
"Red",
"Green",
"Blue",
0 }; // Don't forget final 0
static_assert((sizeof(strings)/sizeof(strings[0])==NumColors+1, "Colors mismatch!");
return strings;
} // Strings<Colors>()
Then, another example of bits within a value: 然后,值中的另一个位示例:
// Flags.h
#include "EnumBitString.h"
enum Flags {
CF = 0x0001,
// Res1 = 0x02,
PF = 0x0004,
// Res2 = 0x08,
AF = 0x0010,
// Res3 = 0x20,
ZF = 0x0040,
NumFlags = 7
}; // Flags
template <>
const char * const *Strings<Flags>() {
static const char *strings[] = { "None",
"Carry",
"",
"Parity",
"",
"Arithmetic",
"",
"Zero",
0 }; // Don't forget final 0
static_assert((sizeof(strings)/sizeof(strings[0])==NumFlags+1, "Flags mismatch!");
return strings;
} // Strings<Flags>()
Finally, a test program: 最后,一个测试程序:
#include <iostream>
#include "Colors.h"
#include "Flags.h"
void TestENUM() {
for (unsigned c = 0; c < 0x0008; ++c) {
Colors color = Colors(c);
std::cout << color << std::endl;
} // for
for (unsigned f = 0; f < 0x0080; ++f) {
Flags flag = Flags(f);
std::cout << flag << std::endl;
} // for
} // TestENUM()
Cool, huh? 很酷,对吧?
You're going to have to maintain a list of the string representations of your enum, be it in a vector, hard-coded, etc. This is one possible implementation. 您将不得不维护枚举的字符串表示列表,无论是在向量中,硬编码等等。这是一种可能的实现方式。
enum Color : char
{
White = 0x00,
Red = 0x01,
Green = 0x02,
Blue = 0x04,
//any others
}
std::string EnumToStr(Color color)
{
std::string response;
if(color & Color::White)
response += "White | ";
if(color & Color::Red)
response += "Red | ";
if(color & Color::Green)
response += "Green | ";
if(color & Color::Blue)
response += "Blue | ";
//do this for as many colors as you wish
if(response.empty())
response = "Unknown Color";
else
response.erase(response.end() - 3, response.end());
return response;
}
Then make another EnumToStr function for each enum you want to do this to, following the same form 然后按照相同的表单为要执行此操作的每个枚举创建另一个EnumToStr函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.