mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-04-24 17:18:33 +02:00

Removed obsolete unpack functions. Updated examples that no longer use obsolete functions. Added reference checking function to unpacked. ( unpacked::referenced() ) Added std:: namespace. Added reference or copy choice function and default behavior: When you use unpacker, default behavior is: STR, BIN, EXT types are always held by reference. When you don't use unpacker, default behavior is: STR, BIN, EXT types are always held by copy. The memory is allocated from zone. You can customize the behavior passing your custom judging function to unpack() or unpacker's constructor.
59 lines
1.1 KiB
C++
59 lines
1.1 KiB
C++
#include <msgpack.hpp>
|
|
#include <string>
|
|
#include <iostream>
|
|
|
|
class old_class {
|
|
public:
|
|
old_class() : value("default") { }
|
|
|
|
std::string value;
|
|
|
|
MSGPACK_DEFINE(value);
|
|
};
|
|
|
|
class new_class {
|
|
public:
|
|
new_class() : value("default"), flag(-1) { }
|
|
|
|
std::string value;
|
|
int flag;
|
|
|
|
MSGPACK_DEFINE(value, flag);
|
|
};
|
|
|
|
int main(void)
|
|
{
|
|
{
|
|
old_class oc;
|
|
new_class nc;
|
|
|
|
msgpack::sbuffer sbuf;
|
|
msgpack::pack(sbuf, oc);
|
|
|
|
msgpack::unpacked result;
|
|
msgpack::unpack(result, sbuf.data(), sbuf.size());
|
|
msgpack::object obj = result.get();
|
|
|
|
obj.convert(&nc);
|
|
|
|
std::cout << obj << " value=" << nc.value << " flag=" << nc.flag << std::endl;
|
|
}
|
|
|
|
{
|
|
new_class nc;
|
|
old_class oc;
|
|
|
|
msgpack::sbuffer sbuf;
|
|
msgpack::pack(sbuf, nc);
|
|
|
|
msgpack::unpacked result;
|
|
msgpack::unpack(result, sbuf.data(), sbuf.size());
|
|
msgpack::object obj = result.get();
|
|
|
|
obj.convert(&oc);
|
|
|
|
std::cout << obj << " value=" << oc.value << std::endl;
|
|
}
|
|
}
|
|
|