-
Notifications
You must be signed in to change notification settings - Fork 16
/
msgpack-variant.h
173 lines (154 loc) · 4.3 KB
/
msgpack-variant.h
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
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
166
167
168
169
170
171
172
173
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2014-2015 Baidu, Inc. |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
// HHVM Variant to msgpack implementation
#ifndef MSGPACK_TYPE_VARIANT_H__
#define MSGPACK_TYPE_VARIANT_H__
#include "hphp/runtime/ext/extension.h"
#include "hphp/runtime/base/array-iterator.h"
#include <msgpack.hpp>
# if HHVM_VERSION_ID < 31201
# define KindOfStaticString KindOfStaticString
# else
# define KindOfStaticString KindOfPersistentString
#endif
using namespace HPHP;
namespace msgpack {
// Unpack
inline Variant& operator>> (object o, Variant& v)
{
switch(o.type) {
case type::NIL:
v.setNull();
break;
case type::BOOLEAN:
v = o.via.boolean ? true : false;
break;
case type::POSITIVE_INTEGER:
v = (int64_t) o.via.u64;
break;
case type::NEGATIVE_INTEGER:
v = (int64_t) o.via.i64;
break;
case type::DOUBLE:
v = o.via.dec;
break;
case type::RAW: {
if (o.via.raw.size == 0) {
v = String("");
} else {
v = String((char*)o.via.raw.ptr, o.via.raw.size, CopyString);
}
break;
}
case type::ARRAY: {
Array array = Array::Create();
if(o.via.array.size != 0) {
object* p(o.via.array.ptr);
for(object* const pend(o.via.array.ptr + o.via.array.size);
p < pend; ++p) {
Variant item;
Variant& item_ref = item;
*p >> item;
array.append(item_ref);
}
}
v = array;
break;
}
case type::MAP: {
Array array = Array::Create();
if(o.via.map.size != 0) {
object_kv* p(o.via.map.ptr);
for(object_kv* const pend(o.via.map.ptr + o.via.map.size);
p < pend; ++p) {
Variant key, val;
p->key >> key;
p->val >> val;
array.set(key, val, true);
}
}
v = array;
break;
}
default:
break;
}
return v;
}
template <typename Stream>
inline packer<Stream>& pack_variant(packer<Stream>& o, const Variant& v, int depth=0)
{
if (depth > 512) {
raise_warning("To deep nesting: 512");
return o;
}
++depth;
switch (v.getType()) {
case KindOfUninit:
case KindOfNull:
o.pack_nil();
break;
case KindOfBoolean:
if (v.asBooleanVal()) {
o.pack_true();
} else {
o.pack_false();
}
break;
case KindOfInt64:
o.pack_int64(v.asInt64Val());
break;
case KindOfDouble:
o.pack_double(v.asDoubleVal());
break;
case KindOfStaticString:
case KindOfString: {
String str = v.toString();
o.pack_raw(str.size());
o.pack_raw_body(str.c_str(), str.size());
break;
}
case KindOfPersistentArray:
case KindOfArray: {
Array array = v.toArray();
if (array->isVectorData()) {
o.pack_array(array.size());
for (ArrayIter iter(array); iter; ++iter) {
pack_variant(o, iter.second(), depth);
}
} else {
o.pack_map(array.size());
for (ArrayIter iter(array); iter; ++iter) {
pack_variant(o, iter.first(), depth);
pack_variant(o, iter.second(), depth);
}
}
break;
}
default:
raise_warning("Not implemented source type");
break;
}
return o;
}
// Pack
template <typename Stream>
inline packer<Stream>& operator<< (packer<Stream>& o, const Variant& v)
{
return pack_variant(o, v);
}
} // namespace msgpack
#endif