c++ - 如何将protobuf枚举作为字符串?

标签 c++ enums protocol-buffers

是否有可能在 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);

最佳答案

EnumDescriptorEnumValueDescriptor类可以用于这种操作,并且 生成的 .pb.h.pb.cc 名称很容易阅读,因此您可以查看它们以获取有关它们提供的功能的详细信息。

在这种特殊情况下,以下应该有效(未经测试):

std::string requestTypeString = MyMessage_RequestType_Name(requestType);

关于c++ - 如何将protobuf枚举作为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32161409/

相关文章:

c++ - 派生类忽略基类函数

c# - boost proto vs c# 表达式树

C++ 当模板参数推导失败时

sorting - sortdir 不适用于 MVC 3 WebGird 中的枚举数据类型

c++ - Protocol Buffer - 存储双数组、1D、2D 和 3D

c++ - 带有谷歌时间戳的 Protobuf C++ 消息导致段错误

java protobuf如何从int创建ByteString

c++ - OpenGL C++ 遮挡查询

enums - 使用 FlowType 处理枚举

mysql - 数据库中的枚举