Skip to content

Commit fee4faa

Browse files
committed
Merge pull request #331 from redboltz/add_as_support_for_boost
Added 'as' support for boost containers.
2 parents e182b06 + 2034427 commit fee4faa

File tree

8 files changed

+250
-17
lines changed

8 files changed

+250
-17
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ before_install:
1111
- sudo apt-get update
1212
install:
1313
- sudo apt-get install -qq gcc-4.8-multilib g++-4.8-multilib
14-
- sudo apt-get install --allow-unauthenticated -qq clang-3.4
14+
- sudo apt-get install --allow-unauthenticated -qq clang-3.5
1515
- sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 90
1616
- sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 90
1717
- sudo apt-get install -y lib32gcc1
@@ -21,9 +21,9 @@ install:
2121
- sudo apt-get install -y bzip2
2222
- sudo apt-get install -y libc6-dbg
2323
- wget https://googletest.googlecode.com/files/gtest-1.7.0.zip
24-
- wget http://sourceforge.net/projects/boost/files/boost/1.57.0/
24+
- wget http://sourceforge.net/projects/boost/files/boost/1.58.0/
2525
- 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 ..
26-
- 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/
26+
- 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/
2727
env:
2828
- ACTION="ci/build_autotools.sh" VERSION="cpp11" ARCH="64" LIBPATH="/usr/local/lib" BOOST="boost" BOOST_INC="/usr/local/boost"
2929
- ACTION="ci/build_autotools.sh" VERSION="cpp11" ARCH="64" LIBPATH="/usr/local/lib" BOOST=""

include/msgpack/adaptor/boost/fusion.hpp

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,18 @@
2121
#include "msgpack/versioning.hpp"
2222
#include "msgpack/adaptor/adaptor_base.hpp"
2323
#include "msgpack/adaptor/check_container_size.hpp"
24+
#include "msgpack/meta.hpp"
25+
26+
#if !defined (MSGPACK_USE_CPP03)
27+
#include "msgpack/adaptor/cpp11/tuple.hpp"
28+
#endif // #if !defined (MSGPACK_USE_CPP03)
2429

2530
#include <boost/fusion/support/is_sequence.hpp>
2631
#include <boost/fusion/sequence/intrinsic/size.hpp>
2732
#include <boost/fusion/algorithm/iteration/for_each.hpp>
33+
#include <boost/fusion/sequence/intrinsic/at.hpp>
34+
#include <boost/fusion/include/mpl.hpp>
35+
#include <boost/mpl/size.hpp>
2836

