Skip to content

Instantly share code, notes, and snippets.

@redboltz
Created November 12, 2017 07:47
Show Gist options
  • Save redboltz/434cb20debf825f658260831250362b8 to your computer and use it in GitHub Desktop.
Save redboltz/434cb20debf825f658260831250362b8 to your computer and use it in GitHub Desktop.
#include <vector>
#include <sstream>
#include <msgpack.hpp>
namespace foo {
struct no_def_con {
no_def_con() = delete;
MSGPACK_DEFINE(i1, i2, v);
int i1;
int i2;
std::vector<no_def_con> v;
};
}
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
namespace adaptor {
template <>
struct as<foo::no_def_con> {
foo::no_def_con operator()(msgpack::object const& o) const {
if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
if (o.via.array.size != 3) throw msgpack::type_error();
return foo::no_def_con{
o.via.array.ptr[0].as<int>(),
o.via.array.ptr[1].as<int>(),
o.via.array.ptr[2].as<std::vector<foo::no_def_con>>()};
}
};
} // adaptor
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
} // msgpack
int main() {
std::stringstream ss;
foo::no_def_con v{42, 43, { {1, 2, {}}, {2, 3, {}} } };
msgpack::pack(ss, v);
auto oh = msgpack::unpack(ss.str().data(), ss.str().size());
auto ndc = oh.get().as<foo::no_def_con>();
assert(ndc.i1 == 42);
assert(ndc.v[0].i1 == 1);
assert(ndc.v[1].i1 == 2);
}
#include <vector>
#include <sstream>
#include <msgpack.hpp>
namespace foo {
struct no_def_con {
no_def_con() = delete;
MSGPACK_DEFINE_MAP(i1, i2, v);
int i1;
int i2;
std::vector<no_def_con> v;
};
}
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
namespace adaptor {
template <>
struct as<foo::no_def_con> {
foo::no_def_con operator()(msgpack::object const& o) const {
if (o.type != msgpack::type::MAP) throw msgpack::type_error();
if (o.via.map.size != 3) throw msgpack::type_error();
return foo::no_def_con{
o.via.map.ptr[0].val.as<int>(),
o.via.map.ptr[1].val.as<int>(),
o.via.map.ptr[2].val.as<std::vector<foo::no_def_con>>()};
}
};
} // adaptor
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
} // msgpack
int main() {
std::stringstream ss;
foo::no_def_con v{42, 43, { {1, 2, {}}, {2, 3, {}} } };
msgpack::pack(ss, v);
auto oh = msgpack::unpack(ss.str().data(), ss.str().size());
auto ndc = oh.get().as<foo::no_def_con>();
assert(ndc.i1 == 42);
assert(ndc.v[0].i1 == 1);
assert(ndc.v[1].i1 == 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment