Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add a repeated field using Google's Protocol Buffer in C++?

I have the below protocol buffer. Note that StockStatic is a repeated field.

message ServiceResponse {     enum Type     {         REQUEST_FAILED = 1;         STOCK_STATIC_SNAPSHOT = 2;     }      message StockStaticSnapshot     {         repeated StockStatic stock_static = 1;     }     required Type type = 1;     optional StockStaticSnapshot stock_static_snapshot = 2; }  message StockStatic {     optional string sector      = 1;     optional string subsector   = 2; } 

I am filling out the StockStatic fields while iterating through a vector.

ServiceResponse.set_type(ServiceResponse_Type_STOCK_STATIC_SNAPSHOT);  ServiceResponse_StockStaticSnapshot stockStaticSnapshot;  for (vector<stockStaticInfo>::iterator it = m_staticStocks.begin(); it!= m_staticStocks.end(); ++it) {     StockStatic* pStockStaticEntity = stockStaticSnapshot.add_stock_static();      SetStockStaticProtoFields(*it, pStockStaticEntity); // sets sector and subsector field to pStockStaticEntity by reading the fields using (*it) } 

But the above code is right only if StockStatic was an optional field and not a repeated field. My questions is what line of code am i missing to make it a repeated field?

like image 927
aajkaltak Avatar asked Nov 20 '09 14:11

aajkaltak


People also ask

What is repeated field in protobuf?

repeated : this field can be repeated any number of times (including zero) in a well-formed message. The order of the repeated values will be preserved.

Are repeated fields ordered in protobuf?

Yes, repeated fields retain the order of items. From Google's Protocol Buffers encoding specification: The order of the elements with respect to each other is preserved when parsing, though the ordering with respect to other fields is lost.

How do you assign a repeated field?

As per the documentation, you aren't able to directly assign to a repeated field. In this case, you can call extend to add all of the elements in the list to the field. Similarly, for adding a single value, use append() , e.g. person.

What is protocol buffer in C#?

Protobuf is used to serialize data structure efficiently. It is faster, smaller & simpler than XML. Protobuf is useful in developing programs to communicate with each other over a wire and it is created by Google. It helps us in creating gRPC services.


1 Answers

No, you're doing the right thing.

Here's a snippet of my protocol buffer (details omitted for brevity):

message DemandSummary {     required uint32 solutionIndex     = 1;     required uint32 demandID          = 2; } message ComputeResponse {     repeated DemandSummary solutionInfo  = 3; } 

...and the C++ to fill up ComputeResponse::solutionInfo:

ComputeResponse response;  for ( int i = 0; i < demList.size(); ++i ) {      DemandSummary* summary = response.add_solutioninfo();     summary->set_solutionindex(solutionID);     summary->set_demandid(demList[i].toUInt()); } 

response.solutionInfo now contains demList.size() elements.

like image 157
leegent Avatar answered Oct 11 '22 15:10

leegent