msgpack_variant_capitalize.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <sstream>
  11. #include <iostream>
  12. #include <algorithm>
  13. #include <cctype>
  14. #include <msgpack.hpp>
  15. struct user {
  16. std::string name;
  17. int age;
  18. std::string address;
  19. MSGPACK_DEFINE(name, age, address);
  20. };
  21. struct proc:boost::static_visitor<void> {
  22. void operator()(std::string& v) const {
  23. std::cout << " match std::string& v" << std::endl;
  24. std::cout << " v: " << v << std::endl;
  25. std::cout << " capitalize" << std::endl;
  26. for (std::string::iterator it = v.begin(), end = v.end();
  27. it != end;
  28. ++it) {
  29. *it = std::toupper(*it);
  30. }
  31. }
  32. void operator()(std::vector<msgpack::type::variant>& v) const {
  33. std::cout << "match vector (msgpack::type::ARRAY)" << std::endl;
  34. std::vector<msgpack::type::variant>::iterator it = v.begin();
  35. std::vector<msgpack::type::variant>::const_iterator end = v.end();
  36. for (; it != end; ++it) {
  37. boost::apply_visitor(*this, *it);
  38. }
  39. }
  40. template <typename T>
  41. void operator()(T const&) const {
  42. std::cout << " match others" << std::endl;
  43. }
  44. };
  45. void print(std::string const& buf) {
  46. for (std::string::const_iterator it = buf.begin(), end = buf.end();
  47. it != end;
  48. ++it) {
  49. std::cout
  50. << std::setw(2)
  51. << std::hex
  52. << std::setfill('0')
  53. << (static_cast<int>(*it) & 0xff)
  54. << ' ';
  55. }
  56. std::cout << std::dec << std::endl;
  57. }
  58. int main() {
  59. std::stringstream ss1;
  60. user u;
  61. u.name = "Takatoshi Kondo";
  62. u.age = 42;
  63. u.address = "Tokyo, JAPAN";
  64. std::cout << "Packing object." << std::endl;
  65. msgpack::pack(ss1, u);
  66. print(ss1.str());
  67. msgpack::object_handle oh1 = msgpack::unpack(ss1.str().data(), ss1.str().size());
  68. msgpack::object const& obj1 = oh1.get();
  69. std::cout << "Unpacked msgpack object." << std::endl;
  70. std::cout << obj1 << std::endl;
  71. msgpack::type::variant v = obj1.as<msgpack::type::variant>();
  72. std::cout << "Applying proc..." << std::endl;
  73. boost::apply_visitor(proc(), v);
  74. std::stringstream ss2;
  75. std::cout << "Packing modified object." << std::endl;
  76. msgpack::pack(ss2, v);
  77. print(ss2.str());
  78. msgpack::object_handle oh2 = msgpack::unpack(ss2.str().data(), ss2.str().size());
  79. msgpack::object const& obj2 = oh2.get();
  80. std::cout << "Modified msgpack object." << std::endl;
  81. std::cout << obj2 << std::endl;
  82. }