Added 'as' support for containers.

This commit is contained in:
Takatoshi Kondo
2015-07-28 17:16:08 +09:00
parent 87f0da1ff1
commit a746afa7cc
18 changed files with 680 additions and 85 deletions

View File

@@ -1,7 +1,7 @@
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2008-2014 FURUHASHI Sadayuki and KONDO Takatoshi
// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
#include "msgpack/versioning.hpp"
#include "msgpack/object_fwd.hpp"
#include "msgpack/meta.hpp"
#include <tuple>
@@ -36,9 +37,7 @@ namespace type {
using std::tuple_element;
using std::uses_allocator;
using std::ignore;
using std::make_tuple;
using std::tie;
using std::forward_as_tuple;
using std::swap;
template< class... Types >
@@ -84,8 +83,18 @@ namespace type {
get() && { return std::get<I>(*this); }
};
template< class... Tuples >
auto tuple_cat(Tuples&&... args) ->
template <class... Args>
inline tuple<Args...> make_tuple(Args&&... args) {
return tuple<Args...>(args...);
}
template<class... Args>
inline tuple<Args&&...> forward_as_tuple (Args&&... args) noexcept {
return tuple<Args&&...>(std::forward<Args>(args)...);
}
template <class... Tuples>
inline auto tuple_cat(Tuples&&... args) ->
decltype(
std::tuple_cat(std::forward<typename std::remove_reference<Tuples>::type::base>(args)...)
) {
@@ -124,11 +133,11 @@ struct MsgpackTuplePacker<Stream, Tuple, 0> {
namespace adaptor {
template <typename... Args>
struct pack<type::tuple<Args...>> {
struct pack<msgpack::type::tuple<Args...>> {
template <typename Stream>
msgpack::packer<Stream>& operator()(
msgpack::packer<Stream>& o,
const type::tuple<Args...>& v) const {
const msgpack::type::tuple<Args...>& v) const {
o.pack_array(sizeof...(Args));
MsgpackTuplePacker<Stream, decltype(v), sizeof...(Args)>::pack(o, v);
return o;
@@ -139,6 +148,32 @@ struct pack<type::tuple<Args...>> {
// --- Convert from tuple to object ---
template <typename... Args>
struct MsgpackTupleAs;
template <typename T, typename... Args>
struct MsgpackTupleAsImpl {
static msgpack::type::tuple<T, Args...> as(msgpack::object const& o) {
return msgpack::type::tuple_cat(
msgpack::type::make_tuple(o.via.array.ptr[o.via.array.size - sizeof...(Args) - 1].as<T>()),
MsgpackTupleAs<Args...>::as(o));
}
};
template <typename... Args>
struct MsgpackTupleAs {
static msgpack::type::tuple<Args...> as(msgpack::object const& o) {
return MsgpackTupleAsImpl<Args...>::as(o);
}
};
template <>
struct MsgpackTupleAs<> {
static msgpack::type::tuple<> as (msgpack::object const&) {
return msgpack::type::tuple<>();
}
};
template <typename Tuple, std::size_t N>
struct MsgpackTupleConverter {
static void convert(
@@ -169,10 +204,20 @@ struct MsgpackTupleConverter<Tuple, 0> {
namespace adaptor {
template <typename... Args>
struct convert<type::tuple<Args...>> {
struct as<msgpack::type::tuple<Args...>, typename std::enable_if<msgpack::all_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(); }
if (o.via.array.size < sizeof...(Args)) { throw msgpack::type_error(); }
return MsgpackTupleAs<Args...>::as(o);
}
};
template <typename... Args>
struct convert<msgpack::type::tuple<Args...>> {
msgpack::object const& operator()(
msgpack::object const& o,
type::tuple<Args...>& v) const {
msgpack::type::tuple<Args...>& v) const {
if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
if(o.via.array.size < sizeof...(Args)) { throw msgpack::type_error(); }
MsgpackTupleConverter<decltype(v), sizeof...(Args)>::convert(o, v);
@@ -213,10 +258,10 @@ struct MsgpackTupleToObjectWithZone<Tuple, 0> {
namespace adaptor {
template <typename... Args>
struct object_with_zone<type::tuple<Args...>> {
struct object_with_zone<msgpack::type::tuple<Args...>> {
void operator()(
msgpack::object::with_zone& o,
type::tuple<Args...> const& v) const {
msgpack::type::tuple<Args...> const& v) const {
o.type = msgpack::type::ARRAY;
o.via.array.ptr = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*sizeof...(Args)));
o.via.array.size = sizeof...(Args);