From 5d69c22bf9fa0192697f943f13745742beb0f966 Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Mon, 29 Aug 2016 22:12:01 +0900 Subject: [PATCH] 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. --- example/cpp11/non_def_con_class.cpp | 40 -------- include/msgpack/v1/adaptor/boost/fusion.hpp | 2 +- include/msgpack/v1/adaptor/cpp11/tuple.hpp | 2 +- .../v1/adaptor/cpp11/unordered_map.hpp | 4 +- .../v1/adaptor/detail/cpp11_msgpack_tuple.hpp | 2 +- include/msgpack/v1/adaptor/pair.hpp | 2 +- include/msgpack/v1/meta.hpp | 6 ++ include/msgpack/v1/meta_decl.hpp | 5 + include/msgpack/v2/meta_decl.hpp | 4 + test/boost_fusion.cpp | 51 ++++++++++ test/msgpack_cpp11.cpp | 96 +++++++++++++++++++ 11 files changed, 168 insertions(+), 46 deletions(-) diff --git a/example/cpp11/non_def_con_class.cpp b/example/cpp11/non_def_con_class.cpp index 0bdb2da7..2f53a3a5 100644 --- a/example/cpp11/non_def_con_class.cpp +++ b/example/cpp11/non_def_con_class.cpp @@ -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().a); - - // useful keys for maps do not have as<>() method implemented: - std::cout << "has_as " << msgpack::has_as::value << std::endl; - std::cout << "has_as " << msgpack::has_as::value << std::endl; - - // BEFORE PATCH: as a result as >() is not available either. - // AFTER PATCH: as >() is available if key OR value have an as<>() implementation - std::cout << "has_as > " << msgpack::has_as >::value << std::endl; - - std::map 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()); - - std::map 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()); } diff --git a/include/msgpack/v1/adaptor/boost/fusion.hpp b/include/msgpack/v1/adaptor/boost/fusion.hpp index f76ca439..29c288ba 100644 --- a/include/msgpack/v1/adaptor/boost/fusion.hpp +++ b/include/msgpack/v1/adaptor/boost/fusion.hpp @@ -45,7 +45,7 @@ struct as< T, boost::mpl::bool_, boost::mpl::if_ < - boost::mpl::and_< + boost::mpl::or_< boost::mpl::_1, msgpack::has_as >, diff --git a/include/msgpack/v1/adaptor/cpp11/tuple.hpp b/include/msgpack/v1/adaptor/cpp11/tuple.hpp index ebc93a41..5fad147b 100644 --- a/include/msgpack/v1/adaptor/cpp11/tuple.hpp +++ b/include/msgpack/v1/adaptor/cpp11/tuple.hpp @@ -109,7 +109,7 @@ struct StdTupleConverter { namespace adaptor { template -struct as, typename std::enable_if::value>::type> { +struct as, typename std::enable_if::value>::type> { std::tuple operator()( msgpack::object const& o) const { if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } diff --git a/include/msgpack/v1/adaptor/cpp11/unordered_map.hpp b/include/msgpack/v1/adaptor/cpp11/unordered_map.hpp index e80a31a7..b4de2df5 100644 --- a/include/msgpack/v1/adaptor/cpp11/unordered_map.hpp +++ b/include/msgpack/v1/adaptor/cpp11/unordered_map.hpp @@ -27,7 +27,7 @@ namespace adaptor { template struct as< std::unordered_map, - typename std::enable_if::value && msgpack::has_as::value>::type> { + typename std::enable_if::value || msgpack::has_as::value>::type> { std::unordered_map 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> { template struct as< std::unordered_multimap, - typename std::enable_if::value && msgpack::has_as::value>::type> { + typename std::enable_if::value || msgpack::has_as::value>::type> { std::unordered_multimap operator()(msgpack::object const& o) const { if (o.type != msgpack::type::MAP) { throw msgpack::type_error(); } msgpack::object_kv* p(o.via.map.ptr); diff --git a/include/msgpack/v1/adaptor/detail/cpp11_msgpack_tuple.hpp b/include/msgpack/v1/adaptor/detail/cpp11_msgpack_tuple.hpp index 0b06412f..1a0a3760 100644 --- a/include/msgpack/v1/adaptor/detail/cpp11_msgpack_tuple.hpp +++ b/include/msgpack/v1/adaptor/detail/cpp11_msgpack_tuple.hpp @@ -146,7 +146,7 @@ struct MsgpackTupleConverter { namespace adaptor { template -struct as, typename std::enable_if::value>::type> { +struct as, typename std::enable_if::value>::type> { msgpack::type::tuple operator()( msgpack::object const& o) const { if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } diff --git a/include/msgpack/v1/adaptor/pair.hpp b/include/msgpack/v1/adaptor/pair.hpp index 46dbfe8d..700a93b0 100644 --- a/include/msgpack/v1/adaptor/pair.hpp +++ b/include/msgpack/v1/adaptor/pair.hpp @@ -28,7 +28,7 @@ namespace adaptor { template struct as, - typename std::enable_if::value>::type> { + typename std::enable_if::value>::type> { std::pair 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(); } diff --git a/include/msgpack/v1/meta.hpp b/include/msgpack/v1/meta.hpp index 62d42007..75ab7978 100644 --- a/include/msgpack/v1/meta.hpp +++ b/include/msgpack/v1/meta.hpp @@ -21,11 +21,17 @@ namespace msgpack { MSGPACK_API_VERSION_NAMESPACE(v1) { /// @endcond + + namespace detail { template struct all_of_imp : std::is_same, bool_pack>{}; +template struct any_of_imp { + static const bool value = !std::is_same, bool_pack>::value; +}; + } // namespace detail template struct seq {}; diff --git a/include/msgpack/v1/meta_decl.hpp b/include/msgpack/v1/meta_decl.hpp index da403830..c3254e76 100644 --- a/include/msgpack/v1/meta_decl.hpp +++ b/include/msgpack/v1/meta_decl.hpp @@ -28,11 +28,16 @@ template struct bool_pack; template struct all_of_imp; +template struct any_of_imp; + } // namespace detail template