如何在 C 语言中使用 Google 的 Protocol Buffer 添加重复字段?

发布于 2024-08-12 03:18:22 字数 1061 浏览 4 评论 0原文

我有以下协议缓冲区。请注意,StockStatic 是一个重复字段。

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 是可选字段而不是重复字段时,上述代码才是正确的。我的问题是我缺少哪行代码才能使其成为重复字段?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〆一缕阳光ご 2024-08-19 03:18:22

不,你做的是正确的事。

这是我的协议缓冲区的片段(为简洁起见,省略了详细信息):

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 现在包含 demList.size()元素。

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.

允世 2024-08-19 03:18:22

这里是 C++ 示例代码,但可能效率不高:

message MyArray
{
    repeated uint64 my_data = 1;
}
//Copy
std::array<unsigned long long, 5> test={1,1,2,3,5};
mynamespace::MyArray pbvar;
auto *dst_ptr = keys.my_data();
google::protobuf::RepeatedField<google::protobuf::uint64> field{test.begin(), test.end()};
dst_ptr->CopyFrom(field);

//Output
for (auto it : pbvar.my_data())
    std::cout<<it<<" ";
std::cout<<std::endl;

Here the c++ sample code but may not efficient:

message MyArray
{
    repeated uint64 my_data = 1;
}
//Copy
std::array<unsigned long long, 5> test={1,1,2,3,5};
mynamespace::MyArray pbvar;
auto *dst_ptr = keys.my_data();
google::protobuf::RepeatedField<google::protobuf::uint64> field{test.begin(), test.end()};
dst_ptr->CopyFrom(field);

//Output
for (auto it : pbvar.my_data())
    std::cout<<it<<" ";
std::cout<<std::endl;
梦断已成空 2024-08-19 03:18:22

完成同样事情的另一种方法:

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

Another way of accomplishing the same thing:

message SearchResponse {
  message Result {
  required string url = 1;
  optional string title = 2;
  repeated string snippets = 3;
  }  
  repeated Result result = 1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文