File: cpp03_define_map.hpp.erb

package info (click to toggle)
msgpack-cxx 7.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,520 kB
  • sloc: cpp: 87,413; ansic: 3,571; sh: 56; makefile: 39
file content (120 lines) | stat: -rw-r--r-- 3,720 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2015-2016 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_CPP03_DEFINE_MAP_HPP
#define MSGPACK_V1_CPP03_DEFINE_MAP_HPP

#include "msgpack/v1/adaptor/detail/cpp03_define_map_decl.hpp"
#include "msgpack/adaptor/msgpack_tuple.hpp"
#include "msgpack/adaptor/adaptor_base.hpp"
#include "msgpack/object_fwd.hpp"

#include <map>

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

<% GENERATION_LIMIT = 31 %>
template <>
struct define_map<> {
    template <typename Packer>
    void msgpack_pack(Packer& pk) const
    {
        pk.pack_map(0);
    }
    void msgpack_unpack(msgpack::object const& o) const
    {
        if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
    }
    void msgpack_object(msgpack::object* o, msgpack::zone&) const
    {
        o->type = msgpack::type::MAP;
        o->via.map.ptr = MSGPACK_NULLPTR;
        o->via.map.size = 0;
    }
};

/// @cond
<%1.step(GENERATION_LIMIT+1,2) {|i|%>
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
struct define_map<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
    define_map(A0& _a0<%1.upto(i) {|j|%>, A<%=j%>& _a<%=j%><%}%>) :
        a0(_a0)<%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {}
    template <typename Packer>
    void msgpack_pack(Packer& pk) const
    {
        pk.pack_map(<%=(i+1)/2%>);
        <%0.upto(i) {|j|%>
        pk.pack(a<%=j%>);<%}%>
    }
    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) {
            if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); }
            kvmap.insert(
                std::map<std::string, msgpack::object const*>::value_type(
                    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
                )
            );
        }
        <%0.step(i,2) {|j|%>
        {
            std::map<std::string, msgpack::object const*>::const_iterator it = kvmap.find(a<%=j%>);
            if (it != kvmap.end()) {
                it->second->convert(a<%=j+1%>);
            }
        }
        <%}%>
    }
    void msgpack_object(msgpack::object* o, msgpack::zone& z) const
    {
        o->type = msgpack::type::MAP;
        o->via.map.ptr = static_cast<msgpack::object_kv*>(z.allocate_align(sizeof(msgpack::object_kv)*<%=(i+1)/2%>, MSGPACK_ZONE_ALIGNOF(msgpack::object_kv)));
        o->via.map.size = <%=(i+1)/2%>;
        <%0.step(i,2) {|j|%>
        o->via.map.ptr[<%=j/2%>].key = msgpack::object(a<%=j%>, z);
        o->via.map.ptr[<%=j/2%>].val = msgpack::object(a<%=j+1%>, z);
        <%}%>
    }
    <%0.upto(i) {|j|%>
    A<%=j%>& a<%=j%>;<%}%>
};
<%}%>
/// @endcond

inline define_map<> make_define_map()
{
    return define_map<>();
}

/// @cond
<%0.upto(GENERATION_LIMIT) {|i|%>
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
inline define_map<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_define_map(A0& a0<%1.upto(i) {|j|%>, A<%=j%>& a<%=j%><%}%>)
{
    return define_map<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
}
<%}%>
/// @endcond

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

#endif // MSGPACK_V1_CPP03_DEFINE_MAP_HPP