class_intrusive.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <string>
  10. #include <iostream>
  11. #include <iomanip>
  12. #include <sstream>
  13. #include <cassert>
  14. // When you want to adapt map instead of array, you can enable these macro definition.
  15. //
  16. // #define MSGPACK_USE_DEFINE_MAP
  17. #include <msgpack.hpp>
  18. struct my_base1 {
  19. int a;
  20. MSGPACK_DEFINE(a);
  21. };
  22. inline bool operator==(my_base1 const& lhs, my_base1 const& rhs) {
  23. return lhs.a == rhs.a;
  24. }
  25. struct my_base2 {
  26. std::string b;
  27. std::string c;
  28. MSGPACK_DEFINE(b, c);
  29. };
  30. inline bool operator==(my_base2 const& lhs, my_base2 const& rhs) {
  31. return lhs.b == rhs.b && lhs.c == rhs.c;
  32. }
  33. class my_class : public my_base1, private my_base2 {
  34. public:
  35. my_class() {} // When you want to convert from msgpack::object to my_class,
  36. // my_class should be default constractible.
  37. my_class(std::string const& name, int age):name_(name), age_(age) {}
  38. void set_b(std::string const& str) { b = str; }
  39. void set_c(std::string const& str) { c = str; }
  40. friend bool operator==(my_class const& lhs, my_class const& rhs) {
  41. return
  42. static_cast<my_base1 const&>(lhs) == static_cast<my_base1 const&>(rhs) &&
  43. static_cast<my_base2 const&>(lhs) == static_cast<my_base2 const&>(rhs) &&
  44. lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_;
  45. }
  46. private:
  47. std::string name_;
  48. int age_;
  49. public:
  50. MSGPACK_DEFINE(name_, age_, MSGPACK_BASE(my_base1), MSGPACK_BASE(my_base2));
  51. };
  52. void print(std::string const& buf) {
  53. for (std::string::const_iterator it = buf.begin(), end = buf.end();
  54. it != end;
  55. ++it) {
  56. std::cout
  57. << std::setw(2)
  58. << std::hex
  59. << std::setfill('0')
  60. << (static_cast<int>(*it) & 0xff)
  61. << ' ';
  62. }
  63. std::cout << std::dec << std::endl;
  64. }
  65. int main() {
  66. { // pack, unpack
  67. my_class my("John Smith", 42);
  68. my.a = 123;
  69. my.set_b("ABC");
  70. my.set_c("DEF");
  71. std::stringstream ss;
  72. msgpack::pack(ss, my);
  73. print(ss.str());
  74. msgpack::object_handle oh =
  75. msgpack::unpack(ss.str().data(), ss.str().size());
  76. msgpack::object obj = oh.get();
  77. std::cout << obj << std::endl;
  78. assert(obj.as<my_class>() == my);
  79. }
  80. { // create object with zone
  81. my_class my("John Smith", 42);
  82. my.a = 123;
  83. my.set_b("ABC");
  84. my.set_c("DEF");
  85. msgpack::zone z;
  86. msgpack::object obj(my, z);
  87. std::cout << obj << std::endl;
  88. assert(obj.as<my_class>() == my);
  89. }
  90. }