是否有可能在 C++ 中获得与 protobuf 枚举等效的字符串?
例如:
以下是消息说明:
package MyPackage;
message MyMessage
{
enum RequestType
{
Login = 0;
Logout = 1;
}
optional RequestType requestType = 1;
}
在我的代码中,我希望做这样的事情:
MyMessage::RequestType requestType = MyMessage::RequestType::Login;
// requestTypeString will be "Login"
std::string requestTypeString = ProtobufEnumToString(requestType);
最佳答案
EnumDescriptor和 EnumValueDescriptor类可以用于这种操作,并且
生成的 .pb.h
和 .pb.cc
名称很容易阅读,因此您可以查看它们以获取有关它们提供的功能的详细信息。
在这种特殊情况下,以下应该有效(未经测试):
std::string requestTypeString = MyMessage_RequestType_Name(requestType);
关于c++ - 如何将protobuf枚举作为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32161409/