Created
November 12, 2017 07:47
-
-
Save redboltz/434cb20debf825f658260831250362b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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