Added 'as' support for boost containers.

This commit is contained in:
Takatoshi Kondo
2015-07-31 16:56:07 +09:00
parent 7e6a498c14
commit 2034427cfd
8 changed files with 250 additions and 17 deletions

View File

@@ -11,7 +11,7 @@ before_install:
- sudo apt-get update
install:
- sudo apt-get install -qq gcc-4.8-multilib g++-4.8-multilib
- sudo apt-get install --allow-unauthenticated -qq clang-3.4
- sudo apt-get install --allow-unauthenticated -qq clang-3.5
- sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 90
- sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 90
- sudo apt-get install -y lib32gcc1
@@ -21,9 +21,9 @@ install:
- sudo apt-get install -y bzip2
- sudo apt-get install -y libc6-dbg
- wget https://googletest.googlecode.com/files/gtest-1.7.0.zip
- wget http://sourceforge.net/projects/boost/files/boost/1.57.0/
- wget http://sourceforge.net/projects/boost/files/boost/1.58.0/
- 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 ..
- 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.57.0/boost_1_57_0.zip && unzip -q boost_1_57_0.zip && sudo mkdir /usr/local/boost && sudo cp -r boost_1_57_0/boost /usr/local/boost/
- 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/
env:
- ACTION="ci/build_autotools.sh" VERSION="cpp11" ARCH="64" LIBPATH="/usr/local/lib" BOOST="boost" BOOST_INC="/usr/local/boost"
- ACTION="ci/build_autotools.sh" VERSION="cpp11" ARCH="64" LIBPATH="/usr/local/lib" BOOST=""

View File

