Use OR instead of AND for all classes that have multiple types.

Revert example. Combination of has_as and not has_as are move to tests.
This commit is contained in:
Takatoshi Kondo
2016-08-29 22:12:01 +09:00
parent 9a2bb0c972
commit 5d69c22bf9
11 changed files with 168 additions and 46 deletions

View File

@@ -23,16 +23,6 @@ struct my {
my(int a):a(a) {}
int a;
MSGPACK_DEFINE(a);
bool operator==(const my & other) const
{
return other.a == a;
}
bool operator<(const my & other) const
{
return other.a > a;
}
};
namespace msgpack {
@@ -58,34 +48,4 @@ int main() {
msgpack::object obj(m1, z);
std::cout << obj << std::endl;
assert(m1.a == obj.as<my>().a);
// useful keys for maps do not have as<>() method implemented:
std::cout << "has_as<int> " << msgpack::has_as<int>::value << std::endl;
std::cout << "has_as<string> " << msgpack::has_as<std::string>::value << std::endl;
// BEFORE PATCH: as a result as<map<K, V> >() is not available either.
// AFTER PATCH: as<map<K, V> >() is available if key OR value have an as<>() implementation
std::cout << "has_as<std::map<int, my> > " << msgpack::has_as<std::map<int, my> >::value << std::endl;
std::map<int, my> m;
m.emplace(1, my(17));
msgpack::object obj2(m, z);
std::cout << obj2 << std::endl;
// BEFORE PATCH: this makes the following break with a compiler error,
// beacuse it uses the deleted my:my() constructor, as it falls back to the
// convert() method.
// AFTER PATCH: the following works and uses the customary as() implementation
assert(m == obj2.as<decltype(m)>());
std::map<my, int> m2;
m2.emplace(17, 42);
msgpack::object obj3(m2, z);
std::cout << obj3 << std::endl;
// BEFORE PATCH: this makes the following break with a compiler error:
// beacuse it uses the deleted my:my() constructor, as it falls back to the
// convert() method.
// AFTER PATCH: the following works and uses the customary as() implementation
assert(m2 == obj3.as<decltype(m2)>());
}

View File

@@ -45,7 +45,7 @@ struct as<
T,
boost::mpl::bool_<true>,
boost::mpl::if_ <
boost::mpl::and_<
boost::mpl::or_<
boost::mpl::_1,
msgpack::has_as<boost::mpl::_2>
>,

View File

@@ -109,7 +109,7 @@ struct StdTupleConverter<Tuple, 0> {
namespace adaptor {
template <typename... Args>
struct as<std::tuple<Args...>, typename std::enable_if<msgpack::all_of<msgpack::has_as, Args...>::value>::type> {
struct as<std::tuple<Args...>, typename std::enable_if<msgpack::any_of<msgpack::has_as, Args...>::value>::type> {
std::tuple<Args...> operator()(
msgpack::object const& o) const {
if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }

View File

@@ -27,7 +27,7 @@ namespace adaptor {
template <typename K, typename V, typename Hash, typename Compare, typename Alloc>
struct as<
std::unordered_map<K, V, Hash, Compare, Alloc>,
typename std::enable_if<msgpack::has_as<K>::value && msgpack::has_as<V>::value>::type> {
typename std::enable_if<msgpack::has_as<K>::value || msgpack::has_as<V>::value>::type> {
std::unordered_map<K, V, Hash, Compare, Alloc> operator()(msgpack::object const& o) const {
if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
msgpack::object_kv* p(o.via.map.ptr);
@@ -100,7 +100,7 @@ struct object_with_zone<std::unordered_map<K, V, Hash, Compare, Alloc>> {
template <typename K, typename V, typename Hash, typename Compare, typename Alloc>
struct as<
std::unordered_multimap<K, V, Hash, Compare, Alloc>,
typename std::enable_if<msgpack::has_as<K>::value && msgpack::has_as<V>::value>::type> {
typename std::enable_if<msgpack::has_as<K>::value || msgpack::has_as<V>::value>::type> {
std::unordered_multimap<K, V, Hash, Compare, Alloc> operator()(msgpack::object const& o) const {
if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
msgpack::object_kv* p(o.via.map.ptr);

View File

@@ -146,7 +146,7 @@ struct MsgpackTupleConverter<Tuple, 0> {
namespace adaptor {
template <typename... Args>
struct as<msgpack::type::tuple<Args...>, typename std::enable_if<msgpack::all_of<msgpack::has_as, Args...>::value>::type> {
struct as<msgpack::type::tuple<Args...>, typename std::enable_if<msgpack::any_of<msgpack::has_as, Args...>::value>::type> {
msgpack::type::tuple<Args...> operator()(
msgpack::object const& o) const {
if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }

View File

@@ -28,7 +28,7 @@ namespace adaptor {
template <typename T1, typename T2>
struct as<std::pair<T1, T2>,
typename std::enable_if<msgpack::all_of<msgpack::has_as, T1, T2>::value>::type> {
typename std::enable_if<msgpack::any_of<msgpack::has_as, T1, T2>::value>::type> {
std::pair<T1, T2> operator()(msgpack::object const& o) const {
if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
if (o.via.array.size != 2) { throw msgpack::type_error(); }

View File

@@ -21,11 +21,17 @@ namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(v1) {
/// @endcond
namespace detail {
template<bool...values> struct all_of_imp
: std::is_same<bool_pack<values..., true>, bool_pack<true, values...>>{};
template<bool...values> struct any_of_imp {
static const bool value = !std::is_same<bool_pack<values..., false>, bool_pack<false, values...>>::value;
};
} // namespace detail
template<std::size_t... Is> struct seq {};

View File

@@ -28,11 +28,16 @@ template<bool...> struct bool_pack;
template<bool...values> struct all_of_imp;
template<bool...values> struct any_of_imp;
} // namespace detail
template<template <class> class T, class... U>
using all_of = detail::all_of_imp<T<U>::value...>;
template<template <class> class T, class... U>
using any_of = detail::any_of_imp<(T<U>::value)...>;
template<std::size_t... Is> struct seq;
template<std::size_t N, std::size_t... Is>

View File

@@ -27,10 +27,14 @@ using v1::detail::bool_pack;
using v1::detail::all_of_imp;
using v1::detail::any_of_imp;
} // namespace detail
using v1::all_of;
using v1::any_of;
using v1::seq;
using v1::gen_seq;

View File

@@ -159,6 +159,57 @@ TEST(MSGPACK_BOOST, pack_convert_no_def_con)
#endif // !defined(_MSC_VER)
struct mystruct_no_def_con_def_con {
mystruct_no_def_con_def_con() = delete;
// Constructor that have parameters corresponding to BOOST_FUSION_ADAPT_STRUCT is mandatory.
// See *1, *2, and *3
mystruct_no_def_con_def_con(
no_def_con1 i,
no_def_con2 j,
int k):
f1(std::move(i)),
f2(std::move(j)),
f3(std::move(k)) {}
no_def_con1 f1;
no_def_con2 f2;
int f3;
};
inline bool operator==(mystruct_no_def_con_def_con const& lhs, mystruct_no_def_con_def_con const& rhs) {
return lhs.f1 == rhs.f1 && lhs.f2 == rhs.f2 && lhs.f3 == rhs.f3;
}
inline bool operator!=(mystruct_no_def_con_def_con const& lhs, mystruct_no_def_con_def_con const& rhs) {
return !(lhs == rhs);
}
BOOST_FUSION_ADAPT_STRUCT(
mystruct_no_def_con_def_con,
f1, // *1
f2, // *2
f3 // *3
)
// MSVC2015's std::tuple requires default constructor during 'as' process.
// It doesn't support Expression SFINAE yet, then 'as' is fallbacked to 'convert'.
// After MSVC would support Expression SFINAE, remove this guard.
#if !defined(_MSC_VER)
TEST(MSGPACK_BOOST, pack_convert_no_def_con_def_con)
{
std::stringstream ss;
mystruct_no_def_con_def_con val1(no_def_con1(1), no_def_con2(2), 3);
msgpack::pack(ss, val1);
msgpack::object_handle oh =
msgpack::unpack(ss.str().data(), ss.str().size());
mystruct_no_def_con_def_con val2 = oh.get().as<mystruct_no_def_con_def_con>();
EXPECT_TRUE(val1 == val2);
}
#endif // !defined(_MSC_VER)
#endif // !defined(MSGPACK_USE_CPP03
#endif // defined(MSGPACK_USE_BOOST)

View File

@@ -612,6 +612,18 @@ TEST(MSGPACK_NO_DEF_CON_ASSOC_VECTOR, simple_buffer)
EXPECT_EQ(val1, val2);
}
TEST(MSGPACK_NO_DEF_CON_DEF_CON_ASSOC_VECTOR, simple_buffer)
{
msgpack::type::assoc_vector<no_def_con, int> val1 { {1, 2}, {3, 4}, {5, 6}};
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, val1);
msgpack::object_handle oh =
msgpack::unpack(sbuf.data(), sbuf.size());
msgpack::type::assoc_vector<no_def_con, int> val2
= oh.get().as<msgpack::type::assoc_vector<no_def_con, int>>();
EXPECT_EQ(val1, val2);
}
TEST(MSGPACK_NO_DEF_CON_MAP, simple_buffer)
{
std::map<no_def_con, no_def_con_composite> val1 { {1, 2}, {3, 4}, {5, 6}};
@@ -624,6 +636,18 @@ TEST(MSGPACK_NO_DEF_CON_MAP, simple_buffer)
EXPECT_EQ(val1, val2);
}
TEST(MSGPACK_NO_DEF_CON_DEF_CON_MAP, simple_buffer)
{
std::map<no_def_con, int> val1 { {1, 2}, {3, 4}, {5, 6}};
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, val1);
msgpack::object_handle oh =
msgpack::unpack(sbuf.data(), sbuf.size());
std::map<no_def_con, int> val2
= oh.get().as<std::map<no_def_con, int>>();
EXPECT_EQ(val1, val2);
}
TEST(MSGPACK_NO_DEF_CON_MULTIMAP, simple_buffer)
{
std::multimap<no_def_con, no_def_con_composite> val1 { {1, 2}, {3, 4}, {5, 6}};
@@ -636,6 +660,18 @@ TEST(MSGPACK_NO_DEF_CON_MULTIMAP, simple_buffer)
EXPECT_EQ(val1, val2);
}
TEST(MSGPACK_NO_DEF_CON_DEF_CON_MULTIMAP, simple_buffer)
{
std::multimap<no_def_con, int> val1 { {1, 2}, {3, 4}, {5, 6}};
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, val1);
msgpack::object_handle oh =
msgpack::unpack(sbuf.data(), sbuf.size());
std::multimap<no_def_con, int> val2
= oh.get().as<std::multimap<no_def_con, int>>();
EXPECT_EQ(val1, val2);
}
TEST(MSGPACK_NO_DEF_CON_DEQUE, simple_buffer)
{
std::deque<no_def_con> val1 { 1, 2, 3 };
@@ -659,6 +695,18 @@ TEST(MSGPACK_NO_DEF_CON_PAIR, simple_buffer)
EXPECT_EQ(val1, val2);
}
TEST(MSGPACK_NO_DEF_CON_DEF_CON_PAIR, simple_buffer)
{
std::pair<no_def_con, int> val1 {1, 2};
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, val1);
msgpack::object_handle oh =
msgpack::unpack(sbuf.data(), sbuf.size());
std::pair<no_def_con, int> val2
= oh.get().as<std::pair<no_def_con, int>>();
EXPECT_EQ(val1, val2);
}
// MSVC2015's std::tuple requires default constructor during 'as' process.
@@ -678,6 +726,18 @@ TEST(MSGPACK_NO_DEF_CON_TUPLE, simple_buffer)
EXPECT_EQ(val1, val2);
}
TEST(MSGPACK_NO_DEF_CON_DEF_CON_TUPLE, simple_buffer)
{
std::tuple<no_def_con, no_def_con, int> val1 {1, 2, 3};
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, val1);
msgpack::object_handle oh =
msgpack::unpack(sbuf.data(), sbuf.size());
std::tuple<no_def_con, no_def_con, int> val2
= oh.get().as<std::tuple<no_def_con, no_def_con, int>>();
EXPECT_EQ(val1, val2);
}
TEST(MSGPACK_NO_DEF_CON_MSGPACK_TUPLE, simple_buffer)
{
msgpack::type::tuple<no_def_con, no_def_con, no_def_con_composite> val1 {1, 2, 3};
@@ -690,6 +750,18 @@ TEST(MSGPACK_NO_DEF_CON_MSGPACK_TUPLE, simple_buffer)
EXPECT_EQ(val1, val2);
}
TEST(MSGPACK_NO_DEF_CON_DEF_CON_MSGPACK_TUPLE, simple_buffer)
{
msgpack::type::tuple<no_def_con, no_def_con, int> val1 {1, 2, 3};
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, val1);
msgpack::object_handle oh =
msgpack::unpack(sbuf.data(), sbuf.size());
msgpack::type::tuple<no_def_con, no_def_con, int> val2
= oh.get().as<msgpack::type::tuple<no_def_con, no_def_con, int>>();
EXPECT_EQ(val1, val2);
}
#endif // !define(_MSC_VER)
TEST(MSGPACK_NO_DEF_FORWARD_LIST, simple_buffer)
@@ -737,6 +809,18 @@ TEST(MSGPACK_NO_DEF_CON_UNORDERED_MAP, simple_buffer)
EXPECT_EQ(val1, val2);
}
TEST(MSGPACK_NO_DEF_CON_DEF_CON_UNORDERED_MAP, simple_buffer)
{
std::unordered_map<no_def_con, int> val1 { {1, 2}, {3, 4}, {5, 6}};
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, val1);
msgpack::object_handle oh =
msgpack::unpack(sbuf.data(), sbuf.size());
std::unordered_map<no_def_con, int> val2
= oh.get().as<std::unordered_map<no_def_con, int>>();
EXPECT_EQ(val1, val2);
}
TEST(MSGPACK_NO_DEF_CON_UNORDERED_MULTIMAP, simple_buffer)
{
std::unordered_multimap<no_def_con, no_def_con_composite> val1 { {1, 2}, {3, 4}, {5, 6}};
@@ -749,6 +833,18 @@ TEST(MSGPACK_NO_DEF_CON_UNORDERED_MULTIMAP, simple_buffer)
EXPECT_EQ(val1, val2);
}
TEST(MSGPACK_NO_DEF_CON_DEF_CON_UNORDERED_MULTIMAP, simple_buffer)
{
std::unordered_multimap<no_def_con, int> val1 { {1, 2}, {3, 4}, {5, 6}};
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, val1);
msgpack::object_handle oh =
msgpack::unpack(sbuf.data(), sbuf.size());
std::unordered_multimap<no_def_con, int> val2
= oh.get().as<std::unordered_multimap<no_def_con, int>>();
EXPECT_EQ(val1, val2);
}
TEST(MSGPACK_NO_DEF_CON_ARRAY, simple_buffer)
{
std::array<no_def_con, 3> val1 { { 1, 2, 3 } };