如何在 protobuf 中使用反射将预分配的消息设置为字段C++
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;
相关文章:
- STL算法和back_inserter可以预分配空间吗?
- 重复使用预分配的向量<复杂<double>>作为<double>长度两倍的向量
- 如何将google::p rotobuf::消息直接序列化到预分配的内存中?
- 预分配的节点向量中的无锁树节点分配
- 在不放置新运算符的情况下,在预分配的内存上使用虚函数初始化对象 - 这可能吗?如果没有,为什么
- 在预分配的内存中移动数据
- 具有预分配结果C++的重载加运算符
- 为循环中多次调用的函数返回值预分配内存的正确方法是什么
- 将稀疏 mat-vec-mult 的结果存储到预分配的向量中
- libpq错误消息解除分配
- 请求提升池体验.它作为带有预分配的分配器有用吗
- std::向量预分配(大小 n,容量 n + 2)
- 如何预分配(预留)priority_queue<vector>?
- 预分配向量是否更有效
- 为什么预分配的函数指针的性能比分支差
- 预分配列表
- 如何在 protobuf 中使用反射将预分配的消息设置为字段C++
- 将元素添加到基于范围的预分配向量中是否合法
- 预分配内存空间供程序使用
- 正在预分配要传递到WinAPI中的std::字符串