Skip to content

Commit c704d4b

Browse files
committed
Added new example.
1 parent 4466bda commit c704d4b

File tree

8 files changed

+535
-52
lines changed

8 files changed

+535
-52
lines changed

CMakeLists.txt

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,25 @@ IF (MSGPACK_32BIT)
8888
ENDIF ()
8989
ENDIF ()
9090

91+
OPTION (MSGPACK_BUILD_EXAMPLES "Build msgpack examples." ON)
92+
9193
IF (MSGPACK_BOOST)
9294
SET (CMAKE_CXX_FLAGS "-DMSGPACK_USE_BOOST ${CMAKE_CXX_FLAGS}")
93-
SET (Boost_USE_STATIC_LIBS ON) # only find static libs
94-
SET (Boost_USE_MULTITHREADED ON)
95-
SET (Boost_USE_STATIC_RUNTIME OFF)
96-
FIND_PACKAGE (Boost COMPONENTS chrono timer system)
97-
IF (Boost_INCLUDE_DIRS)
98-
INCLUDE_DIRECTORIES (
99-
${Boost_INCLUDE_DIRS}
100-
)
101-
ENDIF ()
102-
INCLUDE_DIRECTORIES (
103-
${MSGPACK_BOOST_DIR}
104-
)
95+
ENDIF ()
96+
97+
SET (Boost_USE_STATIC_LIBS ON) # only find static libs
98+
SET (Boost_USE_MULTITHREADED ON)
99+
SET (Boost_USE_STATIC_RUNTIME OFF)
100+
FIND_PACKAGE (Boost COMPONENTS chrono context timer system)
101+
IF (Boost_INCLUDE_DIRS)
102+
INCLUDE_DIRECTORIES (
103+
${Boost_INCLUDE_DIRS}
104+
)
105+
ENDIF ()
106+
IF (MSGPACK_BOOST_DIR)
107+
INCLUDE_DIRECTORIES (
108+
${MSGPACK_BOOST_DIR}
109+
)
105110
ENDIF ()
106111

107112
IF (MSGPACK_CHAR_SIGN)
@@ -141,7 +146,6 @@ FIND_PACKAGE (Threads)
141146
IF (GTEST_FOUND AND ZLIB_FOUND AND THREADS_FOUND)
142147
OPTION (MSGPACK_BUILD_TESTS "Build msgpack tests." ON)
143148
ENDIF ()
144-
OPTION (MSGPACK_BUILD_EXAMPLES "Build msgpack examples." ON)
145149

146150
OPTION (MSGPACK_ENABLE_CXX "Enable C++ interface." ON)
147151
OPTION (MSGPACK_ENABLE_SHARED "Build shared libaries in addition to static libraries." ON)

example/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ ADD_SUBDIRECTORY (c)
22
ADD_SUBDIRECTORY (cpp03)
33
ADD_SUBDIRECTORY (cpp11)
44
ADD_SUBDIRECTORY (boost)
5+
ADD_SUBDIRECTORY (x3)

example/cpp03/CMakeLists.txt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ IF (NOT MSVC)
2222
)
2323
ENDIF ()
2424

25-
IF (MSGPACK_BOOST)
26-
IF (NOT MSVC)
27-
LIST (APPEND with_boost_lib_PROGRAMS
28-
speed_test.cpp
29-
speed_test_nested_array.cpp
30-
)
31-
ENDIF ()
25+
IF (Boost_TIMER_LIBRARY AND Boost_CHRONO_LIBRARY AND Boost_SYSTEM_LIBRARY)
26+
LIST (APPEND with_boost_lib_PROGRAMS
27+
speed_test.cpp
28+
speed_test_nested_array.cpp
29+
)
3230
ENDIF ()
3331

3432
FOREACH (source_file ${exec_PROGRAMS})
@@ -56,10 +54,10 @@ FOREACH (source_file ${with_pthread_PROGRAMS})
5654
${source_file}
5755
)
5856
TARGET_LINK_LIBRARIES (${source_file_we}
59-
pthread
57+
${CMAKE_THREAD_LIBS_INIT}
6058
)
6159
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
62-
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wall -Wextra -Werror -g -O3 -pthread")
60+
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wall -Wextra -Werror -g -O3")
6361
ENDIF ()
6462
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
6563
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")

example/x3/CMakeLists.txt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
IF (MSGPACK_USE_X3_PARSE)
2+
INCLUDE_DIRECTORIES (
3+
../include
4+
)
5+
6+
LIST (APPEND exec_PROGRAMS
7+
unpack.cpp
8+
parse.cpp
9+
)
10+
IF (Boost_CONTEXT_LIBRARY AND Boost_SYSTEM_LIBRARY AND CMAKE_THREAD_LIBS_INIT)
11+
LIST (APPEND with_boost_PROGRAMS
12+
stream_unpack.cpp
13+
)
14+
ENDIF ()
15+
FOREACH (source_file ${exec_PROGRAMS})
16+
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
17+
ADD_EXECUTABLE (
18+
${source_file_we}
19+
${source_file}
20+
)
21+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
22+
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wall -Wextra -Werror -g ")
23+
ENDIF ()
24+
25+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
26+
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wno-mismatched-tags")
27+
ENDIF ()
28+
29+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
30+
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
31+
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
32+
ELSE ()
33+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
34+
ENDIF ()
35+
ENDIF ()
36+
ENDFOREACH ()
37+
FOREACH (source_file ${with_boost_PROGRAMS})
38+
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
39+
ADD_EXECUTABLE (
40+
${source_file_we}
41+
${source_file}
42+
)
43+
TARGET_LINK_LIBRARIES (${source_file_we}
44+
${Boost_CONTEXT_LIBRARY}
45+
${Boost_SYSTEM_LIBRARY}
46+
${CMAKE_THREAD_LIBS_INIT}
47+
)
48+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
49+
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wall -Wextra -Werror -g ")
50+
ENDIF ()
51+
52+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
53+
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wno-mismatched-tags")
54+
ENDIF ()
55+
56+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
57+
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
58+
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
59+
ELSE ()
60+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
61+
ENDIF ()
62+
ENDIF ()
63+
ENDFOREACH ()
64+
ENDIF ()

example/x3/parse.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// MessagePack for C++ example
2+
//
3+
// Copyright (C) 2017 KONDO Takatoshi
4+
//
5+
// Distributed under the Boost Software License, Version 1.0.
6+
// (See accompanying file LICENSE_1_0.txt or copy at
7+
// http://www.boost.org/LICENSE_1_0.txt)
8+
//
9+
10+
#include <iostream>
11+
#include <sstream>
12+
#include <cassert>
13+
14+
// MSGPACK_USE_X3_PARSE should be defined before including msgpack.hpp
15+
// It usually defined as a compiler option as -DMSGPACK_USE_X3_PARSE.
16+
17+
#include <msgpack.hpp>
18+
19+
struct json_like_visitor : msgpack::v2::null_visitor {
20+
json_like_visitor(std::string& s):m_s(s) {}
21+
22+
bool visit_nil() {
23+
m_s += "null";
24+
return true;
25+
}
26+
bool visit_boolean(bool v) {
27+
if (v) m_s += "true";
28+
else m_s += "false";
29+
return true;
30+
}
31+
bool visit_positive_integer(uint64_t v) {
32+
std::stringstream ss;
33+
ss << v;
34+
m_s += ss.str();
35+
return true;
36+
}
37+
bool visit_negative_integer(int64_t v) {
38+
std::stringstream ss;
39+
ss << v;
40+
m_s += ss.str();
41+
return true;
42+
}
43+
bool visit_float(double v) {
44+
std::stringstream ss;
45+
ss << v;
46+
m_s += ss.str();
47+
return true;
48+
}
49+
bool visit_str(const char* v, uint32_t size) {
50+
m_s += '"' + std::string(v, size) + '"';
51+
return true;
52+
}
53+
bool start_array(uint32_t /*num_elements*/) {
54+
m_s += "[";
55+
return true;
56+
}
57+
bool end_array_item() {
58+
m_s += ",";
59+
return true;
60+
}
61+
bool end_array() {
62+
m_s.erase(m_s.size() - 1, 1); // remove the last ','
63+
m_s += "]";
64+
return true;
65+
}
66+
bool start_map(uint32_t /*num_kv_pairs*/) {
67+
m_s += "{";
68+
return true;
69+
}
70+
bool end_map_key() {
71+
m_s += ":";
72+
return true;
73+
}
74+
bool end_map_value() {
75+
m_s += ",";
76+
return true;
77+
}
78+
bool end_map() {
79+
m_s.erase(m_s.size() - 1, 1); // remove the last ','
80+
m_s += "}";
81+
return true;
82+
}
83+
void parse_error(size_t /*parsed_offset*/, size_t /*error_offset*/) {
84+
}
85+
void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) {
86+
}
87+
std::string& m_s;
88+
};
89+
90+
int main() {
91+
std::stringstream ss;
92+
std::map<std::string, std::vector<int>> v1 {
93+
{ "ABC", { 1, 2, 3 } },
94+
{ "DEFG", { 4, 5 } }
95+
};
96+
std::vector<std::string> v2 {
97+
"HIJ", "KLM", "NOP"
98+
};
99+
msgpack::pack(ss, v1);
100+
msgpack::pack(ss, v2);
101+
102+
std::string const& buf = ss.str();
103+
auto it = buf.begin();
104+
auto end = buf.end();
105+
{
106+
std::string str;
107+
bool ret = msgpack::parse(it, end, json_like_visitor(str));
108+
// it is updated here.
109+
assert(ret);
110+
std::cout << str << std::endl;
111+
}
112+
{
113+
std::string str;
114+
bool ret = msgpack::parse(it, end, json_like_visitor(str));
115+
// it is updated here.
116+
assert(ret);
117+
std::cout << str << std::endl;
118+
}
119+
}

0 commit comments

Comments
 (0)