簡體   English   中英

如何在C ++上的protobuf中使用反射將預分配的消息設置為字段?

[英]How to set a preallocated message as field using reflection in protobuf on C++?

我有這樣的代碼:

TestMessage* output;
::google::protobuf::Message* input;
// ...
auto extension_field = input->GetDescriptor()->extension(i);
// ...
auto reflection = output->GetReflection();
reflection->MutableMessage(output, extension_field)->CopyFrom(*input);

此代碼接收兩條消息,檢查一條是另一條消息的擴展,將輸入消息復制到輸出消息的相應擴展字段。

我想優化它 - 並用“移動”替換復制。 我怎么能用reflection來做呢?

最接近的是使用Reflection::Swap代替CopyFrom 頂級對象不會被消耗,但它的所有子對象(字符串,子消息等)都將被消耗。

auto msg = reflection->MutableMessage(output, output_field);
msg->GetReflection()->Swap(msg, input);
delete input;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM