|
| 1 | +// MessagePack for C++ example |
| 2 | +// |
| 3 | +// Copyright (C) 2015 KONDO Takatoshi |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +// |
| 17 | + |
| 18 | +#include <string> |
| 19 | +#include <sstream> |
| 20 | +#include <iostream> |
| 21 | + |
| 22 | +#include <msgpack.hpp> |
| 23 | + |
| 24 | +struct user { |
| 25 | + std::string name; |
| 26 | + int age; |
| 27 | + std::string address; |
| 28 | + MSGPACK_DEFINE_MAP(name, age, address); |
| 29 | +}; |
| 30 | + |
| 31 | +struct proc:boost::static_visitor<void> { |
| 32 | + // msgpack::type::MAP is converted to std::multimap, not std::map. |
| 33 | + void operator()(std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>& v) const { |
| 34 | + std::cout << "match map" << std::endl; |
| 35 | + std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>::iterator it = v.begin(); |
| 36 | + std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>::iterator end = v.end(); |
| 37 | + while(it != end) { |
| 38 | + boost::string_ref const& key = it->first.as_boost_string_ref(); |
| 39 | + if (key == "name") { |
| 40 | + boost::string_ref const& value = it->second.as_boost_string_ref(); |
| 41 | + if (value == "Takatoshi Kondo") { |
| 42 | + // You can add values to msgpack::type::variant_ref. |
| 43 | + v.insert( |
| 44 | + std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>::value_type( |
| 45 | + "role", |
| 46 | + "msgpack-c committer" |
| 47 | + ) |
| 48 | + ); |
| 49 | + } |
| 50 | + ++it; |
| 51 | + } |
| 52 | + else if (key == "age") { |
| 53 | + // You can remove key-value pair from msgpack::type::variant_ref |
| 54 | + |
| 55 | +#if defined(MSGPACK_USE_CPP03) |
| 56 | + v.erase(it++); |
| 57 | +#else // defined(MSGPACK_USE_CPP03) |
| 58 | +# if MSGPACK_LIB_STD_CXX |
| 59 | + it = v.erase(std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>::const_iterator(it)); |
| 60 | +# else // MSGPACK_LIB_STD_CXX |
| 61 | + it = v.erase(it); |
| 62 | +# endif // MSGPACK_LIB_STD_CXX |
| 63 | +#endif // defined(MSGPACK_USE_CPP03) |
| 64 | + } |
| 65 | + else if (key == "address") { |
| 66 | + // When you want to append string |
| 67 | + // "Tokyo" -> "Tokyo, JAPAN" |
| 68 | + // Use msgpack::type::variant instead of msgpack::type::variant_ref |
| 69 | + // or do as follows: |
| 70 | + boost::string_ref const& value = it->second.as_boost_string_ref(); |
| 71 | + it->second = std::string(&value.front(), value.size()) + ", JAPAN"; |
| 72 | + ++it; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + template <typename T> |
| 77 | + void operator()(T const&) const { |
| 78 | + std::cout << " match others" << std::endl; |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +int main() { |
| 83 | + std::stringstream ss; |
| 84 | + user u; |
| 85 | + u.name = "Takatoshi Kondo"; |
| 86 | + u.age = 42; |
| 87 | + u.address = "Tokyo"; |
| 88 | + msgpack::pack(ss, u); |
| 89 | + |
| 90 | + msgpack::unpacked unp = msgpack::unpack(ss.str().data(), ss.str().size()); |
| 91 | + msgpack::object const& obj = unp.get(); |
| 92 | + std::cout << "Unpacked msgpack object." << std::endl; |
| 93 | + std::cout << obj << std::endl; |
| 94 | + msgpack::type::variant_ref v = obj.as<msgpack::type::variant_ref>(); |
| 95 | + std::cout << "Applying proc..." << std::endl; |
| 96 | + boost::apply_visitor(proc(), v); |
| 97 | + msgpack::zone z; |
| 98 | + std::cout << "Applied msgpack object." << std::endl; |
| 99 | + std::cout << msgpack::object(v, z) << std::endl; |
| 100 | +} |
0 commit comments