reuse_zone.cpp 1019 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 <iostream>
  10. #include <sstream>
  11. #include <cassert>
  12. #include <string>
  13. #include <vector>
  14. #include <msgpack.hpp>
  15. int main() {
  16. std::vector<int> v;
  17. v.push_back(1);
  18. v.push_back(42);
  19. std::string s("ABC");
  20. std::stringstream ss;
  21. msgpack::pack(ss, v);
  22. msgpack::pack(ss, s);
  23. msgpack::zone z;
  24. std::size_t offset = 0;
  25. // msgpack array is constructed on z.
  26. msgpack::object obj = msgpack::unpack(z, ss.str().data(), ss.str().size(), offset);
  27. std::cout << obj << std::endl;
  28. assert(obj.as<std::vector<int> >() == v);
  29. // msgpack str is constructed on z.
  30. std::string const& str = msgpack::unpack(z, ss.str().data(), ss.str().size(), offset).as<std::string>();
  31. std::cout << str << std::endl;
  32. assert(str == s);
  33. }