Skip to content

Commit 86a7d78

Browse files
author
me
committed
updated readme and examples
1 parent dc2c93d commit 86a7d78

File tree

4 files changed

+142
-104
lines changed

4 files changed

+142
-104
lines changed

README.md

Lines changed: 94 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -5,73 +5,56 @@
55
# msgpack_cpp
66
C++ header-only msgpack library
77

8-
## Example
8+
## Examples
99

10-
### High-level objects
10+
### Basic types
1111

12-
#### `std::tuple`
13-
14-
```
15-
using namespace msgpackcpp;
16-
17-
std::tuple<int, float, std::string> a(1, 3.14, "Hello there!");
18-
19-
std::vector<char> buf;
20-
21-
// Creates a lambda which captures `buf` by reference and appends data to it when invoked
22-
auto out = sink(buf);
23-
24-
// The first argument can be any function object with signature void(const char* data, size_t len) (when C++20 is used, it satisfies the sink_type concept)
25-
serialize(out, a);
26-
27-
// Creates a mutable lambda which captures `buf` by reference and reads data from it when invoked
28-
auto in = source(buf);
29-
30-
// The first argument can be any function object with signature void(char* data, size_t len) (when C++20 is used, it satisfies the source_type concept)
31-
deserialize(in, a);
32-
```
33-
34-
#### `std::vector`
35-
36-
```
37-
using namespace msgpackcpp;
38-
39-
std::vector<int> v1(10);
40-
std::iota(begin(v1), end(v1), 0);
41-
std::vector<int> v2;
42-
43-
// You can also serialize into an ostream object
44-
std::ostringstream sout;
45-
auto out = sink(sout);
46-
serialize(out, v1);
47-
48-
// Deserialize from an istream object
49-
std::istringstream sin(sout.str());
50-
auto in = source(sin);
51-
deserialize(in, v2);
52-
```
53-
54-
#### `std::map`
12+
```cpp
13+
#include "msgpack.h"
14+
#include "msgpack_sinks.h"
5515

16+
int main()
17+
{
18+
using msgpackcpp::serialize;
19+
using msgpackcpp::deserialize;
20+
using msgpackcpp::sink;
21+
using msgpackcpp::source;
22+
23+
// Some data
24+
int a = 1, aa;
25+
double b = 3.15, bb;
26+
std::string c = "hello there", cc;
27+
std::vector<char> d = {0,1,2,3,4,5,6,7,8,9}, dd;
28+
std::map<std::string, int> e = {{"a", 1}, {"b", 2}}, ee;
29+
30+
31+
// Serialize
32+
std::vector<char> buf;
33+
auto out = sink(buf);
34+
serialize(out, a);
35+
serialize(out, b);
36+
serialize(out, c);
37+
serialize(out, d);
38+
serialize(out, e);
39+
40+
// Deserialize
41+
auto in = source(buf);
42+
deserialize(in, aa);
43+
deserialize(in, bb);
44+
deserialize(in, cc);
45+
deserialize(in, dd);
46+
deserialize(in, ee);
47+
}
5648
```
57-
using namespace msgpackcpp;
5849

59-
std::map<std::string, int> a = {{"a", 1}, {"b", 2}};
60-
std::map<std::string, int> b;
50+
### Custom types
6151

62-
std::vector<char> buf;
63-
auto out = sink(buf);
64-
serialize(out, a);
52+
Option 1 : define `serialize()` and `deserialize()` functions in the same namespace as your custom struct
6553

66-
auto in = source(buf);
67-
deserialize(in, b);
68-
```
54+
```cpp
55+
#include "msgpack.h"
56+
#include "msgpack_sinks.h"
6957

