cpp11_define_map.hpp
Go to the documentation of this file.
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2008-2016 FURUHASHI Sadayuki and 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_DEFINE_MAP_HPP
#define MSGPACK_V1_CPP11_DEFINE_MAP_HPP
#include "msgpack/v1/adaptor/detail/cpp11_define_map_decl.hpp"
#include <tuple>
#include <map>
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(v1) {
namespace type {
template <typename Tuple, std::size_t N>
struct define_map_imp {
template <typename Packer>
static void pack(Packer& pk, Tuple const& t) {
define_map_imp<Tuple, N-1>::pack(pk, t);
pk.pack(std::get<N-1>(t));
}
static void unpack(
msgpack::object const& o, Tuple const& t,
std::map<std::string, msgpack::object const*> const& kvmap) {
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));
}
}
static void object(msgpack::object* o, msgpack::zone& z, Tuple const& t) {
define_map_imp<Tuple, N-2>::object(o, z, t);
o->via.map.ptr[(N-1)/2].key = msgpack::object(std::get<N-2>(t), z);
o->via.map.ptr[(N-1)/2].val = msgpack::object(std::get<N-1>(t), z);
}
};
template <typename Tuple>
struct define_map_imp<Tuple, 0> {
template <typename Packer>
static void pack(Packer&, Tuple const&) {}
static void unpack(
msgpack::object const&, Tuple const&,
std::map<std::string, msgpack::object const*> const&) {}
static void object(msgpack::object*, msgpack::zone&, Tuple const&) {}
};
template <typename... Args>
struct define_map {
define_map(Args&... args) :
a(args...) {}
template <typename Packer>
void msgpack_pack(Packer& pk) const
{
static_assert(sizeof...(Args) % 2 == 0, "");
pk.pack_map(sizeof...(Args) / 2);
define_map_imp<std::tuple<Args&...>, sizeof...(Args)>::pack(pk, a);
}
void msgpack_unpack(msgpack::object const& o) const
{
if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
std::map<std::string, msgpack::object const*> kvmap;
for (uint32_t i = 0; i < o.via.map.size; ++i) {
kvmap.emplace(
std::string(
o.via.map.ptr[i].key.via.str.ptr,
o.via.map.ptr[i].key.via.str.size),
&o.via.map.ptr[i].val);
}
define_map_imp<std::tuple<Args&...>, sizeof...(Args)>::unpack(o, a, kvmap);
}
void msgpack_object(msgpack::object* o, msgpack::zone& z) const
{
static_assert(sizeof...(Args) % 2 == 0, "");
o->type = msgpack::type::MAP;
o->via.map.ptr = static_cast<msgpack::object_kv*>(z.allocate_align(sizeof(msgpack::object_kv)*sizeof...(Args)/2, MSGPACK_ZONE_ALIGNOF(msgpack::object_kv)));
o->via.map.size = sizeof...(Args) / 2;
define_map_imp<std::tuple<Args&...>, sizeof...(Args)>::object(o, z, a);
}
std::tuple<Args&...> a;
};
template <typename... Args>
inline define_map<Args...> make_define_map(Args&... args)
{
return define_map<Args...>(args...);
}
} // namespace type
} // MSGPACK_API_VERSION_NAMESPACE(v1)
} // namespace msgpack
#endif // MSGPACK_V1_CPP11_DEFINE_MAP_HPP
msgpack::type::define_map_imp
Definition: cpp11_define_map.hpp:25
msgpack::object_map::ptr
msgpack::object_kv * ptr
Definition: object_fwd.hpp:29
msgpack::type::define_map_imp< Tuple, 0 >::unpack
static void unpack(msgpack::object const &, Tuple const &, std::map< std::string, msgpack::object const *> const &)
Definition: cpp11_define_map.hpp:51
msgpack::type::define_map::a
std::tuple< Args &... > a
Definition: cpp11_define_map.hpp:92
msgpack::zone::allocate_align
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:246
msgpack::type::define_map_imp::unpack
static void unpack(msgpack::object const &o, Tuple const &t, std::map< std::string, msgpack::object const *> const &kvmap)
Definition: cpp11_define_map.hpp:31
msgpack::type::define_map::msgpack_pack
void msgpack_pack(Packer &pk) const
Definition: cpp11_define_map.hpp:62
msgpack::type::define_map_imp::object
static void object(msgpack::object *o, msgpack::zone &z, Tuple const &t)
Definition: cpp11_define_map.hpp:40
msgpack::object::via
union_type via
Definition: object_fwd.hpp:93
msgpack::type::define_map
Definition: cpp11_define_map.hpp:58
msgpack::object_kv::val
msgpack::object val
Definition: object.hpp:31
msgpack
Definition: adaptor_base.hpp:15
msgpack::object_str::ptr
const char * ptr
Definition: object_fwd.hpp:34
msgpack::zone
Definition: cpp03_zone.hpp:22
msgpack::object_kv
Definition: object.hpp:29
msgpack::type::define_map_imp< Tuple, 0 >::pack
static void pack(Packer &, Tuple const &)
Definition: cpp11_define_map.hpp:50
msgpack::type::define_map::msgpack_unpack
void msgpack_unpack(msgpack::object const &o) const
Definition: cpp11_define_map.hpp:69
msgpack::type_error
Definition: object_fwd.hpp:236
msgpack::type::define_map::msgpack_object
void msgpack_object(msgpack::object *o, msgpack::zone &z) const
Definition: cpp11_define_map.hpp:82
msgpack::object::union_type::map
msgpack::object_map map
Definition: object_fwd.hpp:86
msgpack::type::make_define_map
define_map make_define_map()
Definition: cpp03_define_map.hpp:2530
msgpack::object::union_type::str
msgpack::object_str str
Definition: object_fwd.hpp:87
msgpack::object
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
msgpack::type::define_map_imp< Tuple, 0 >::object
static void object(msgpack::object *, msgpack::zone &, Tuple const &)
Definition: cpp11_define_map.hpp:54
msgpack::object::type
msgpack::type::object_type type
Definition: object_fwd.hpp:92
MSGPACK_API_VERSION_NAMESPACE
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
msgpack::object_kv::key
msgpack::object key
Definition: object.hpp:30
cpp11_define_map_decl.hpp
msgpack::object_map::size
uint32_t size
Definition: object_fwd.hpp:28
msgpack::type::MAP
Definition: object_fwd_decl.hpp:42
msgpack::type::define_map_imp::pack
static void pack(Packer &pk, Tuple const &t)
Definition: cpp11_define_map.hpp:27
msgpack::object_str::size
uint32_t size
Definition: object_fwd.hpp:33
MSGPACK_ZONE_ALIGNOF
#define MSGPACK_ZONE_ALIGNOF(type)
Definition: cpp03_zone_decl.hpp:30
msgpack::type::define_map::define_map
define_map(Args &... args)
Definition: cpp11_define_map.hpp:59