[英]How to use Union Types Protobuf C++
I am new in using Protobuf. 我是使用Protobuf的新手。 I have a server client communication (UDP) in C++.
我在C ++中有一个服务器客户端通信(UDP)。 Now I use Protobuf to send a message which contains some Information to the Server.
现在我使用Protobuf向服务器发送包含一些信息的消息。
package Test;
message vName{
required int32 name = 1;
}
message vNat{
required int32 nat = 1;
}
message vTan{
required int32 tan = 1;
}
message Test{
enum Type { vName = 1; vNAT = 2; vTAN = 3;}
required Type type = 1;
optional vName name = 2;
optional vNat nat = 2;
optional vTan tan = 2;
}
Now i want only send the Information which is set. 现在我只想发送设置的信息。 For example Type is 1. Then how can i access or set the name?
例如,Type为1.那么我如何访问或设置名称?
Can anybody make an small snippet that i can understand how to use it? 任何人都可以制作一个我能理解如何使用它的小片段吗?
I apologize for my english skills :D 我为我的英语技能道歉:D
Protobuf version: 2.5.0 Protobuf版本:2.5.0
OS: Windows Enviroment: Visual Studio 操作系统:Windows 环境:Visual Studio
Language: C++ 语言:C ++
From https://developers.google.com/protocol-buffers/docs/techniques#union You may also want to have an enum field that identifies which message is filled in, so that you can switch on it: 来自https://developers.google.com/protocol-buffers/docs/techniques#union您可能还希望有一个枚举字段,用于标识填写了哪条消息,以便您可以启用它:
message OneMessage {
enum Type { FOO = 1; BAR = 2; BAZ = 3; }
// Identifies which field is filled in.
required Type type = 1;
// One of the following will be filled in.
optional Foo foo = 2;
optional Bar bar = 3;
optional Baz baz = 4;
}
How can I use this in the Code? 我如何在代码中使用它? I think this is what I want.
我想这就是我想要的。 Have anybody an idea where i can find an example?
有谁知道我在哪里可以找到一个例子?
听起来你正在寻找的是这个 ,而不是使用可选字段和变通方法枚举。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.