Skip to content

Commit d3450c1

Browse files
committed
Merge pull request #311 from redboltz/add_map_based_example
Added map based packing and converting example.
2 parents e25ecc5 + fa7f840 commit d3450c1

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 <iostream>
20+
#include <iomanip>
21+
#include <sstream>
22+
#include <cassert>
23+
24+
#define MSGPACK_USE_BOOST
25+
#include <msgpack.hpp>
26+
27+
struct base1 {
28+
base1():a("default") {}
29+
std::string a;
30+
MSGPACK_DEFINE_MAP(a);
31+
};
32+
33+
struct v1 : base1 {
34+
v1():name("default"), age(0) {}
35+
std::string name;
36+
int age;
37+
MSGPACK_DEFINE_MAP(MSGPACK_BASE_MAP(base1), name, age);
38+
};
39+
40+
struct base2 {
41+
base2():a("default") {}
42+
std::string a;
43+
MSGPACK_DEFINE_MAP(a);
44+
};
45+
46+
// Removed: base1, name
47+
// Added : base2, address
48+
struct v2 : base2 {
49+
v2(): age(0), address("default") {}
50+
int age;
51+
std::string address;
52+
MSGPACK_DEFINE_MAP(MSGPACK_BASE_MAP(base2), age, address);
53+
};
54+
55+
// The member variable "age" is in common between v1 and v2.
56+
57+
void print(std::string const& buf) {
58+
for (std::string::const_iterator it = buf.begin(), end = buf.end();
59+
it != end;
60+
++it) {
61+
std::cout
62+
<< std::setw(2)
63+
<< std::hex
64+
<< std::setfill('0')
65+
<< (static_cast<int>(*it) & 0xff)
66+
<< ' ';
67+
}
68+
std::cout << std::dec << std::endl;
69+
}
70+
71+
int main() {
72+
{ // pack v1, unpack, convert to v2
73+
v1 v;
74+
v.a = "ABC";
75+
v.name = "John Smith";
76+
v.age = 35;
77+
78+
std::stringstream ss;
79+
msgpack::pack(ss, v);
80+
81+
print(ss.str());
82+
83+
msgpack::unpacked unp = msgpack::unpack(ss.str().data(), ss.str().size());
84+
85+
msgpack::object obj = unp.get();
86+
std::cout << obj << std::endl;
87+
88+
v2 newv = obj.as<v2>();
89+
90+
std::cout << "v2::a " << newv.a << std::endl;
91+
std::cout << "v2::age " << newv.age << std::endl;
92+
std::cout << "v2::address " << newv.address << std::endl;
93+
94+
// "age" is set from v1
95+
assert(newv.a == "default");
96+
assert(newv.age == 35);
97+
assert(newv.address == "default");
98+
}
99+
{ // create v2 object with zone, convert to v1
100+
v2 v;
101+
v.a = "DEF";
102+
v.age = 42;
103+
v.address = "Tokyo";
104+
105+
msgpack::zone z;
106+
msgpack::object obj(v, z);
107+
std::cout << obj << std::endl;
108+
109+
v1 newv = obj.as<v1>();
110+
111+
std::cout << "v1::a " << newv.a << std::endl;
112+
std::cout << "v1::name " << newv.name << std::endl;
113+
std::cout << "v1::age " << newv.age << std::endl;
114+
115+
// "age" is set from v2
116+
assert(newv.a == "default");
117+
assert(newv.name == "default");
118+
assert(newv.age == 42);
119+
}
120+
}

0 commit comments

Comments
 (0)