2937
namespace msgpack {
3038

@@ -34,11 +42,58 @@ MSGPACK_API_VERSION_NAMESPACE(v1) {
3442

3543
namespace adaptor {
3644

45+
#if !defined (MSGPACK_USE_CPP03)
46+
47+
template <typename T>
48+
struct as<
49+
T,
50+
typename msgpack::enable_if<
51+
boost::fusion::traits::is_sequence<T>::value &&
52+
boost::mpl::fold<
53+
T,
54+
boost::mpl::bool_<true>,
55+
boost::mpl::if_ <
56+
boost::mpl::and_<
57+
boost::mpl::_1,
58+
msgpack::has_as<boost::mpl::_2>
59+
>,
60+
boost::mpl::bool_<true>,
61+
boost::mpl::bool_<false>
62+
>
63+
>::type::value
64+
>::type
65+
> {
66+
T operator()(msgpack::object const& o) const {
67+
if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
68+
if (o.via.array.size != checked_get_container_size(boost::mpl::size<T>::value)) {
69+
throw msgpack::type_error();
70+
}
71+
using tuple_t = decltype(to_tuple(std::declval<T>(), gen_seq<boost::mpl::size<T>::value>()));
72+
return to_t(
73+
o.as<tuple_t>(),
74+
msgpack::gen_seq<boost::mpl::size<T>::value>());
75+
}
76+
template<std::size_t... Is, typename U>
77+
static std::tuple<
78+
typename std::remove_reference<
79+
typename boost::fusion::result_of::at_c<T, Is>::type
80+
>::type...>
81+
to_tuple(U const& u, seq<Is...>) {
82+
return std::make_tuple(boost::fusion::at_c<Is>(u)...);
83+
}
84+
template<std::size_t... Is, typename U>
85+
static T to_t(U const& u, seq<Is...>) {
86+
return T(std::get<Is>(u)...);
87+
}
88+
};
89+
90+
#endif // !defined (MSGPACK_USE_CPP03)
91+
3792
template <typename T>
3893
struct convert<T, typename msgpack::enable_if<boost::fusion::traits::is_sequence<T>::value>::type > {
3994
msgpack::object const& operator()(msgpack::object const& o, T& v) const {
40-
if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
41-
if(o.via.array.size != checked_get_container_size(boost::fusion::size(v))) {
95+
if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
96+
if (o.via.array.size != checked_get_container_size(boost::fusion::size(v))) {
4297
throw msgpack::type_error();
4398
}
4499
uint32_t index = 0;

include/msgpack/adaptor/boost/optional.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,18 @@
2222
#include "msgpack/adaptor/adaptor_base.hpp"
2323
#include "msgpack/adaptor/check_container_size.hpp"
2424

25+
// To supress warning on Boost.1.58.0
26+
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
27+
#pragma GCC diagnostic push
28+
#pragma GCC diagnostic ignored "-Wunused-parameter"
29+
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
30+
2531
#include <boost/optional.hpp>
2632

33+
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
34+
#pragma GCC diagnostic pop
35+
#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
36+
2737
namespace msgpack {
2838

2939
/// @cond
@@ -32,6 +42,18 @@ MSGPACK_API_VERSION_NAMESPACE(v1) {
3242

3343
namespace adaptor {
3444

45+
#if !defined (MSGPACK_USE_CPP03)
46+
47+
template <typename T>
48+
struct as<boost::optional<T>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
49+
boost::optional<T> operator()(msgpack::object const& o) const {
50+
if(o.is_nil()) return boost::none;
51+
return o.as<T>();
52+
}
53+
};
54+
55+
#endif // !defined (MSGPACK_USE_CPP03)
56+
3557
template <typename T>
3658
struct convert<boost::optional<T> > {
3759
msgpack::object const& operator()(msgpack::object const& o, boost::optional<T>& v) const {

include/msgpack/adaptor/cpp11/array.hpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "msgpack/versioning.hpp"
2323
#include "msgpack/adaptor/adaptor_base.hpp"
2424
#include "msgpack/adaptor/check_container_size.hpp"
25+
#include "msgpack/meta.hpp"
2526

2627
#include <array>
2728

@@ -35,26 +36,20 @@ namespace adaptor {
3536

3637
namespace detail {
3738

38-
template<std::size_t... Is> struct seq {};
39-
40-
template<std::size_t N, std::size_t... Is>
41-
struct gen_seq : gen_seq<N-1, N-1, Is...> {};
42-
43-
template<std::size_t... Is>
44-
struct gen_seq<0, Is...> : seq<Is...> {};
39+
namespace array {
4540

4641
template<typename T, std::size_t N1, std::size_t... I1, std::size_t N2, std::size_t... I2>
4742
inline std::array<T, N1+N2> concat(
4843
std::array<T, N1>&& a1,
4944
std::array<T, N2>&& a2,
50-
seq<I1...>,
51-
seq<I2...>) {
45+
msgpack::seq<I1...>,
46+
msgpack::seq<I2...>) {
5247
return {{ std::move(a1[I1])..., std::move(a2[I2])... }};
5348
}
5449

5550
template<typename T, std::size_t N1, std::size_t N2>
5651
inline std::array<T, N1+N2> concat(std::array<T, N1>&& a1, std::array<T, N2>&& a2) {
57-
return concat(std::move(a1), std::move(a2), gen_seq<N1>(), gen_seq<N2>());
52+
return concat(std::move(a1), std::move(a2), msgpack::gen_seq<N1>(), msgpack::gen_seq<N2>());
5853
}
5954

6055
template <typename T, std::size_t N>
@@ -72,14 +67,16 @@ struct as_impl<T, 0> {
7267
}
7368
};
7469

70+
} // namespace array
71+
7572
} // namespace detail
7673

7774
template <typename T, std::size_t N>
7875
struct as<std::array<T, N>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
7976
std::array<T, N> operator()(msgpack::object const& o) const {
8077
if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
8178
if(o.via.array.size != N) { throw msgpack::type_error(); }
82-
return detail::as_impl<T, N>::as(o);
79+
return detail::array::as_impl<T, N>::as(o);
8380
}
8481
};
8582

include/msgpack/meta.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ template<bool...values> struct all_of_imp
4040
template<template <class> class T, class... U>
4141
using all_of = detail::all_of_imp<T<U>::value...>;
4242

43+
template<std::size_t... Is> struct seq {};
44+
45+
template<std::size_t N, std::size_t... Is>
46+
struct gen_seq : gen_seq<N-1, N-1, Is...> {};
47+
48+
template<std::size_t... Is>
49+
struct gen_seq<0, Is...> : seq<Is...> {};
50+
4351
/// @cond
4452
} // MSGPACK_API_VERSION_NAMESPACE(v1)
4553
/// @endcond

include/msgpack/object_fwd.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ struct has_as {
100100
T>::type;
101101
template <typename>
102102
static std::false_type check(...);
103-
using type = decltype(check<T>(nullptr));
104103
public:
104+
using type = decltype(check<T>(nullptr));
105105
static constexpr bool value = type::value;
106106
};
107107

test/boost_fusion.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#if defined(MSGPACK_USE_BOOST)
1313

1414
#include <boost/fusion/adapted/struct/define_struct.hpp>
15+
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
1516

1617
const double kEPS = 1e-10;
1718

@@ -48,4 +49,109 @@ TEST(MSGPACK_BOOST, object_with_zone_convert)
4849
EXPECT_TRUE(fabs(val2.f2 - val1.f2) <= kEPS);
4950
}
5051

52+
#if !defined(MSGPACK_USE_CPP03)
53+
54+
struct no_def_con1 {
55+
no_def_con1() = delete;
56+
no_def_con1(int i):i(i) {}
57+
int i;
58+
MSGPACK_DEFINE(i);
59+
};
60+
61+
inline bool operator==(no_def_con1 const& lhs, no_def_con1 const& rhs) {
62+
return lhs.i == rhs.i;
63+
}
64+
65+
inline bool operator!=(no_def_con1 const& lhs, no_def_con1 const& rhs) {
66+
return !(lhs == rhs);
67+
}
68+
69+
struct no_def_con2 {
70+
no_def_con2() = delete;
71+
no_def_con2(int i):i(i) {}
72+
int i;
73+
MSGPACK_DEFINE(i);
74+
};
75+
76+
inline bool operator==(no_def_con2 const& lhs, no_def_con2 const& rhs) {
77+
return lhs.i == rhs.i;
78+
}
79+
80+
inline bool operator!=(no_def_con2 const& lhs, no_def_con2 const& rhs) {
81+
return !(lhs == rhs);
82+
}
83+
84+
85+
namespace msgpack {
86+
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
87+
namespace adaptor {
88+
89+
template <>
90+
struct as<no_def_con1> {
91+
no_def_con1 operator()(msgpack::object const& o) const {
92+
if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
93+
if (o.via.array.size != 1) throw msgpack::type_error();
94+
return no_def_con1(o.via.array.ptr[0].as<int>());
95+
}
96+
};
97+
98+
template <>
99+
struct as<no_def_con2> {
100+
no_def_con2 operator()(msgpack::object const& o) const {
101+
if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
102+
if (o.via.array.size != 1) throw msgpack::type_error();
103+
return no_def_con2(o.via.array.ptr[0].as<int>());
104+
}
105+
};
106+
107+
} // adaptor
108+
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
109+
} // msgpack
110+
111+
struct mystruct_no_def_con {
112+
mystruct_no_def_con() = delete;
113+
// Constructor that have parameters corresponding to BOOST_FUSION_ADAPT_STRUCT is mandatory.
114+
// See *1, *2, and *3
115+
mystruct_no_def_con(
116+
no_def_con1 i,
117+
no_def_con2 j,
118+
no_def_con1 k):
119+
f1(std::move(i)),
120+
f2(std::move(j)),
121+
f3(std::move(k)) {}
122+
123+
no_def_con1 f1;
124+
no_def_con2 f2;
125+
no_def_con1 f3;
126+
};
127+
128+
inline bool operator==(mystruct_no_def_con const& lhs, mystruct_no_def_con const& rhs) {
129+
return lhs.f1 == rhs.f1 && lhs.f2 == rhs.f2 && lhs.f3 == rhs.f3;
130+
}
131+
132+
inline bool operator!=(mystruct_no_def_con const& lhs, mystruct_no_def_con const& rhs) {
133+
return !(lhs == rhs);
134+
}
135+
136+
BOOST_FUSION_ADAPT_STRUCT(
137+
mystruct_no_def_con,
138+
f1, // *1
139+
f2, // *2
140+
f3 // *3
141+
)
142+
143+
TEST(MSGPACK_BOOST, pack_convert_no_def_con)
144+
{
145+
std::stringstream ss;
146+
mystruct_no_def_con val1(no_def_con1(1), no_def_con2(2), no_def_con1(3));
147+
msgpack::pack(ss, val1);
148+
msgpack::unpacked ret;
149+
msgpack::unpack(ret, ss.str().data(), ss.str().size());
150+
mystruct_no_def_con val2 = ret.get().as<mystruct_no_def_con>();
151+
EXPECT_TRUE(val1 == val2);
152+
}
153+
154+
155+
#endif // !defined(MSGPACK_USE_CPP03
156+
51157
#endif // defined(MSGPACK_USE_BOOST)

test/boost_optional.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,49 @@ TEST(MSGPACK_BOOST, object_with_zone_vector_optional)
127127
EXPECT_TRUE(val1 == val2);
128128
}
129129

130+
#if !defined(MSGPACK_USE_CPP03)
131+
132+
struct no_def_con {
133+
no_def_con() = delete;
134+
no_def_con(int i):i(i) {}
135+
int i;
136+
MSGPACK_DEFINE(i);
137+
};
138+
139+
inline bool operator==(no_def_con const& lhs, no_def_con const& rhs) {
140+
return lhs.i == rhs.i;
141+
}
142+
143+
inline bool operator!=(no_def_con const& lhs, no_def_con const& rhs) {
144+
return !(lhs == rhs);
145+
}
146+
147+
namespace msgpack {
148+
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
149+
namespace adaptor {
150+
template <>
151+
struct as<no_def_con> {
152+
no_def_con operator()(msgpack::object const& o) const {
153+
if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
154+
if (o.via.array.size != 1) throw msgpack::type_error();
155+
return no_def_con(o.via.array.ptr[0].as<int>());
156+
}
157+
};
158+
} // adaptor
159+
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
160+
} // msgpack
161+
162+
TEST(MSGPACK_BOOST, pack_convert_no_def_con)
163+
{
164+
std::stringstream ss;
165+
boost::optional<no_def_con> val1 = no_def_con(1);
166+
msgpack::pack(ss, val1);
167+
msgpack::unpacked ret;
168+
msgpack::unpack(ret, ss.str().data(), ss.str().size());
169+
boost::optional<no_def_con> val2 = ret.get().as<boost::optional<no_def_con>>();
170+
EXPECT_TRUE(val1 == val2);
171+
}
172+
173+
#endif // !defined(MSGPACK_USE_CPP03
174+
130175
#endif // defined(MSGPACK_USE_BOOST)

0 commit comments

Comments
 (0)