Skip to content

Commit b09d74a

Browse files
author
me
committed
adding examples
1 parent 01ccb80 commit b09d74a

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

examples/CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
project(MsgPackExamples)
3+
4+
# Helpers
5+
set(IS_MSVC "$<CXX_COMPILER_ID:MSVC>")
6+
set(IS_NOT_MSVC "$<NOT:${IS_MSVC}>")
7+
8+
# Boost
9+
include(FetchContent)
10+
set(BOOST_INCLUDE_LIBRARIES describe)
11+
set(BOOST_ENABLE_CMAKE ON)
12+
FetchContent_Declare(
13+
Boost
14+
URL "https://github.com/boostorg/boost/releases/download/boost-1.89.0/boost-1.89.0-cmake.tar.xz"
15+
URL_HASH MD5=537da0e22b31b8b7185cc44fdde70458
16+
DOWNLOAD_EXTRACT_TIMESTAMP true
17+
)
18+
FetchContent_MakeAvailable(Boost)
19+
20+
# Examples
21+
function(add_example target_name)
22+
add_executable(${target_name} ${ARGN})
23+
target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include)
24+
target_compile_options(${target_name} PRIVATE $<${IS_NOT_MSVC}:-Wall -Wextra -Werror>)
25+
target_link_options(${target_name} PRIVATE $<$<AND:$<CONFIG:RELEASE>,${IS_NOT_MSVC}>:-s>)
26+
target_link_libraries(${target_name} PRIVATE Boost::describe)
27+
endfunction()
28+
29+
add_example(example1 example1.cpp)
30+
add_example(example2 example2.cpp)
31+
add_example(example3 example3.cpp)

examples/example1.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "msgpack.h"
2+
3+
int main()
4+
{
5+
6+
}

examples/example2.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <cstdint>
2+
#include "msgpack.h"
3+
#include "msgpack_sinks.h"
4+
5+
namespace mynamespace
6+
{
7+
struct my_struct
8+
{
9+
int my_int{};
10+
float my_float{};
11+
std::string my_string;
12+
std::vector<short> my_audio;
13+
};
14+
15+
template<SINK_TYPE Sink>
16+
void serialize(Sink& out, const my_struct& obj)
17+
{
18+
using msgpackcpp::serialize;
19+
serialize(out, std::tie(obj.my_int, obj.my_float, obj.my_string, obj.my_audio));
20+
}
21+
22+
template<SOURCE_TYPE Source>
23+
void deserialize(Source& in, my_struct& obj)
24+
{
25+
using msgpackcpp::deserialize;
26+
auto members = std::tie(obj.my_int, obj.my_float, obj.my_string, obj.my_audio);
27+
deserialize(in, members);
28+
}
29+
}
30+
31+
int main()
32+
{
33+
using msgpackcpp::serialize;
34+
using msgpackcpp::deserialize;
35+
using msgpackcpp::sink;
36+
using msgpackcpp::source;
37+
38+
mynamespace::my_struct a = {1, 3.14, "hello there", {0, 1, 2, 3, 4}};
39+
mynamespace::my_struct b;
40+
41+
std::vector<char> buf;
42+
auto out = sink(buf);
43+
serialize(out, a);
44+
auto in = source(buf);
45+
deserialize(in, b);
46+
47+
printf("%d %f %s [", b.my_int, b.my_float, b.my_string.c_str());
48+
for (auto s : b.my_audio) printf("%hd ", s);
49+
printf("]\n");
50+
}

examples/example3.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <cstdint>
2+
#include <boost/describe/class.hpp>
3+
#include "msgpack.h"
4+
#include "msgpack_sinks.h"
5+
#include "msgpack_describe.h"
6+
7+
namespace mynamespace
8+
{
9+
struct my_struct
10+
{
11+
int my_int{};
12+
float my_float{};
13+
std::string my_string;
14+
std::vector<short> my_audio;
15+
};
16+
17+
BOOST_DESCRIBE_STRUCT(my_struct, (), (my_int, my_float, my_string, my_audio))
18+
}
19+
20+
int main()
21+
{
22+
using msgpackcpp::serialize;
23+
using msgpackcpp::deserialize;
24+
using msgpackcpp::sink;
25+
using msgpackcpp::source;
26+
27+
mynamespace::my_struct a = {1, 3.14, "hello there", {0, 1, 2, 3, 4}};
28+
mynamespace::my_struct b;
29+
30+
std::vector<char> buf;
31+
auto out = sink(buf);
32+
serialize(out, a);
33+
auto in = source(buf);
34+
deserialize(in, b);
35+
36+
printf("%d %f %s [", b.my_int, b.my_float, b.my_string.c_str());
37+
for (auto s : b.my_audio) printf("%hd ", s);
38+
printf("]\n");
39+
}

0 commit comments

Comments
 (0)