Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adaptor: better diagnostic for converter #1058

Open
wants to merge 1 commit into
base: cpp_master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions include/msgpack/v1/adaptor/adaptor_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

#include "msgpack/v1/adaptor/adaptor_base_decl.hpp"

#include "msgpack/v1/cpp_config.hpp"


namespace msgpack {

/// @cond
Expand All @@ -21,13 +24,50 @@ MSGPACK_API_VERSION_NAMESPACE(v1) {

namespace adaptor {

// Unpack detector

#ifndef MSGPACK_USE_CPP03

namespace impl {

template < typename T >
using msgpack_unpack_t
= decltype( std::declval< T >().msgpack_unpack( std::declval< msgpack::object::implicit_type >() ) );

template < typename T, typename = void_t<> >
struct has_msgpack_unpack : std::false_type
{};

template < typename T >
struct has_msgpack_unpack< T, void_t< impl::msgpack_unpack_t< T > > > : std::true_type
{};

} // namespace impl

#endif

// Adaptor functors

#ifdef MSGPACK_USE_CPP03

template <typename T, typename Enabler>
struct convert {
msgpack::object const& operator()(msgpack::object const& o, T& v) const;
};

#else

template <typename T, typename Enabler>
struct convert;

template<typename T>
struct convert<T, typename std::enable_if<adaptor::impl::has_msgpack_unpack<T>::value>::type>
{
msgpack::object const &operator()(msgpack::object const &o, T &v) const;
};

#endif

template <typename T, typename Enabler>
struct pack {
template <typename Stream>
Expand Down
20 changes: 20 additions & 0 deletions include/msgpack/v1/cpp_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,24 @@ template<class T> struct is_pointer : detail::is_pointer_helper<typename remove_
#define MSGPACK_DEPRECATED(msg)
#endif // MSGPACK_CPP_VERSION >= 201402L


#if !defined(MSGPACK_USE_CPP03) && MSGPACK_CPP_VERSION < 201703L

namespace msgpack {

/// @cond
MSGPACK_API_VERSION_NAMESPACE(v1) {
/// @endcond

template<typename...> using void_t = void;

/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
/// @endcond

} // namespace msgpack

#endif


#endif // MSGPACK_V1_CPP_CONFIG_HPP
20 changes: 20 additions & 0 deletions include/msgpack/v1/cpp_config_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ struct is_pointer;

#include <memory>
#include <tuple>
#include <type_traits>

namespace msgpack {
/// @cond
Expand Down Expand Up @@ -124,4 +125,23 @@ MSGPACK_API_VERSION_NAMESPACE(v1) {
#define MSGPACK_HAS_INCLUDE(header) 0
#endif // defined(__has_include)

#if MSGPACK_CPP_VERSION >= 201703L

namespace msgpack {
/// @cond
MSGPACK_API_VERSION_NAMESPACE(v1) {
/// @endcond

// type_traits
using std::void_t;

/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
/// @endcond
} // namespace msgpack


#endif


#endif // MSGPACK_V1_CPP_CONFIG_DECL_HPP
7 changes: 7 additions & 0 deletions include/msgpack/v1/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,17 @@ struct packer_serializer {
} // namespace detail

// Adaptor functors' member functions definitions.
#ifdef MSGPACK_USE_CPP03
template <typename T, typename Enabler>
inline
msgpack::object const&
adaptor::convert<T, Enabler>::operator()(msgpack::object const& o, T& v) const {
#else
template <typename T>
inline
msgpack::object const&
adaptor::convert<T, typename std::enable_if<adaptor::impl::has_msgpack_unpack<T>::value>::type>::operator()(msgpack::object const& o, T& v) const {
#endif
v.msgpack_unpack(o.convert());
return o;
}
Expand Down
15 changes: 15 additions & 0 deletions test/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,18 @@ BOOST_AUTO_TEST_CASE(array_unsigned_char)
}

#endif // MSGPACK_CPP_VERSION >= 201703

#ifndef MSGPACK_USE_CPP03

struct struct_without_adaptor
{
int foo;
};

BOOST_AUTO_TEST_CASE(has_msgpack_unpack)
{
static_assert(msgpack::v1::adaptor::impl::has_msgpack_unpack<struct_without_adaptor>::value == false, "");
static_assert(msgpack::v1::adaptor::impl::has_msgpack_unpack<myclass>::value, "");
}

#endif