custom.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // MessagePack for C++ example
  2. //
  3. // Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. #include <msgpack.hpp>
  10. #include <sstream>
  11. #include <string>
  12. #include <iostream>
  13. class old_class {
  14. public:
  15. old_class() : value("default") { }
  16. std::string value;
  17. MSGPACK_DEFINE(value);
  18. };
  19. class new_class {
  20. public:
  21. new_class() : value("default"), flag(-1) { }
  22. std::string value;
  23. int flag;
  24. MSGPACK_DEFINE(value, flag);
  25. };
  26. int main(void)
  27. {
  28. {
  29. old_class oc;
  30. new_class nc;
  31. std::stringstream sbuf;
  32. msgpack::pack(sbuf, oc);
  33. msgpack::object_handle oh =
  34. msgpack::unpack(sbuf.str().data(), sbuf.str().size());
  35. msgpack::object obj = oh.get();
  36. obj.convert(nc);
  37. std::cout << obj << " value=" << nc.value << " flag=" << nc.flag << std::endl;
  38. }
  39. {
  40. new_class nc;
  41. old_class oc;
  42. std::stringstream sbuf;
  43. msgpack::pack(sbuf, nc);
  44. msgpack::object_handle oh =
  45. msgpack::unpack(sbuf.str().data(), sbuf.str().size());
  46. msgpack::object obj = oh.get();
  47. obj.convert(oc);
  48. std::cout << obj << " value=" << oc.value << std::endl;
  49. }
  50. }