70-
#### Custom object
71-
72-
Option 1 : define `serialize()` and `deserialize()` functions in the same namespace as your custom struct. This will get picked up by ADL.
73-
74-
```
7558
namespace mynamespace
7659
{
7760
struct my_struct1
@@ -83,73 +66,81 @@ namespace mynamespace
8366
};
8467

8568
template<SINK_TYPE Sink>
86-
void serialize(Sink& out, const my_struct1& obj)
69+
void serialize(Sink& out, const my_struct& obj)
8770
{
88-
using msgpackcpp::serialize;
89-
serialize(out, std::tie(obj.my_int, obj.my_float, obj.my_string, obj.my_audio));
71+
auto packed = std::tie(obj.my_int, obj.my_float, obj.my_string, obj.my_audio);
72+
serialize(out, packed);
9073
}
9174

9275
template<SOURCE_TYPE Source>
93-
void deserialize(Source& in, my_struct1& obj)
76+
void deserialize(Source& in, my_struct& obj)
9477
{
95-
using msgpackcpp::deserialize;
96-
auto members = std::tie(obj.my_int, obj.my_float, obj.my_string, obj.my_audio);
97-
deserialize(in, members);
98-
}
78+
auto packed = std::tie(obj.my_int, obj.my_float, obj.my_string, obj.my_audio);
79+
deserialize(in, packed);
80+
}
9981
}
10082

101-
...
102-
103-
using namespace msgpackcpp;
104-
105-
mynamespace::my_struct1 a = {1, 3.14, "hello there", {0, 1, 2, 3, 4}};
106-
mynamespace::my_struct1 b;
107-
108-
std::vector<char> buf;
109-
auto out = sink(buf);
110-
serialize(out, a);
111-
112-
auto in = source(buf);
113-
deserialize(in, b);
83+
int main()
84+
{
85+
using msgpackcpp::serialize;
86+
using msgpackcpp::deserialize;
87+
using msgpackcpp::sink;
88+
using msgpackcpp::source;
89+
90+
mynamespace::my_struct a = {1, 3.14, "hello there", {0, 1, 2, 3, 4}};
91+
mynamespace::my_struct b;
92+
93+
// Serialize
94+
std::stringstream buf;
95+
auto out = sink(buf);
96+
serialize(out, a);
97+
98+
// Deserialize
99+
auto in = source(buf);
100+
deserialize(in, b);
101+
}
114102
```
115103
116104
Option 2 : use Boost.Describe to describe your struct
117105
118-
```
106+
```cpp
119107
#include <boost/describe/class.hpp>
120-
#include <msgpack_describe.h>
108+
#include "msgpack.h"
109+
#include "msgpack_sinks.h"
110+
#include "msgpack_describe.h"
121111
122-
namespace mynamespace2
112+
namespace mynamespace
123113
{
124-
struct my_struct2
114+
struct my_struct1
125115
{
126116
int my_int{};
127117
float my_float{};
128118
std::string my_string;
129119
std::vector<short> my_audio;
130120
};
131121
132-
BOOST_DESCRIBE_STRUCT(my_struct2, (), (my_int, my_float, my_string, my_audio))
122+
BOOST_DESCRIBE_STRUCT(my_struct, (), (my_int, my_float, my_string, my_audio))
133123
}
134124
135-
...
136-
137-
using namespace msgpackcpp;
138-
139-
mynamespace2::my_struct2 a = {1, 3.14, "hello there", {0, 1, 2, 3, 4}};
140-
mynamespace2::my_struct2 b;
141-
142-
std::vector<char> buf;
143-
auto out = sink(buf);
144-
145-
// You can serialize your struct like a std::map,
146-
// where the member variable names are string keys.
147-
// Or you can serialize like a std::tuple where there are no keys.
148-
// The latter creates a smaller serialized buffer.
149-
serialize(out, a, /*as_map=*/true);
150-
151-
auto in = source(buf);
152-
deserialize(in, b, /*as_map=*/true);
125+
int main()
126+
{
127+
using msgpackcpp::serialize;
128+
using msgpackcpp::deserialize;
129+
using msgpackcpp::sink;
130+
using msgpackcpp::source;
131+
132+
mynamespace::my_struct a = {1, 3.14, "hello there", {0, 1, 2, 3, 4}};
133+
mynamespace::my_struct b;
134+
135+
// Serialize
136+
std::stringstream buf;
137+
auto out = sink(buf);
138+
serialize(out, a);
139+
140+
// Deserialize
141+
auto in = source(buf);
142+
deserialize(in, b);
143+
}
153144
```
154145

155146
## Installation

examples/example1.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,47 @@
1+
#include <cstdio>
2+
#include <sstream>
3+
#include <map>
14
#include "msgpack.h"
5+
#include "msgpack_sinks.h"
26

37
int main()
48
{
5-
9+
using msgpackcpp::serialize;
10+
using msgpackcpp::deserialize;
11+
using msgpackcpp::sink;
12+
using msgpackcpp::source;
13+
14+
// Some data
15+
int a = 1, aa;
16+
double b = 3.15, bb;
17+
std::string c = "hello there", cc;
18+
std::vector<char> d = {0,1,2,3,4,5,6,7,8,9}, dd;
19+
std::map<std::string, int> e = {{"a", 1}, {"b", 2}}, ee;
20+
21+
const auto run = [&](auto& buf)
22+
{
23+
// Serialize
24+
auto out = sink(buf);
25+
serialize(out, a);
26+
serialize(out, b);
27+
serialize(out, c);
28+
serialize(out, d);
29+
serialize(out, e);
30+
31+
// Deserialize
32+
auto in = source(buf);
33+
deserialize(in, aa);
34+
deserialize(in, bb);
35+
deserialize(in, cc);
36+
deserialize(in, dd);
37+
deserialize(in, ee);
38+
};
39+
40+
// (De)serialize to vector
41+
std::vector<char> buf0;
42+
run(buf0);
43+
44+
// (De)serialize to istream/ostream
45+
std::stringstream buf1;
46+
run(buf1);
647
}

examples/example2.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ int main()
3737
mynamespace::my_struct a = {1, 3.14, "hello there", {0, 1, 2, 3, 4}};
3838
mynamespace::my_struct b;
3939

40+
// Serialize
4041
std::vector<char> buf;
4142
auto out = sink(buf);
4243
serialize(out, a);
44+
45+
// Deserialize
4346
auto in = source(buf);
4447
deserialize(in, b);
4548

examples/example3.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ int main()
2727
mynamespace::my_struct a = {1, 3.14, "hello there", {0, 1, 2, 3, 4}};
2828
mynamespace::my_struct b;
2929

30+
// Serialize
3031
std::vector<char> buf;
3132
auto out = sink(buf);
3233
serialize(out, a);
34+
35+
// Deserialize
3336
auto in = source(buf);
3437
deserialize(in, b);
3538

0 commit comments

Comments
 (0)