Added `as()` checking to MSGPACK_DEFINE family.
This commit is contained in:
Takatoshi Kondo 2017-11-11 15:44:31 +09:00
parent d1eac600e9
commit b4db293181
4 changed files with 84 additions and 3 deletions

View File

@ -541,6 +541,7 @@ IF (MSGPACK_ENABLE_CXX)
include/msgpack/v1/adaptor/detail/cpp03_define_map_decl.hpp
include/msgpack/v1/adaptor/detail/cpp03_msgpack_tuple.hpp
include/msgpack/v1/adaptor/detail/cpp03_msgpack_tuple_decl.hpp
include/msgpack/v1/adaptor/detail/cpp11_convert_helper.hpp
include/msgpack/v1/adaptor/detail/cpp11_define_array.hpp
include/msgpack/v1/adaptor/detail/cpp11_define_array_decl.hpp
include/msgpack/v1/adaptor/detail/cpp11_define_map.hpp

View File

@ -0,0 +1,45 @@
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2017 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef MSGPACK_V1_CPP11_CONVERT_HELPER_HPP
#define MSGPACK_V1_CPP11_CONVERT_HELPER_HPP
#include <tuple>
#include <msgpack/object_fwd.hpp>
namespace msgpack {
/// @cond
MSGPACK_API_VERSION_NAMESPACE(v1) {
/// @endcond
namespace type {
template <typename T>
inline typename std::enable_if<
has_as<T>::value
>::type
convert_helper(msgpack::object const& o, T& t) {
t = o.as<T>();
}
template <typename T>
inline typename std::enable_if<
!has_as<T>::value
>::type
convert_helper(msgpack::object const& o, T& t) {
o.convert(t);
}
} // namespace type
/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
/// @endcond
} // namespace msgpack
#endif // MSGPACK_V1_CPP11_CONVERT_HELPER_HPP

View File

@ -11,6 +11,7 @@
#define MSGPACK_V1_CPP11_DEFINE_ARRAY_HPP
#include "msgpack/v1/adaptor/detail/cpp11_define_array_decl.hpp"
#include "msgpack/v1/adaptor/detail/cpp11_convert_helper.hpp"
#include <tuple>
@ -20,6 +21,39 @@ MSGPACK_API_VERSION_NAMESPACE(v1) {
/// @endcond
namespace type {
namespace detail {
template <int N, typename... Ts>
struct get;
template <int N, typename T, typename... Ts>
struct get<N, std::tuple<T, Ts...>>
{
using type = typename get<N - 1, std::tuple<Ts...>>::type;
};
template <typename T, typename... Ts>
struct get<0, std::tuple<T, Ts...>>
{
using type = T;
};
template <typename T>
inline typename std::enable_if<
has_as<T>::value
>::type
unpack_impl(msgpack::object const& o, T& t) {
t = o.as<T>();
}
template <typename T>
inline typename std::enable_if<
!has_as<T>::value
>::type
unpack_impl(msgpack::object const& o, T& t) {
o.convert(t);
}
} // namespace detail
template <typename Tuple, std::size_t N>
struct define_array_imp {
template <typename Packer>
@ -31,7 +65,7 @@ struct define_array_imp {
define_array_imp<Tuple, N-1>::unpack(o, t);
const size_t size = o.via.array.size;
if(size <= N-1) { return; }
o.via.array.ptr[N-1].convert(std::get<N-1>(t));
convert_helper(o.via.array.ptr[N-1], std::get<N-1>(t));
}
static void object(msgpack::object* o, msgpack::zone& z, Tuple const& t) {
define_array_imp<Tuple, N-1>::object(o, z, t);
@ -48,7 +82,7 @@ struct define_array_imp<Tuple, 1> {
static void unpack(msgpack::object const& o, Tuple& t) {
const size_t size = o.via.array.size;
if(size <= 0) { return; }
o.via.array.ptr[0].convert(std::get<0>(t));
convert_helper(o.via.array.ptr[0], std::get<0>(t));
}
static void object(msgpack::object* o, msgpack::zone& z, Tuple const& t) {
o->via.array.ptr[0] = msgpack::object(std::get<0>(t), z);

View File

@ -11,6 +11,7 @@
#define MSGPACK_V1_CPP11_DEFINE_MAP_HPP
#include "msgpack/v1/adaptor/detail/cpp11_define_map_decl.hpp"
#include "msgpack/v1/adaptor/detail/cpp11_convert_helper.hpp"
#include <tuple>
#include <map>
@ -34,7 +35,7 @@ struct define_map_imp {
define_map_imp<Tuple, N-2>::unpack(o, t, kvmap);
auto it = kvmap.find(std::get<N-2>(t));
if (it != kvmap.end()) {
it->second->convert(std::get<N-1>(t));
convert_helper(*it->second, std::get<N-1>(t));
}
}
static void object(msgpack::object* o, msgpack::zone& z, Tuple const& t) {