一直在尋找c++的自動(dòng)序列化庫(kù),希望能夠自動(dòng)生成序列化代碼,像ICE、gSoap都有實(shí)現(xiàn)。gSoap主要是它只能序列化成基于xml的soap格式數(shù)據(jù);而ICE好是好,可惜吃不起。最近有朋友推薦Protocol Buffer,這個(gè)東西基本上附合我的要求,可以序列化成高效的二進(jìn)制格式,如果能夠提供序列化成文本(如:XML)的功能,會(huì)便于調(diào)試。
手冊(cè)上有一段話(huà)初看讓人疑惑:
Protocol Buffers and O-O Design Protocol buffer classes are basically dumb data holders (like structs in C++); they don't make good first class citizens in an object model. If you want to add richer behaviour to a generated class, the best way to do this is to wrap the generated protocol buffer class in an application-specific class. Wrapping protocol buffers is also a good idea if you don't have control over the design of the .proto file (if, say, you're reusing one from another project). In that case, you can use the wrapper class to craft an interface better suited to the unique environment of your application: hiding some data and methods, exposing convenience functions, etc. You should never add behaviour to the generated classes by inheriting from them. This will break internal mechanisms and is not good object-oriented practice anyway.
大意是不要從它生成的Message類(lèi)進(jìn)行派生,以便加入一些行為函數(shù),而應(yīng)該采用包裝(wrap)的方式,說(shuō)白了就是組合(在你的類(lèi)中放一個(gè)生成的Message類(lèi)成員變量)。通過(guò)查看郵件列表,載錄以下詳細(xì)解釋?zhuān)?br /> *如果進(jìn)行派生,那么你的類(lèi)中會(huì)混入Protocol Buffer生成的一些方法,而這些方法將來(lái)還可能會(huì)變化,
 這就意味著你的類(lèi)將依賴(lài)Protocol Buffer的實(shí)現(xiàn)。用設(shè)計(jì)相關(guān)的術(shù)語(yǔ)就是:你繼承了實(shí)現(xiàn),你把業(yè)務(wù)和數(shù)據(jù)混在了一起。