Added examples to cmake building process.

Set /WX flas that is warnings as errors on MSVC build.
Updated linux, osx, msvc build to refer to appropriate boost libraries.
This commit is contained in:
Takatoshi Kondo
2015-08-12 12:53:56 +09:00
parent e50cc5d79f
commit 12b5a6235a
15 changed files with 284 additions and 25 deletions

View File

@@ -0,0 +1,107 @@
LIST (APPEND exec_PROGRAMS
class_intrusive.cpp
class_intrusive_map.cpp
class_non_intrusive.cpp
custom.cpp
enum.cpp
map_based_versionup.cpp
protocol.cpp
protocol_new.cpp
reuse_zone.cpp
simple.cpp
)
IF (NOT MSVC)
LIST (APPEND with_pthread_PROGRAMS
stream.cpp
)
ENDIF ()
IF (MSGPACK_BOOST)
IF (NOT MSVC)
LIST (APPEND with_boost_lib_PROGRAMS
speed_test.cpp
speed_test_nested_array.cpp
)
ENDIF ()
ENDIF ()
FOREACH (source_file ${exec_PROGRAMS})
INCLUDE_DIRECTORIES (
../include
${MSGPACK_BOOST_DIR}
)
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
ADD_EXECUTABLE (
${source_file_we}
${source_file}
)
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-std=c++03 -Wall -Wextra -Werror -Wno-mismatched-tags -g -O3")
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
ELSE ()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
ENDIF ()
ENDIF ()
ENDFOREACH ()
FOREACH (source_file ${with_pthread_PROGRAMS})
INCLUDE_DIRECTORIES (
../include
${MSGPACK_BOOST_DIR}
)
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
ADD_EXECUTABLE (
${source_file_we}
${source_file}
)
TARGET_LINK_LIBRARIES (${source_file_we}
pthread
)
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-std=c++03 -Wall -Wextra -Werror -Wno-mismatched-tags -g -O3 -pthread")
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
ELSE ()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
ENDIF ()
ENDIF ()
ENDFOREACH ()
FOREACH (source_file ${with_boost_lib_PROGRAMS})
INCLUDE_DIRECTORIES (
../include
${Boost_INCLUDE_DIRS}
)
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
ADD_EXECUTABLE (
${source_file_we}
${source_file}
)
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
TARGET_LINK_LIBRARIES (${source_file_we}
${Boost_TIMER_LIBRARY}
${Boost_CHRONO_LIBRARY}
${Boost_SYSTEM_LIBRARY}
)
IF (NOT MSVC AND NOT APPLE)
TARGET_LINK_LIBRARIES (${source_file_we}
rt
)
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-std=c++03 -Wall -Wextra -Werror -Wno-mismatched-tags -g -O3")
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
ELSE ()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
ENDIF ()
ENDIF ()
ENDFOREACH ()

View File

@@ -0,0 +1,80 @@
// MessagePack for C++ example
//
// Copyright (C) 2015 KONDO Takatoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cassert>
#include <msgpack.hpp>
class my_class {
public:
my_class() {} // When you want to convert from msgpack::object to my_class,
// my_class should be default constractible.
my_class(std::string const& name, int age):name_(name), age_(age) {}
friend bool operator==(my_class const& lhs, my_class const& rhs) {
return lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_;
}
private:
std::string name_;
int age_;
public:
MSGPACK_DEFINE(name_, age_);
};
void print(std::string const& buf) {
for (std::string::const_iterator it = buf.begin(), end = buf.end();
it != end;
++it) {
std::cout
<< std::setw(2)
<< std::hex
<< std::setfill('0')
<< (static_cast<int>(*it) & 0xff)
<< ' ';
}
std::cout << std::dec << std::endl;
}
int main() {
{ // pack, unpack
my_class my("John Smith", 42);
std::stringstream ss;
msgpack::pack(ss, my);
print(ss.str());
msgpack::unpacked unp;
msgpack::unpack(unp, ss.str().data(), ss.str().size());
msgpack::object obj = unp.get();
std::cout << obj << std::endl;
assert(obj.as<my_class>() == my);
}
{ // create object with zone
my_class my("John Smith", 42);
msgpack::zone z;
msgpack::object obj(my, z);
std::cout << obj << std::endl;
assert(obj.as<my_class>() == my);
}
}

View File

@@ -29,7 +29,7 @@ template <typename T, std::size_t level>
struct vecvec {
typedef std::vector<typename vecvec<T, level - 1>::type> type;
static void fill(type& v, std::size_t num_of_elems, T const& val) {
for (int elem = 0; elem < num_of_elems; ++elem) {
for (std::size_t elem = 0; elem < num_of_elems; ++elem) {
typename vecvec<T, level - 1>::type child;
vecvec<T, level - 1>::fill(child, num_of_elems, val);
v.push_back(child);
@@ -41,7 +41,7 @@ template <typename T>
struct vecvec<T, 0> {
typedef std::vector<T> type;
static void fill(type& v, std::size_t num_of_elems, T const& val) {
for (int elem = 0; elem < num_of_elems; ++elem) {
for (std::size_t elem = 0; elem < num_of_elems; ++elem) {
v.push_back(val);
}
}
@@ -50,9 +50,9 @@ struct vecvec<T, 0> {
void test_array_of_array() {
std::cout << "[TEST][array_of_array]" << std::endl;
// setup
int const depth = 16;
int const depth = 4;
std::cout << "Setting up array data..." << std::endl;
typename vecvec<int, depth>::type v1;
vecvec<int, depth>::type v1;
vecvec<int, depth>::fill(v1, 3, 42);
std::cout << "Start packing..." << std::endl;
@@ -77,7 +77,7 @@ void test_array_of_array() {
std::cout << result << std::endl;
}
std::cout << "Unpack finished..." << std::endl;
typename vecvec<int, depth>::type v2;
vecvec<int, depth>::type v2;
std::cout << "Start converting..." << std::endl;
{
boost::timer::cpu_timer timer;

View File

@@ -122,7 +122,7 @@ private:
int main(void)
{
int pair[2];
pipe(pair);
if (pipe(pair) != 0) return -1;
// run server thread
Server srv(pair[0]);