2009-11-20 4 views
11

아래 프로토콜 버퍼가 있습니다. StockStatic은 반복 필드입니다.Google 프로토콜 버퍼 반복 필드 C++

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; 
} 

벡터를 반복하면서 StockStatic 필드를 채우고 있습니다.

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) 
} 

그러나 위의 코드는 StockStatic이 선택 필드이고 반복 필드가 아닌 경우에만 적합합니다. 내 질문은 반복되는 필드로 만들기 위해 누락 된 코드 줄은 무엇입니까?

+0

궁금한 점이 있다면 반복 할 때 직면하게되는 문제는 무엇일까요? ** 반복 된 문자열 스 니펫 **에 대한 – VNarasimhaM

답변

13

아니, 당신이 옳은 일을하고 있습니다.

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

... 그리고 ComputeResponse :: solutionInfo 채울 수있는 C++ :

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 지금 포함을 여기

은 (간결 ommited 세부 사항) 내 PB의 조각이다 demList.size() 요소.

+0

고마워 .. 난 그냥 response-> add_solutioninfo()에 대한 호출을 깨달았다 ---- 하나 이상의 요소를 추가 .. 감사합니다! – aajkaltak

0

같은 일을 달성하는 또 다른 방법 :

message SearchResponse { 
    message Result { 
    required string url = 1; 
    optional string title = 2; 
    repeated string snippets = 3; 
    } 
    repeated Result result = 1; 
} 
+0

** cpp 코드는 무엇입니까? –