@@ -21,10 +21,18 @@
#include "msgpack/versioning.hpp"
#include "msgpack/adaptor/adaptor_base.hpp"
#include "msgpack/adaptor/check_container_size.hpp"
#include "msgpack/meta.hpp"
#if !defined (MSGPACK_USE_CPP03)
#include "msgpack/adaptor/cpp11/tuple.hpp"
#endif // #if !defined (MSGPACK_USE_CPP03)
#include <boost/fusion/support/is_sequence.hpp>
#include <boost/fusion/sequence/intrinsic/size.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/sequence/intrinsic/at.hpp>
#include <boost/fusion/include/mpl.hpp>
#include <boost/mpl/size.hpp>
namespace msgpack {
@@ -34,6 +42,53 @@ MSGPACK_API_VERSION_NAMESPACE(v1) {
namespace adaptor {
#if !defined (MSGPACK_USE_CPP03)
template <typename T>
struct as<
T,
typename msgpack::enable_if<
boost::fusion::traits::is_sequence<T>::value &&
boost::mpl::fold<
T,
boost::mpl::bool_<true>,
boost::mpl::if_ <
boost::mpl::and_<
boost::mpl::_1,
msgpack::has_as<boost::mpl::_2>
>,
boost::mpl::bool_<true>,
boost::mpl::bool_<false>
>
>::type::value
>::type
> {
T operator()(msgpack::object const& o) const {
if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
if (o.via.array.size != checked_get_container_size(boost::mpl::size<T>::value)) {
throw msgpack::type_error();
}
using tuple_t = decltype(to_tuple(std::declval<T>(), gen_seq<boost::mpl::size<T>::value>()));
return to_t(
o.as<tuple_t>(),
msgpack::gen_seq<boost::mpl::size<T>::value>());
}
template<std::size_t... Is, typename U>
static std::tuple<
typename std::remove_reference<
typename boost::fusion::result_of::at_c<T, Is>::type
>::type...>
to_tuple(U const& u, seq<Is...>) {
return std::make_tuple(boost::fusion::at_c<Is>(u)...);
}
template<std::size_t... Is, typename U>
static T to_t(U const& u, seq<Is...>) {
return T(std::get<Is>(u)...);
}
};
#endif // !defined (MSGPACK_USE_CPP03)
template <typename T>
struct convert<T, typename msgpack::enable_if<boost::fusion::traits::is_sequence<T>::value>::type > {
msgpack::object const& operator()(msgpack::object const& o, T& v) const {

View File

@@ -22,8 +22,18 @@
#include "msgpack/adaptor/adaptor_base.hpp"
#include "msgpack/adaptor/check_container_size.hpp"
// To supress warning on Boost.1.58.0
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
#include <boost/optional.hpp>
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
#pragma GCC diagnostic pop
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
namespace msgpack {
/// @cond
@@ -32,6 +42,18 @@ MSGPACK_API_VERSION_NAMESPACE(v1) {
namespace adaptor {
#if !defined (MSGPACK_USE_CPP03)
template <typename T>
struct as<boost::optional<T>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
boost::optional<T> operator()(msgpack::object const& o) const {
if(o.is_nil()) return boost::none;
return o.as<T>();
}
};
#endif // !defined (MSGPACK_USE_CPP03)
template <typename T>
struct convert<boost::optional<T> > {
msgpack::object const& operator()(msgpack::object const& o, boost::optional<T>& v) const {

View File

@@ -22,6 +22,7 @@
#include "msgpack/versioning.hpp"
#include "msgpack/adaptor/adaptor_base.hpp"
#include "msgpack/adaptor/check_container_size.hpp"
#include "msgpack/meta.hpp"
#include <array>
@@ -35,26 +36,20 @@ namespace adaptor {
namespace detail {
template<std::size_t... Is> struct seq {};
template<std::size_t N, std::size_t... Is>
struct gen_seq : gen_seq<N-1, N-1, Is...> {};
template<std::size_t... Is>
struct gen_seq<0, Is...> : seq<Is...> {};
namespace array {
template<typename T, std::size_t N1, std::size_t... I1, std::size_t N2, std::size_t... I2>
inline std::array<T, N1+N2> concat(
std::array<T, N1>&& a1,
std::array<T, N2>&& a2,
seq<I1...>,
seq<I2...>) {
msgpack::seq<I1...>,
msgpack::seq<I2...>) {
return {{ std::move(a1[I1])..., std::move(a2[I2])... }};
}
template<typename T, std::size_t N1, std::size_t N2>
inline std::array<T, N1+N2> concat(std::array<T, N1>&& a1, std::array<T, N2>&& a2) {
return concat(std::move(a1), std::move(a2), gen_seq<N1>(), gen_seq<N2>());
return concat(std::move(a1), std::move(a2), msgpack::gen_seq<N1>(), msgpack::gen_seq<N2>());
}
template <typename T, std::size_t N>
@@ -72,6 +67,8 @@ struct as_impl<T, 0> {
}
};
} // namespace array
} // namespace detail
template <typename T, std::size_t N>
@@ -79,7 +76,7 @@ struct as<std::array<T, N>, typename std::enable_if<msgpack::has_as<T>::value>::
std::array<T, N> operator()(msgpack::object const& o) const {
if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
if(o.via.array.size != N) { throw msgpack::type_error(); }
return detail::as_impl<T, N>::as(o);
return detail::array::as_impl<T, N>::as(o);
}
};

View File

@@ -40,6 +40,14 @@ template<bool...values> struct all_of_imp
template<template <class> class T, class... U>
using all_of = detail::all_of_imp<T<U>::value...>;
template<std::size_t... Is> struct seq {};
template<std::size_t N, std::size_t... Is>
struct gen_seq : gen_seq<N-1, N-1, Is...> {};
template<std::size_t... Is>
struct gen_seq<0, Is...> : seq<Is...> {};
/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
/// @endcond

View File

@@ -100,8 +100,8 @@ private:
T>::type;
template <typename>
static std::false_type check(...);
using type = decltype(check<T>(nullptr));
public:
using type = decltype(check<T>(nullptr));
static constexpr bool value = type::value;
};

View File

@@ -12,6 +12,7 @@
#if defined(MSGPACK_USE_BOOST)
#include <boost/fusion/adapted/struct/define_struct.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
const double kEPS = 1e-10;
@@ -48,4 +49,109 @@ TEST(MSGPACK_BOOST, object_with_zone_convert)
EXPECT_TRUE(fabs(val2.f2 - val1.f2) <= kEPS);
}
#if !defined(MSGPACK_USE_CPP03)
struct no_def_con1 {
no_def_con1() = delete;
no_def_con1(int i):i(i) {}
int i;
MSGPACK_DEFINE(i);
};
inline bool operator==(no_def_con1 const& lhs, no_def_con1 const& rhs) {
return lhs.i == rhs.i;
}
inline bool operator!=(no_def_con1 const& lhs, no_def_con1 const& rhs) {
return !(lhs == rhs);
}
struct no_def_con2 {
no_def_con2() = delete;
no_def_con2(int i):i(i) {}
int i;
MSGPACK_DEFINE(i);
};
inline bool operator==(no_def_con2 const& lhs, no_def_con2 const& rhs) {
return lhs.i == rhs.i;
}
inline bool operator!=(no_def_con2 const& lhs, no_def_con2 const& rhs) {
return !(lhs == rhs);
}
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
namespace adaptor {
template <>
struct as<no_def_con1> {
no_def_con1 operator()(msgpack::object const& o) const {
if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
if (o.via.array.size != 1) throw msgpack::type_error();
return no_def_con1(o.via.array.ptr[0].as<int>());
}
};
template <>
struct as<no_def_con2> {
no_def_con2 operator()(msgpack::object const& o) const {
if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
if (o.via.array.size != 1) throw msgpack::type_error();
return no_def_con2(o.via.array.ptr[0].as<int>());
}
};
} // adaptor
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
} // msgpack
struct mystruct_no_def_con {
mystruct_no_def_con() = delete;
// Constructor that have parameters corresponding to BOOST_FUSION_ADAPT_STRUCT is mandatory.
// See *1, *2, and *3
mystruct_no_def_con(
no_def_con1 i,
no_def_con2 j,
no_def_con1 k):
f1(std::move(i)),
f2(std::move(j)),
f3(std::move(k)) {}
no_def_con1 f1;
no_def_con2 f2;
no_def_con1 f3;
};
inline bool operator==(mystruct_no_def_con const& lhs, mystruct_no_def_con const& rhs) {
return lhs.f1 == rhs.f1 && lhs.f2 == rhs.f2 && lhs.f3 == rhs.f3;
}
inline bool operator!=(mystruct_no_def_con const& lhs, mystruct_no_def_con const& rhs) {
return !(lhs == rhs);
}
BOOST_FUSION_ADAPT_STRUCT(
mystruct_no_def_con,
f1, // *1
f2, // *2
f3 // *3
)
TEST(MSGPACK_BOOST, pack_convert_no_def_con)
{
std::stringstream ss;
mystruct_no_def_con val1(no_def_con1(1), no_def_con2(2), no_def_con1(3));
msgpack::pack(ss, val1);
msgpack::unpacked ret;
msgpack::unpack(ret, ss.str().data(), ss.str().size());
mystruct_no_def_con val2 = ret.get().as<mystruct_no_def_con>();
EXPECT_TRUE(val1 == val2);
}
#endif // !defined(MSGPACK_USE_CPP03
#endif // defined(MSGPACK_USE_BOOST)

View File

@@ -127,4 +127,49 @@ TEST(MSGPACK_BOOST, object_with_zone_vector_optional)
EXPECT_TRUE(val1 == val2);
}
#if !defined(MSGPACK_USE_CPP03)
struct no_def_con {
no_def_con() = delete;
no_def_con(int i):i(i) {}
int i;
MSGPACK_DEFINE(i);
};
inline bool operator==(no_def_con const& lhs, no_def_con const& rhs) {
return lhs.i == rhs.i;
}
inline bool operator!=(no_def_con const& lhs, no_def_con const& rhs) {
return !(lhs == rhs);
}
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
namespace adaptor {
template <>
struct as<no_def_con> {
no_def_con operator()(msgpack::object const& o) const {
if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
if (o.via.array.size != 1) throw msgpack::type_error();
return no_def_con(o.via.array.ptr[0].as<int>());
}
};
} // adaptor
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
} // msgpack
TEST(MSGPACK_BOOST, pack_convert_no_def_con)
{
std::stringstream ss;
boost::optional<no_def_con> val1 = no_def_con(1);
msgpack::pack(ss, val1);
msgpack::unpacked ret;
msgpack::unpack(ret, ss.str().data(), ss.str().size());
boost::optional<no_def_con> val2 = ret.get().as<boost::optional<no_def_con>>();
EXPECT_TRUE(val1 == val2);
}
#endif // !defined(MSGPACK_USE_CPP03
#endif // defined(MSGPACK_USE_BOOST)