Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MSGPACK_DEFINE without changing class declarations

Is there a way in MsgPack C++ to use MSGPACK_DEFINE without altering the class members? We'd like to keep message pack stuff out of the headers, and only use it internally in a library.

It seems like just wrapping each class would work, but am hoping there is a better way.

like image 655
Noah Watkins Avatar asked Nov 04 '22 04:11

Noah Watkins


1 Answers

UPD. Alternatively you can use MSGPACK_DEFINE_EXTERNAL macro I wrote.

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

Just #include "define_external.hpp", and then call MSGPACK_DEFINE_EXTERNAL passing in the class and its members you want to be serialized/deserialized.

For example:

MSGPACK_DEFINE_EXTERNAL(v3f, X, Y, Z);

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


To achieve this in my project I just defined operator>> and operator<<. This is not as straightforward as just 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(v.X);
    o.pack(v.Y);
    o.pack(v.Z);
    return o;
}

}

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

like image 168
xyz Avatar answered Nov 15 '22 22:11

xyz