简体   繁体   中英

How to use Union Types Protobuf C++

I am new in using Protobuf. I have a server client communication (UDP) in C++. Now I use Protobuf to send a message which contains some Information to the Server.

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?

Can anybody make an small snippet that i can understand how to use it?

I apologize for my english skills :D

Protobuf version: 2.5.0

OS: Windows Enviroment: Visual Studio

Language: 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:

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?

听起来你正在寻找的是这个 ,而不是使用可选字段和变通方法枚举。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM