Using MSGPACK_DEFINE without changing class declarations

Is there a way in MsgPack C ++ to use MSGPACK_DEFINE without changing class members? We would like to save the message package material from the headers and use it only inside the library.

It seems that every class will work, but I hope there is a better way.

+4
source share
1 answer

UPD Alternatively, you can use the MSGPACK_DEFINE_EXTERNAL macro that I wrote.

The source .hpp.erb is available here , and the generated .hpp is.

Just #include "define_external.hpp" and then call MSGPACK_DEFINE_EXTERNAL , passing in the class and its members that you want to serialize / deserialize.

For instance:

 MSGPACK_DEFINE_EXTERNAL(v3f, X, Y, Z); 

I tested this header file to work with gcc 4.8.2, clang 3.3, and MSVC 2010.


To achieve this in my project, I simply defined operator>> and operator<< . It is not as simple as using MSGPACK_DEFINE , but it works.

 namespace msgpack { inline v3f& operator>> (object o, v3f& v) { if(o.type != type::ARRAY) { throw type_error(); } if(o.via.array.size != 3) { throw type_error(); } o.via.array.ptr[0].convert(&v.X); o.via.array.ptr[1].convert(&v.Y); o.via.array.ptr[2].convert(&v.Z); return v; } template <typename Stream> inline packer<Stream>& operator<< (packer<Stream>& o, const v3f& v) { o.pack_array(3); o.pack(vX); o.pack(vY); o.pack(vZ); return o; } } 

You can find more examples at src/msgpack/type/ .

+2
source

All Articles