Skip to content

Commit 46bb14d

Browse files
committed
msgpack.cpp
1 parent 633b7dc commit 46bb14d

1 file changed

Lines changed: 51 additions & 4 deletions

File tree

section4/msgpack.cpp

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,68 @@
33
// sudo apt-get install libmsgpack-dev
44
//
55
// /usr/include/msgpack
6-
// g++ msgpack.cpp -std=c++11 -lmsgpackc -o a.out;./a.out
7-
// g++ msgpack.cpp -std=c++14 -lmsgpackc -o a.out;./a.out
6+
// g++ msgpack.cpp -std=c++11 -o a.out;./a.out
7+
// g++ msgpack.cpp -std=c++14 -o a.out;./a.out
88

99
#include <iostream>
10+
#include <string>
11+
#include <vector>
12+
#include <algorithm>
1013

1114
#include <msgpack.hpp>
1215

16+
using namespace std;
17+
1318
void case1()
1419
{
15-
//msgpack::type::tuple<>
20+
vector<int> v = {1,2,3,4,5};
21+
22+
msgpack::sbuffer sbuf;
23+
msgpack::pack(sbuf, v);
24+
25+
cout << sbuf.size() << endl;
26+
27+
auto handle = msgpack::unpack(sbuf.data(), sbuf.size());
28+
auto obj = handle.get();
29+
cout << obj << endl;
30+
31+
decltype(v) v2;
32+
obj.convert(v2);
33+
34+
assert(std::equal(begin(v), end(v), begin(v2)));
35+
}
36+
37+
class Book final
38+
{
39+
public:
40+
int id;
41+
string title;
42+
set<string> tags;
43+
public:
44+
MSGPACK_DEFINE(id, title, tags);
45+
};
46+
47+
void case2()
48+
{
49+
Book book1 = {1, "1984", {"a","b"}};
50+
51+
msgpack::sbuffer sbuf;
52+
msgpack::pack(sbuf, book1);
53+
54+
auto obj = msgpack::unpack(sbuf.data(), sbuf.size()).get();
55+
56+
Book book2;
57+
obj.convert(book2);
58+
59+
assert(book2.id == book1.id);
60+
assert(book2.tags.size() == 2);
61+
cout << book2.title << endl;
1662
}
1763

1864
int main()
1965
{
20-
using namespace std;
66+
case1();
67+
case2();
2168

2269
cout << "msgpack demo" << endl;
2370
}

0 commit comments

Comments
 (0)