Pet*_*lák 6 c++ deep-copy protocol-buffers c++11
我想复制一个通用的constprotobuf Message进行进一步操作.我想出来了
Message* myfn(const Message *msg) {
Message *copy = msg->New();
copy->CopyFrom(*msg);
// do stuff
return copy;
}
Run Code Online (Sandbox Code Playgroud)
这是正确/惯用的吗?或者有更好的方法(可能是C++ 11特定的)?
这是对的。
如果你想要类型安全,一点模板魔法可以帮助你:
template<class Msg,
std::enable_if_t<std::is_base_of<protobuf::Message,
Msg>::value>>
std::unique_ptr<Msg> clone(const Msg* msg)
{
std::unique_ptr<Msg> p(msg->New());
p->CopyFrom(*msg);
return p;
}
Run Code Online (Sandbox Code Playgroud)
您将看到我已将协议缓冲区对象包装在 unique_ptr 中。这提供了一些异常安全性并且具有可转换为shared_ptr的额外优点。
为什么这是个好主意?好吧,考虑一个名为 Foo 的协议缓冲区对象,它有 2 个名为 bar 和 baz 的字符串成员:
void test(const Foo* source_foo)
{
// clone and convert the unique_ptr to shared_ptr
std::shared_ptr<Foo> myclone(clone(source_foo));
myclone->bar() += " cloned";
myclone->baz() += " cloned again";
// get a shared_ptr to the members:
auto mybar = std::shared_ptr<const std::string>(myclone, &myclone->bar());
auto mybaz = std::shared_ptr<const std::string>(myclone, &myclone->baz());
give_away(mybar);
do_something_else(mybaz);
// at this point the use_count of myclone is at least 3.
// if give_away results in holding on to the shared_ptr
// to string then myclone will be kept
// alive until the last shared_ptr goes away
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6714 次 |
| 最近记录: |