map_based_versionup.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // MessagePack for C++ example
  2. //
  3. // Copyright (C) 2015 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. #include <msgpack.hpp>
  15. struct base1 {
  16. base1():a("default") {}
  17. std::string a;
  18. MSGPACK_DEFINE_MAP(a);
  19. };
  20. struct v1 : base1 {
  21. v1():name("default"), age(0) {}
  22. std::string name;
  23. int age;
  24. MSGPACK_DEFINE_MAP(MSGPACK_BASE_MAP(base1), name, age);
  25. };
  26. struct base2 {
  27. base2():a("default") {}
  28. std::string a;
  29. MSGPACK_DEFINE_MAP(a);
  30. };
  31. // Removed: base1, name
  32. // Added : base2, address
  33. struct v2 : base2 {
  34. v2(): age(0), address("default") {}
  35. int age;
  36. std::string address;
  37. MSGPACK_DEFINE_MAP(MSGPACK_BASE_MAP(base2), age, address);
  38. };
  39. // The member variable "age" is in common between v1 and v2.
  40. void print(std::string const& buf) {
  41. for (std::string::const_iterator it = buf.begin(), end = buf.end();
  42. it != end;
  43. ++it) {
  44. std::cout
  45. << std::setw(2)
  46. << std::hex
  47. << std::setfill('0')
  48. << (static_cast<int>(*it) & 0xff)
  49. << ' ';
  50. }
  51. std::cout << std::dec << std::endl;
  52. }
  53. int main() {
  54. { // pack v1, unpack, convert to v2
  55. v1 v;
  56. v.a = "ABC";
  57. v.name = "John Smith";
  58. v.age = 35;
  59. std::stringstream ss;
  60. msgpack::pack(ss, v);
  61. print(ss.str());
  62. msgpack::object_handle oh = msgpack::unpack(ss.str().data(), ss.str().size());
  63. msgpack::object obj = oh.get();
  64. std::cout << obj << std::endl;
  65. v2 newv = obj.as<v2>();
  66. std::cout << "v2::a " << newv.a << std::endl;
  67. std::cout << "v2::age " << newv.age << std::endl;
  68. std::cout << "v2::address " << newv.address << std::endl;
  69. // "age" is set from v1
  70. assert(newv.a == "default");
  71. assert(newv.age == 35);
  72. assert(newv.address == "default");
  73. }
  74. { // create v2 object with zone, convert to v1
  75. v2 v;
  76. v.a = "DEF";
  77. v.age = 42;
  78. v.address = "Tokyo";
  79. msgpack::zone z;
  80. msgpack::object obj(v, z);
  81. std::cout << obj << std::endl;
  82. v1 newv = obj.as<v1>();
  83. std::cout << "v1::a " << newv.a << std::endl;
  84. std::cout << "v1::name " << newv.name << std::endl;
  85. std::cout << "v1::age " << newv.age << std::endl;
  86. // "age" is set from v2
  87. assert(newv.a == "default");
  88. assert(newv.name == "default");
  89. assert(newv.age == 42);
  90. }
  91. }