Created
October 16, 2014 03:31
-
-
Save redboltz/3ef4718a9e4429b21e21 to your computer and use it in GitHub Desktop.
msgpack-c user overloading example.
This file contains hidden or 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 <sstream> | |
#include <iostream> | |
// ------------------------------------------------- | |
// You need to include msgpack_fwd.hpp for overload. | |
#include <msgpack_fwd.hpp> | |
// ------------------------------------------------- | |
struct my { | |
int i; | |
}; | |
namespace msgpack { | |
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) { | |
// --------------------------------------- | |
// Insert your overload declarations here. | |
// --------------------------------------- | |
object const& operator>> (object const& o, my& v); | |
template <typename Stream> | |
packer<Stream>& operator<< (packer<Stream>& o, my const& v); | |
void operator<< (object::with_zone& o, my const& v); | |
void operator<< (object& o, my const& v); | |
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) | |
} // namespace msgpack | |
// -------------------------------------------------------------------------- | |
// You need to include msgpack.hpp after your overload function declarations. | |
#include <msgpack.hpp> | |
// -------------------------------------------------------------------------- | |
namespace msgpack { | |
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) { | |
// -------------------------------------- | |
// Insert your overload definitions here. | |
// -------------------------------------- | |
inline object const& operator>> (object const& o, my& v) { | |
std::cout << "object const& operator>> (object const& o, my& v)" << std::endl; | |
switch (o.type) { | |
case type::POSITIVE_INTEGER: | |
v.i = o.via.u64; | |
break; | |
case type::NEGATIVE_INTEGER: | |
v.i = o.via.i64; | |
break; | |
default: | |
throw type_error(); | |
break; | |
} | |
return o; | |
} | |
template <typename Stream> | |
inline packer<Stream>& operator<< (packer<Stream>& o, my const& v) { | |
std::cout << "packer<Stream>& operator<< (packer<Stream>& o, my const& v)" << std::endl; | |
o.pack(v.i); | |
return o; | |
} | |
inline void operator<< (object& o, my const& v) { | |
std::cout << "void operator<< (object& o, my const& v)" << std::endl; | |
if (v.i < 0) { | |
o.type = type::NEGATIVE_INTEGER; | |
o.via.i64 = v.i; | |
} | |
else { | |
o.type = type::POSITIVE_INTEGER; | |
o.via.u64 = v.i; | |
} | |
} | |
inline void operator<< (object::with_zone& o, my const& v) { | |
std::cout << "void operator<< (object::with_zone& o, my const& v)" << std::endl; | |
static_cast<object&>(o) << v; | |
} | |
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) | |
} // namespace msgpack | |
int main() { | |
my m; | |
m.i = 42; | |
std::cout << "--- pack ---" << std::endl; | |
std::stringstream ss; | |
msgpack::pack(ss, m); | |
std::cout << "--- unpack ---" << std::endl; | |
msgpack::unpacked unp; | |
msgpack::unpack(unp, ss.str().data(), ss.str().size()); | |
std::cout << "--- convert ---" << std::endl; | |
assert(unp.get().as<my>().i == 42); | |
std::cout << "--- create ---" << std::endl; | |
msgpack::zone z; | |
msgpack::object o(m, z); | |
std::cout << "--- convert ---" << std::endl; | |
assert(o.as<my>().i == 42); | |
} | |
// output | |
// | |
// --- pack --- | |
// packer<Stream>& operator<< (packer<Stream>& o, my const& v) | |
// --- unpack --- | |
// --- convert --- | |
// object const& operator>> (object const& o, my& v) | |
// --- create --- | |
// void operator<< (object::with_zone& o, my const& v) | |
// void operator<< (object& o, my const& v) | |
// --- convert --- | |
// object const& operator>> (object const& o, my& v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment