Skip to content

Instantly share code, notes, and snippets.

@redboltz
Created April 9, 2015 11:05
Show Gist options
  • Save redboltz/ce47fac1cd6efe01fbd1 to your computer and use it in GitHub Desktop.
Save redboltz/ce47fac1cd6efe01fbd1 to your computer and use it in GitHub Desktop.
msgpack base class support
#include <msgpack.hpp>
#include <sstream>
#include <cassert>
#include <iostream>
struct A {
int a;
MSGPACK_DEFINE(a);
};
struct B : A {
int b;
MSGPACK_DEFINE(MSGPACK_BASE(A), b);
};
struct C : B {
int c;
MSGPACK_DEFINE(MSGPACK_BASE(B), c);
};
namespace msgpack {
inline msgpack::object base_of(msgpack::object const& o) {
return o.via.array.ptr[0];
}
}
int main() {
C c;
c.a = 1;
c.b = 2;
c.c = 3;
std::stringstream ss;
msgpack::pack(ss, c);
auto upd = msgpack::unpack(ss.str().data(), ss.str().size());
auto obj = upd.get();
A a2 = obj.via.array.ptr[0].via.array.ptr[0].as<A>();
B b2 = obj.via.array.ptr[0].as<B>();
C c2 = obj.as<C>();
std::cout << obj << std::endl;
assert(c.a == a2.a);
assert(c.a == b2.a);
assert(c.b == b2.b);
assert(c.a == c2.a);
assert(c.b == c2.b);
assert(c.c == c2.c);
A a3 = msgpack::base_of(msgpack::base_of(obj)).as<A>();
B b3 = msgpack::base_of(obj).as<B>();
assert(c.a == a3.a);
assert(c.a == b3.a);
assert(c.b == b3.b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment