简体   繁体   English

在C ++中使用protobuf和POCO

[英]Working with protobuf and POCOs in C++

I would like to use protobuf with a C++ project I'm working on. 我想将protobuf与我正在研究的C ++项目一起使用。 However, I don't like to work with the auto-generated classes protoc creates and prefer to stick with the POCOs I already have. 但是,我不喜欢使用protoc创建的自动生成的类,并且更喜欢坚持我已经拥有的POCO。 This is because the POCOs are already in use in other parts of the code and I want to be able to switch the serialization mechanism with ease later on. 这是因为POCO已经在代码的其他部分中使用,我希望以后能够轻松切换序列化机制。 But manually writing converters between POCOs and protobuf message classes seems tedious and wrong. 但是在POCO和protobuf消息类之间手动编写转换器似乎很乏味和错误。

I want to know if there's a way to use protobuf to create a serializer - an auto-generated class that will be able to serialize and deserialize my POCOs, without bugging me with internals. 我想知道是否有一种方法可以使用protobuf来创建一个序列化程序 - 一个自动生成的类,它能够序列化和反序列化我的POCO,而不会让我厌烦内部。

Thanks. 谢谢。

First, you may like Cap'n Proto better, it was created by one of Google's former Google Protocol Buffer maintainers. 首先,你可能更喜欢Cap'n Proto ,它是由Google之前的Google Protocol Buffer维护者创建的。 Worth looking into, anyway. 值得关注的是,无论如何。

But otherwise, you really need to consider why you're using Google Protocol Buffers. 但除此之外,您真的需要考虑为什么要使用Google Protocol Buffers。

If you want to achieve the forward and backward compatibility, and to be able to open, then edit, then save an object that possibly a different person created, with a different version of your protocol buffer declaration, and then sent along to yet another person with an even different version of the declaration... then you need to just bite the bullet and use the generated C++ from the Google Protocol Buffer Compiler. 如果要实现向前和向后兼容,并且能够打开,然后编辑,然后使用不同版本的协议缓冲区声明保存可能创建不同人员的对象,然后发送给另一个人使用甚至不同版本的声明......然后你需要咬紧牙关并使用Google协议缓冲编译器生成的C ++。

It's really not just a serialization format. 它实际上不仅仅是一种序列化格式。 It's specifically designed to make it easy living with different versions of your serialization, over time. 它经过专门设计,可以随着时间的推移轻松实现不同版本的序列化。

If you don't need that flexibility, and you don't like the generated code, you may want to consider a different serialization tool. 如果您不需要这种灵活性,并且不喜欢生成的代码,则可能需要考虑使用其他序列化工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM