Skip to content

Commit 81960fa

Browse files
author
pfeatherstone
committed
README + examples
1 parent aee3c4e commit 81960fa

File tree

4 files changed

+300
-3
lines changed

4 files changed

+300
-3
lines changed

README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,149 @@
11
# msgpack_cpp
22
C++ header-only msgpack library using kiss principle
3+
4+
# Example
5+
6+
## High-level objects
7+
8+
### `std::tuple`
9+
10+
```
11+
using namespace msgpackcpp;
12+
13+
std::tuple<int, float, std::string> a(1, 3.14, "Hello there!");
14+
15+
std::vector<char> buf;
16+
17+
// Creates a lambda which captures `buf` by reference and appends data to it when invoked
18+
auto out = sink(buf);
19+
20+
// The first argument can be any function object with signature void(const char* data, size_t len)
21+
serialize(out, a);
22+
23+
// Creates a mutable lambda which captures `buf` by reference and reads data from it when invoked
24+
auto in = source(buf);
25+
26+
// The first argument can be any function object with signature void(char* data, size_t len)
27+
deserialize(in, a);
28+
```
29+
30+
### `std::vector`
31+
32+
```
33+
using namespace msgpackcpp;
34+
35+
std::vector<int> v1(10);
36+
std::iota(begin(v1), end(v1), 0);
37+
std::vector<int> v2;
38+
39+
// You can also serialize into an ostream object
40+
std::ostringstream sout;
41+
auto out = sink(sout);
42+
serialize(out, v1);
43+
44+
// Deserialize from an istream object
45+
std::istringstream sin(sout.str());
46+
auto in = source(sin);
47+
deserialize(in, v2);
48+
```
49+
50+
### `std::map`
51+
52+
```
53+
using namespace msgpackcpp;
54+
55+
std::map<std::string, int> a = {{"a", 1}, {"b", 2}};
56+
std::map<std::string, int> b;
57+
58+
std::vector<char> buf;
59+
auto out = sink(buf);
60+
serialize(out, a);
61+
62+
auto in = source(buf);
63+
deserialize(in, b);
64+
```
65+
66+
### Custom object
67+
68+
Option 1 : defined `serialize()` and `deserialize()` functions in the same namespace as your custom struct. This will get picked up by ADL.
69+
70+
```
71+
namespace mynamespace
72+
{
73+
struct my_struct1
74+
{
75+
int my_int{};
76+
float my_float{};
77+
std::string my_string;
78+
std::vector<short> my_audio;
79+
};
80+
81+
template<class Stream>
82+
void serialize(Stream& out, const my_struct1& obj)
83+
{
84+
using msgpackcpp::serialize;
85+
serialize(out, std::tie(obj.my_int, obj.my_float, obj.my_string, obj.my_audio));
86+
}
87+
88+
template<class Source>
89+
void deserialize(Source& in, my_struct1& obj)
90+
{
91+
using msgpackcpp::deserialize;
92+
auto members = std::tie(obj.my_int, obj.my_float, obj.my_string, obj.my_audio);
93+
deserialize(in, members);
94+
}
95+
}
96+
97+
...
98+
99+
using namespace msgpackcpp;
100+
101+
mynamespace::my_struct1 a = {1, 3.14, "hello there", {0, 1, 2, 3, 4}};
102+
mynamespace::my_struct1 b;
103+
104+
std::vector<char> buf;
105+
auto out = sink(buf);
106+
serialize(out, a);
107+
108+
auto in = source(buf);
109+
deserialize(in, b);
110+
```
111+
112+
Option 2 : use Boost.Describe to describe your struct
113+
114+
```
115+
#include <boost/describe/class.hpp>
116+
#include <msgpack_describe.h>
117+
118+
namespace mynamespace2
119+
{
120+
struct my_struct2
121+
{
122+
int my_int{};
123+
float my_float{};
124+
std::string my_string;
125+
std::vector<short> my_audio;
126+
};
127+
128+
BOOST_DESCRIBE_STRUCT(my_struct2, (), (my_int, my_float, my_string, my_audio))
129+
}
130+
131+
...
132+
133+
using namespace msgpackcpp;
134+
135+
mynamespace2::my_struct2 a = {1, 3.14, "hello there", {0, 1, 2, 3, 4}};
136+
mynamespace2::my_struct2 b;
137+
138+
std::vector<char> buf;
139+
auto out = sink(buf);
140+
141+
// You can serialize your struct like a std::map,
142+
// where the member variable names are string keys.
143+
// Or you can serialize like a std::tuple where there are no keys.
144+
// The latter creates a smaller serialized buffer.
145+
serialize(out, a, /*as_map=*/true);
146+
147+
auto in = source(buf);
148+
deserialize(in, b, /*as_map=*/true);
149+
```

CMakeLists.txt renamed to test/CMakeLists.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@ FetchContent_MakeAvailable(Boost msgpack)
2424

2525
# Unit test
2626
add_executable(Test test.cpp)
27-
target_compile_features(Test PRIVATE cxx_std_20)
27+
target_compile_features(Test PRIVATE cxx_std_17)
2828
target_compile_options(Test PRIVATE -Wall -Wextra)
2929
target_include_directories(Test
30-
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
31-
PRIVATE ${doctest_SOURCE_DIR}/doctest)
30+
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include)
3231
target_link_libraries(Test
3332
PRIVATE Boost::describe Boost::unit_test_framework
3433
PRIVATE msgpack-cxx)
34+
35+
# Example
36+
add_executable(Example example.cpp)
37+
target_compile_features(Example PRIVATE cxx_std_17)
38+
target_compile_options(Example PRIVATE -Wall -Wextra)
39+
target_include_directories(Example PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include)
40+
target_link_libraries(Example PRIVATE Boost::describe)

test/example.cpp

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <numeric>
4+
#include <boost/describe/class.hpp>
5+
#include <msgpack.h>
6+
#include <msgpack_describe.h>
7+
#include <msgpack_sinks.h>
8+
9+
void example1_tuple()
10+
{
11+
using namespace msgpackcpp;
12+
13+
std::tuple<int, float, std::string> a(1, 3.14, "Hello there!");
14+
15+
std::vector<char> buf;
16+
17+
// Creates a lambda which captures `buf` by reference and appends data to it when invoked
18+
auto out = sink(buf);
19+
20+
// The first argument can be any function object with signature void(const char* data, size_t len)
21+
serialize(out, a);
22+
23+
// Creates a mutable lambda which captures `buf` by reference and reads data from it when invoked
24+
auto in = source(buf);
25+
26+
// The first argument can be any function object with signature void(char* data, size_t len)
27+
deserialize(in, a);
28+
}
29+
30+
void example2_vector()
31+
{
32+
using namespace msgpackcpp;
33+
34+
std::vector<int> v1(10);
35+
std::iota(begin(v1), end(v1), 0);
36+
std::vector<int> v2;
37+
38+
// You can also serialize into an ostream object
39+
std::ostringstream sout;
40+
auto out = sink(sout);
41+
serialize(out, v1);
42+
43+
// Deserialize from an istream object
44+
std::istringstream sin(sout.str());
45+
auto in = source(sin);
46+
deserialize(in, v2);
47+
}
48+
49+
void example3_map()
50+
{
51+
using namespace msgpackcpp;
52+
53+
std::map<std::string, int> a = {{"a", 1}, {"b", 2}};
54+
std::map<std::string, int> b;
55+
56+
std::vector<char> buf;
57+
auto out = sink(buf);
58+
serialize(out, a);
59+
60+
auto in = source(buf);
61+
deserialize(in, b);
62+
}
63+
64+
namespace mynamespace
65+
{
66+
struct my_struct1
67+
{
68+
int my_int{};
69+
float my_float{};
70+
std::string my_string;
71+
std::vector<short> my_audio;
72+
};
73+
74+
template<class Stream>
75+
void serialize(Stream& out, const my_struct1& obj)
76+
{
77+
using msgpackcpp::serialize;
78+
serialize(out, std::tie(obj.my_int, obj.my_float, obj.my_string, obj.my_audio));
79+
}
80+
81+
template<class Source>
82+
void deserialize(Source& in, my_struct1& obj)
83+
{
84+
using msgpackcpp::deserialize;
85+
auto members = std::tie(obj.my_int, obj.my_float, obj.my_string, obj.my_audio);
86+
deserialize(in, members);
87+
}
88+
}
89+
90+
void example4_struct()
91+
{
92+
using namespace msgpackcpp;
93+
94+
mynamespace::my_struct1 a = {1, 3.14, "hello there", {0, 1, 2, 3, 4}};
95+
mynamespace::my_struct1 b;
96+
97+
std::vector<char> buf;
98+
auto out = sink(buf);
99+
serialize(out, a);
100+
101+
auto in = source(buf);
102+
deserialize(in, b);
103+
104+
std::cout << b.my_int << ' ' << b.my_float << ' ' << b.my_string << ' ' << b.my_audio.size() << '\n';
105+
}
106+
107+
namespace mynamespace2
108+
{
109+
struct my_struct2
110+
{
111+
int my_int{};
112+
float my_float{};
113+
std::string my_string;
114+
std::vector<short> my_audio;
115+
};
116+
117+
BOOST_DESCRIBE_STRUCT(my_struct2, (), (my_int, my_float, my_string, my_audio))
118+
}
119+
120+
void example5_struct()
121+
{
122+
using namespace msgpackcpp;
123+
124+
mynamespace2::my_struct2 a = {1, 3.14, "hello there", {0, 1, 2, 3, 4}};
125+
mynamespace2::my_struct2 b;
126+
127+
std::vector<char> buf;
128+
auto out = sink(buf);
129+
serialize(out, a, /*as_map=*/true);
130+
131+
auto in = source(buf);
132+
deserialize(in, b, /*as_map=*/true);
133+
134+
std::cout << b.my_int << ' ' << b.my_float << ' ' << b.my_string << ' ' << b.my_audio.size() << '\n';
135+
}
136+
137+
int main()
138+
{
139+
example1_tuple();
140+
example2_vector();
141+
example3_map();
142+
example4_struct();
143+
example5_struct();
144+
}
File renamed without changes.

0 commit comments

Comments
 (0)