msgpack.cpp 4.88 KB
Newer Older
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
#include <migraphx/msgpack.hpp>
#include <migraphx/serialize.hpp>
#include <msgpack.hpp>

namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
{
    namespace adaptor {

    template <>
    struct convert<migraphx::value>
    {
        const msgpack::object& operator()(const msgpack::object& o, migraphx::value& v) const
        {
            switch(o.type)
            {
            case msgpack::type::NIL:
            {
                v = nullptr;
                break;
            }
            case msgpack::type::BOOLEAN:
            {
                v = o.as<bool>();
                break;
            }
            case msgpack::type::POSITIVE_INTEGER:
            {
                v = o.as<std::uint64_t>();
                break;
            }
            case msgpack::type::NEGATIVE_INTEGER:
            {
                v = o.as<std::int64_t>();
                break;
            }
            case msgpack::type::FLOAT32:
            case msgpack::type::FLOAT64:
            {
                v = o.as<double>();
                break;
            }
            case msgpack::type::STR:
            {
                v = o.as<std::string>();
                break;
            }
48
49
50
51
            case msgpack::type::BIN:
            {
                v = migraphx::value::binary{o.via.bin.ptr, o.via.bin.size};
                break;
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
            }
            case msgpack::type::ARRAY:
            {
                migraphx::value r = migraphx::value::array{};
                std::for_each(
                    o.via.array.ptr,
                    o.via.array.ptr + o.via.array.size,
                    [&](const msgpack::object& so) { r.push_back(so.as<migraphx::value>()); });
                v = r;
                break;
            }
            case msgpack::type::MAP:
            {
                migraphx::value r = migraphx::value::object{};
                std::for_each(o.via.map.ptr,
                              o.via.map.ptr + o.via.map.size,
                              [&](const msgpack::object_kv& p) {
                                  r[p.key.as<std::string>()] = p.val.as<migraphx::value>();
                              });
                v = r;
                break;
            }
            case msgpack::type::EXT: { MIGRAPHX_THROW("msgpack EXT type not supported.");
            }
            }
            return o;
        }
    };

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    template <>
    struct pack<migraphx::value::binary>
    {
        template <class Stream>
        packer<Stream>& operator()(msgpack::packer<Stream>& o,
                                   const migraphx::value::binary& x) const
        {
            const auto* data = reinterpret_cast<const char*>(x.data());
            auto size        = x.size();
            o.pack_bin(size);
            o.pack_bin_body(data, size);
            return o;
        }
    };

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    template <>
    struct pack<migraphx::value>
    {
        template <class Stream>
        void write(msgpack::packer<Stream>& o, const std::nullptr_t&) const
        {
            o.pack_nil();
        }
        template <class Stream, class T>
        void write(msgpack::packer<Stream>& o, const T& x) const
        {
            o.pack(x);
        }
        template <class Stream>
        void write(msgpack::packer<Stream>& o, const std::vector<migraphx::value>& v) const
        {
            if(v.empty())
            {
                o.pack_array(0);
                return;
            }
            if(not v.front().get_key().empty())
            {
                o.pack_map(v.size());
                for(auto&& x : v)
                {
                    o.pack(x.get_key());
                    o.pack(x.without_key());
                }
            }
            else
            {
                o.pack_array(v.size());
                for(auto&& x : v)
                {
                    o.pack(x);
                }
            }
        }
        template <class Stream>
        packer<Stream>& operator()(msgpack::packer<Stream>& o, const migraphx::value& v) const
        {
            v.visit([&](auto&& x) { this->write(o, x); });
            return o;
        }
    };

    } // namespace adaptor
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
} // namespace msgpack

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

struct vector_stream
{
    std::vector<char> buffer{};
    vector_stream& write(const char* b, std::size_t n)
    {
        buffer.insert(buffer.end(), b, b + n);
        return *this;
    }
};

std::vector<char> to_msgpack(const value& v)
{
    vector_stream vs;
    msgpack::pack(vs, v);
    return vs.buffer;
}
166
value from_msgpack(const char* buffer, std::size_t size)
167
{
168
    msgpack::object_handle oh = msgpack::unpack(buffer, size);
169
170
    return oh.get().as<value>();
}
171
172
173
174
value from_msgpack(const std::vector<char>& buffer)
{
    return from_msgpack(buffer.data(), buffer.size());
}
175
176
177

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx