Skip to content

Commit a1fc786

Browse files
committed
Merge pull request #341 from redboltz/refine_examples
Refine examples
2 parents 4501551 + 12b5a62 commit a1fc786

20 files changed

+302
-33
lines changed

.travis.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ os:
1212
- linux
1313
before_install:
1414
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test; fi
15-
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo add-apt-repository -y ppa:h-rayflood/llvm; fi
15+
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo add-apt-repository -y ppa:h-rayflood/llvm-upper; fi
1616
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get update -qq; fi
1717
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get update; fi
1818
install:
1919
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -qq gcc-4.8-multilib g++-4.8-multilib; fi
20-
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install --allow-unauthenticated -qq clang-3.5; fi
20+
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install --allow-unauthenticated -qq clang-3.6; fi
2121
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 90; fi
2222
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 90; fi
2323
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -y lib32gcc1; fi
@@ -27,10 +27,13 @@ install:
2727
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -y bzip2; fi
2828
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -y libc6-dbg; fi
2929
- wget https://googletest.googlecode.com/files/gtest-1.7.0.zip
30-
- wget http://sourceforge.net/projects/boost/files/boost/1.58.0/
3130
- wget http://valgrind.org/downloads/valgrind-3.10.1.tar.bz2 && tar xjf valgrind-3.10.1.tar.bz2 && cd valgrind-3.10.1 && ./configure && make && sudo make install && cd ..
32-
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then unzip -q gtest-1.7.0.zip && cd gtest-1.7.0 && sudo cp -r include/gtest /usr/local/include && g++ src/gtest-all.cc -I. -Iinclude -c && g++ src/gtest_main.cc -I. -Iinclude -c && ar -rv libgtest.a gtest-all.o && ar -rv libgtest_main.a gtest_main.o && sudo mv *.a /usr/local/lib && g++ -m32 src/gtest-all.cc -I. -Iinclude -c && g++ -m32 src/gtest_main.cc -I. -Iinclude -c && ar -rv libgtest.a gtest-all.o && ar -rv libgtest_main.a gtest_main.o && sudo mkdir /usr/local/lib32 && sudo mv *.a /usr/local/lib32 && cd .. && wget http://sourceforge.net/projects/boost/files/boost/1.58.0/boost_1_58_0.zip && unzip -q boost_1_58_0.zip && sudo mkdir /usr/local/boost && sudo cp -r boost_1_58_0/boost /usr/local/boost/; fi
33-
- if [ "$TRAVIS_OS_NAME" == "osx" ]; then unzip -q gtest-1.7.0.zip && cd gtest-1.7.0 && sudo cp -r include/gtest /usr/local/include && clang++ src/gtest-all.cc -I. -Iinclude -c && g++ src/gtest_main.cc -I. -Iinclude -c && ar -rv libgtest.a gtest-all.o && ar -rv libgtest_main.a gtest_main.o && sudo mv *.a /usr/local/lib && cd .. && wget http://sourceforge.net/projects/boost/files/boost/1.58.0/boost_1_58_0.zip && unzip -q boost_1_58_0.zip && sudo mkdir /usr/local/boost && sudo cp -r boost_1_58_0/boost /usr/local/boost/; fi
31+
32+
- if [ "$TRAVIS_OS_NAME" == "osx" ]; then brew unlink boost; fi
33+
- if [ "$BOOST" == "boost" ]; then wget http://sourceforge.net/projects/boost/files/boost/1.58.0/boost_1_58_0.zip && unzip -q boost_1_58_0.zip && cd boost_1_58_0 && ./bootstrap.sh && ./b2 --with-timer --with-chrono address-model="$ARCH" > /dev/null && sudo ./b2 --with-timer --with-chrono address-model="$ARCH" install > /dev/null && cd ..; fi
34+
35+
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then unzip -q gtest-1.7.0.zip && cd gtest-1.7.0 && sudo cp -r include/gtest /usr/local/include && g++ src/gtest-all.cc -I. -Iinclude -c && g++ src/gtest_main.cc -I. -Iinclude -c && ar -rv libgtest.a gtest-all.o && ar -rv libgtest_main.a gtest_main.o && sudo mv *.a /usr/local/lib && g++ -m32 src/gtest-all.cc -I. -Iinclude -c && g++ -m32 src/gtest_main.cc -I. -Iinclude -c && ar -rv libgtest.a gtest-all.o && ar -rv libgtest_main.a gtest_main.o && sudo mkdir /usr/local/lib32 && sudo mv *.a /usr/local/lib32 && cd ..; fi
36+
- if [ "$TRAVIS_OS_NAME" == "osx" ]; then unzip -q gtest-1.7.0.zip && cd gtest-1.7.0 && sudo cp -r include/gtest /usr/local/include && clang++ src/gtest-all.cc -I. -Iinclude -c && g++ src/gtest_main.cc -I. -Iinclude -c && ar -rv libgtest.a gtest-all.o && ar -rv libgtest_main.a gtest_main.o && sudo mv *.a /usr/local/lib && cd ..; fi
3437

3538
env:
3639
- ACTION="ci/build_autotools.sh" VERSION="cpp03" ARCH="64" LIBPATH="/usr/local/lib" BOOST="boost" BOOST_INC="/usr/local/boost"
@@ -43,7 +46,7 @@ env:
4346
- ACTION="ci/build_cmake.sh" VERSION="cpp03" ARCH="32" LIBPATH="/usr/local/lib32" BOOST="boost" BOOST_INC="/usr/local/boost"
4447

4548
before_script:
46-
- export PATH=/usr/local/bin:$PATH && rm -rf install1
49+
- export PATH=/usr/local/bin:$PATH && rm -rf install
4750

4851
matrix:
4952
exclude:

CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ ENDIF ()
5555

5656
IF (MSGPACK_BOOST)
5757
SET (CMAKE_CXX_FLAGS "-DMSGPACK_USE_BOOST ${CMAKE_CXX_FLAGS}")
58+
SET (Boost_USE_STATIC_LIBS ON) # only find static libs
59+
SET (Boost_USE_MULTITHREADED ON)
60+
SET (Boost_USE_STATIC_RUNTIME OFF)
61+
FIND_PACKAGE (Boost COMPONENTS chrono timer system)
5862
ENDIF ()
5963

6064
FILE (GLOB_RECURSE PREDEF_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/external/boost/predef/include/boost ${CMAKE_CURRENT_SOURCE_DIR}/external/boost/predef/include/boost/*.h)
@@ -221,7 +225,6 @@ INCLUDE_DIRECTORIES (
221225
./
222226
include/
223227
${CMAKE_CURRENT_BINARY_DIR}/include/
224-
${MSGPACK_BOOST_DIR}
225228
)
226229

227230
ADD_LIBRARY (msgpack SHARED
@@ -253,9 +256,9 @@ IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" ST
253256
ENDIF ()
254257
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
255258
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
256-
STRING(REGEX REPLACE "/W[0-4]" "/W3" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
259+
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
257260
ELSE ()
258-
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
261+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
259262
ENDIF ()
260263
ENDIF ()
261264

@@ -267,6 +270,8 @@ IF (NOT DEFINED CMAKE_INSTALL_LIBDIR)
267270
SET(CMAKE_INSTALL_LIBDIR lib)
268271
ENDIF ()
269272

273+
ADD_SUBDIRECTORY (example)
274+
270275
INSTALL (TARGETS msgpack msgpack-static DESTINATION ${CMAKE_INSTALL_LIBDIR})
271276
INSTALL (DIRECTORY include DESTINATION ${CMAKE_INSTALL_PREFIX})
272277
IF (NOT MSVC)

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ before_build:
2424
build_script:
2525
- md build
2626
- cd build
27-
- cmake -DGTEST_LIBRARY=%APPVEYOR_BUILD_FOLDER%\gtest-1.7.0\build\Release\gtest.lib -DGTEST_MAIN_LIBRARY=%APPVEYOR_BUILD_FOLDER%\gtest-1.7.0\build\Release\gtest_main.lib -DGTEST_INCLUDE_DIR=%APPVEYOR_BUILD_FOLDER%\gtest-1.7.0\include -DZLIB_LIBRARY=%APPVEYOR_BUILD_FOLDER%\zlib-1.2.8\build\Release\zlib.lib -DZLIB_INCLUDE_DIR=%APPVEYOR_BUILD_FOLDER%\zlib-1.2.8 ..
27+
- cmake -DMSGPACK_BOOST=ON -DMSGPACK_BOOST_DIR=C:\Libraries\boost -DGTEST_LIBRARY=%APPVEYOR_BUILD_FOLDER%\gtest-1.7.0\build\Release\gtest.lib -DGTEST_MAIN_LIBRARY=%APPVEYOR_BUILD_FOLDER%\gtest-1.7.0\build\Release\gtest_main.lib -DGTEST_INCLUDE_DIR=%APPVEYOR_BUILD_FOLDER%\gtest-1.7.0\include -DZLIB_LIBRARY=%APPVEYOR_BUILD_FOLDER%\zlib-1.2.8\build\Release\zlib.lib -DZLIB_INCLUDE_DIR=%APPVEYOR_BUILD_FOLDER%\zlib-1.2.8 ..
2828
- cmake --build . --config Release
2929

3030
test_script:

example/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ADD_SUBDIRECTORY (c)
2+
ADD_SUBDIRECTORY (cpp03)
3+
ADD_SUBDIRECTORY (cpp11)

example/c/CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
INCLUDE_DIRECTORIES (
2+
../include
3+
)
4+
5+
LIST (APPEND exec_PROGRAMS
6+
lib_buffer_unpack.c
7+
simple_c.c
8+
speed_test_uint32_array.c
9+
speed_test_uint64_array.c
10+
user_buffer_unpack.c
11+
)
12+
13+
FOREACH (source_file ${exec_PROGRAMS})
14+
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
15+
ADD_EXECUTABLE (
16+
${source_file_we}
17+
${source_file}
18+
)
19+
TARGET_LINK_LIBRARIES (${source_file_we}
20+
msgpack
21+
)
22+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
23+
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wall -Wextra -Werror -Wno-mismatched-tags -g -O3")
24+
ENDIF ()
25+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
26+
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
27+
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
28+
ELSE ()
29+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
30+
ENDIF ()
31+
ENDIF ()
32+
ENDFOREACH ()
File renamed without changes.

example/c/speed_test_uint32_array.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ void test()
77
msgpack_packer * pk;
88
size_t upk_pos = 0;
99
msgpack_unpacked msg;
10-
10+
1111
msgpack_sbuffer_init(&buf);
1212

1313
pk = msgpack_packer_new(&buf, msgpack_sbuffer_write);
1414

1515
msgpack_pack_array(pk, size);
1616
{
17-
int idx = 0;
17+
size_t idx = 0;
1818
for (; idx < size; ++idx)
1919
msgpack_pack_uint32(pk, 1);
2020
}
@@ -28,7 +28,7 @@ void test()
2828
msgpack_sbuffer_destroy(&buf);
2929
}
3030

31-
int main(int argc, char **argv)
31+
int main(void)
3232
{
3333
int i = 0;
3434
for (; i < 10; ++i) test();

example/c/speed_test_uint64_array.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void test()
1515

1616
msgpack_pack_array(pk, size);
1717
{
18-
int idx = 0;
18+
size_t idx = 0;
1919
for (; idx < size; ++idx)
2020
msgpack_pack_uint64(pk, test_u64);
2121
}
@@ -29,7 +29,7 @@ void test()
2929
msgpack_sbuffer_destroy(&buf);
3030
}
3131

32-
int main(int argc, char **argv)
32+
int main(void)
3333
{
3434
int i = 0;
3535
for (; i < 10; ++i) test();

example/cpp03/CMakeLists.txt

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
LIST (APPEND exec_PROGRAMS
2+
class_intrusive.cpp
3+
class_intrusive_map.cpp
4+
class_non_intrusive.cpp
5+
custom.cpp
6+
enum.cpp
7+
map_based_versionup.cpp
8+
protocol.cpp
9+
protocol_new.cpp
10+
reuse_zone.cpp
11+
simple.cpp
12+
)
13+
14+
IF (NOT MSVC)
15+
LIST (APPEND with_pthread_PROGRAMS
16+
stream.cpp
17+
)
18+
ENDIF ()
19+
20+
IF (MSGPACK_BOOST)
21+
IF (NOT MSVC)
22+
LIST (APPEND with_boost_lib_PROGRAMS
23+
speed_test.cpp
24+
speed_test_nested_array.cpp
25+
)
26+
ENDIF ()
27+
ENDIF ()
28+
29+
FOREACH (source_file ${exec_PROGRAMS})
30+
INCLUDE_DIRECTORIES (
31+
../include
32+
${MSGPACK_BOOST_DIR}
33+
)
34+
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
35+
ADD_EXECUTABLE (
36+
${source_file_we}
37+
${source_file}
38+
)
39+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
40+
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-std=c++03 -Wall -Wextra -Werror -Wno-mismatched-tags -g -O3")
41+
ENDIF ()
42+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
43+
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
44+
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
45+
ELSE ()
46+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
47+
ENDIF ()
48+
ENDIF ()
49+
ENDFOREACH ()
50+
51+
FOREACH (source_file ${with_pthread_PROGRAMS})
52+
INCLUDE_DIRECTORIES (
53+
../include
54+
${MSGPACK_BOOST_DIR}
55+
)
56+
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
57+
ADD_EXECUTABLE (
58+
${source_file_we}
59+
${source_file}
60+
)
61+
TARGET_LINK_LIBRARIES (${source_file_we}
62+
pthread
63+
)
64+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
65+
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-std=c++03 -Wall -Wextra -Werror -Wno-mismatched-tags -g -O3 -pthread")
66+
ENDIF ()
67+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
68+
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
69+
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
70+
ELSE ()
71+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
72+
ENDIF ()
73+
ENDIF ()
74+
ENDFOREACH ()
75+
76+
FOREACH (source_file ${with_boost_lib_PROGRAMS})
77+
INCLUDE_DIRECTORIES (
78+
../include
79+
${Boost_INCLUDE_DIRS}
80+
)
81+
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
82+
ADD_EXECUTABLE (
83+
${source_file_we}
84+
${source_file}
85+
)
86+
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
87+
TARGET_LINK_LIBRARIES (${source_file_we}
88+
${Boost_TIMER_LIBRARY}
89+
${Boost_CHRONO_LIBRARY}
90+
${Boost_SYSTEM_LIBRARY}
91+
)
92+
IF (NOT MSVC AND NOT APPLE)
93+
TARGET_LINK_LIBRARIES (${source_file_we}
94+
rt
95+
)
96+
ENDIF ()
97+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
98+
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-std=c++03 -Wall -Wextra -Werror -Wno-mismatched-tags -g -O3")
99+
ENDIF ()
100+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
101+
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
102+
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
103+
ELSE ()
104+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
105+
ENDIF ()
106+
ENDIF ()
107+
ENDFOREACH ()
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// MessagePack for C++ example
2+
//
3+
// Copyright (C) 2015 KONDO Takatoshi
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
18+
#include <string>
19+
#include <iostream>
20+
#include <iomanip>
21+
#include <sstream>
22+
#include <cassert>
23+
24+
#include <msgpack.hpp>
25+
26+
27+
class my_class {
28+
public:
29+
my_class() {} // When you want to convert from msgpack::object to my_class,
30+
// my_class should be default constractible.
31+
my_class(std::string const& name, int age):name_(name), age_(age) {}
32+
33+
friend bool operator==(my_class const& lhs, my_class const& rhs) {
34+
return lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_;
35+
}
36+
37+
private:
38+
std::string name_;
39+
int age_;
40+
41+
public:
42+
MSGPACK_DEFINE(name_, age_);
43+
};
44+
45+
void print(std::string const& buf) {
46+
for (std::string::const_iterator it = buf.begin(), end = buf.end();
47+
it != end;
48+
++it) {
49+
std::cout
50+
<< std::setw(2)
51+
<< std::hex
52+
<< std::setfill('0')
53+
<< (static_cast<int>(*it) & 0xff)
54+
<< ' ';
55+
}
56+
std::cout << std::dec << std::endl;
57+
}
58+
59+
int main() {
60+
{ // pack, unpack
61+
my_class my("John Smith", 42);
62+
std::stringstream ss;
63+
msgpack::pack(ss, my);
64+
65+
print(ss.str());
66+
67+
msgpack::unpacked unp;
68+
msgpack::unpack(unp, ss.str().data(), ss.str().size());
69+
msgpack::object obj = unp.get();
70+
std::cout << obj << std::endl;
71+
assert(obj.as<my_class>() == my);
72+
}
73+
{ // create object with zone
74+
my_class my("John Smith", 42);
75+
msgpack::zone z;
76+
msgpack::object obj(my, z);
77+
std::cout << obj << std::endl;
78+
assert(obj.as<my_class>() == my);
79+
}
80+
}

0 commit comments

Comments
 (0)