// MessagePack for C++ example // // Copyright (C) 2015 KONDO Takatoshi // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // #include #include #include #include #include #include #include #include #include #include void array() { std::array a { { 1, 2, 3, 4, 5 } }; std::stringstream ss; msgpack::pack(ss, a); msgpack::object_handle oh = msgpack::unpack(ss.str().data(), ss.str().size()); msgpack::object obj = oh.get(); std::cout << obj << std::endl; assert((obj.as>()) == a); } void tuple() { std::tuple t(true, "ABC", 42); std::stringstream ss; msgpack::pack(ss, t); auto const& str = ss.str(); auto oh = msgpack::unpack(str.data(), str.size()); auto obj = oh.get(); std::cout << obj << std::endl; assert(obj.as() == t); } void unordered_map() { std::unordered_map m { {"ABC", 1}, {"DEF", 3} }; std::stringstream ss; msgpack::pack(ss, m); auto const& str = ss.str(); auto oh = msgpack::unpack(str.data(), str.size()); msgpack::object obj = oh.get(); std::cout << obj << std::endl; assert(obj.as() == m); } void unordered_set() { std::unordered_set s { "ABC", "DEF" }; std::stringstream ss; msgpack::pack(ss, s); auto const& str = ss.str(); auto oh = msgpack::unpack(str.data(), str.size()); auto obj = oh.get(); std::cout << obj << std::endl; assert(obj.as() == s); } void forward_list() { using type = std::forward_list; type f { "ABC", "DEF" }; std::stringstream ss; msgpack::pack(ss, f); auto const& str = ss.str(); auto oh = msgpack::unpack(str.data(), str.size()); auto obj = oh.get(); std::cout << obj << std::endl; assert(obj.as() == f); } void combi() { std::array a { { 1, 2, 3, 4, 5 } }; std::tuple t {true, "ABC", 42}; std::unordered_map m { {"ABC", 1}, {"DEF", 3} }; std::unordered_set s { "ABC", "DEF" }; std::forward_list f { "ABC", "DEF" }; std::stringstream ss; msgpack::pack(ss, a); msgpack::pack(ss, t); msgpack::pack(ss, m); msgpack::pack(ss, s); msgpack::pack(ss, f); std::size_t offset = 0; std::cout << "offset: " << offset << std::endl; { auto const& str = ss.str(); auto oh = msgpack::unpack(str.data(), str.size(), offset); auto obj = oh.get(); std::cout << obj << std::endl; assert(obj.as() == a); } std::cout << "offset: " << offset << std::endl; { auto const& str = ss.str(); auto oh = msgpack::unpack(str.data(), str.size(), offset); auto obj = oh.get(); std::cout << obj << std::endl; assert(obj.as() == t); } std::cout << "offset: " << offset << std::endl; { auto const& str = ss.str(); auto oh = msgpack::unpack(str.data(), str.size(), offset); auto obj = oh.get(); std::cout << obj << std::endl; assert(obj.as() == m); } std::cout << "offset: " << offset << std::endl; { auto const& str = ss.str(); auto oh = msgpack::unpack(str.data(), str.size(), offset); auto obj = oh.get(); std::cout << obj << std::endl; assert(obj.as() == s); } std::cout << "offset: " << offset << std::endl; { auto const& str = ss.str(); auto oh = msgpack::unpack(str.data(), str.size(), offset); auto obj = oh.get(); std::cout << obj << std::endl; assert(obj.as() == f); } std::cout << "offset: " << offset << std::endl; } int main() { array(); tuple(); unordered_map(); unordered_set(); forward_list(); combi(); }