forked from syoyo/eson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheson.h
336 lines (277 loc) · 8.65 KB
/
eson.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
Copyright (c) 2013-2015, Light Transport Entertainment Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __ESON_H__
#define __ESON_H__
#include <stdint.h>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string>
#include <map>
namespace eson {
typedef enum {
NULL_TYPE = 0,
FLOAT64_TYPE = 1,
INT64_TYPE = 2,
BOOL_TYPE = 3,
STRING_TYPE = 4,
ARRAY_TYPE = 5, // @todo
BINARY_TYPE = 6,
OBJECT_TYPE = 7,
} Type;
class Value
{
public:
typedef struct {
const uint8_t *ptr;
int64_t size;
} Binary;
typedef std::vector<Value> Array;
typedef std::map<std::string, Value> Object;
protected:
int type_; // Data type
mutable uint64_t size_; // Data size
mutable bool dirty_;
//union {
bool boolean_;
int64_t int64_;
double float64_;
std::string string_;
Binary binary_;
Array array_;
Object object_;
//};
public:
Value() : type_(NULL_TYPE), dirty_(true) {
}
explicit Value(bool b) : type_(BOOL_TYPE), dirty_(false) {
boolean_ = b;
size_ = 1;
}
explicit Value(int64_t i) : type_(INT64_TYPE), dirty_(false) {
int64_ = i;
size_ = 8;
}
explicit Value(double n) : type_(FLOAT64_TYPE), dirty_(false) {
float64_ = n;
size_ = 8;
}
explicit Value(const std::string& s) : type_(STRING_TYPE), dirty_(false) {
string_ = std::string(s);
size_ = string_.size();
}
explicit Value(const uint8_t* p, uint64_t n) : type_(BINARY_TYPE), dirty_(false) {
binary_ = Binary();
binary_.ptr = p; // Just save a pointer.
binary_.size = n;
size_ = n;
}
explicit Value(const Array& a) : type_(ARRAY_TYPE), dirty_(true) {
array_ = Array(a);
size_ = ComputeArraySize();
}
explicit Value(const Object& o) : type_(OBJECT_TYPE), dirty_(true) {
object_ = Object(o);
size_ = ComputeObjectSize();
}
~Value() {};
/// Compute size of array element.
int64_t ComputeArraySize() const {
assert(type_ == ARRAY_TYPE);
int64_t array_size = 0;
assert(array_.size() > 0);
char base_element_type = array_[0].Type();
//
// Elements in the array must be all same type.
//
int64_t sum = 0;
for (size_t i = 0; i < array_.size(); i++) {
char element_type = array_[i].Type();
assert(base_element_type == element_type);
sum += array_[i].ComputeSize();
}
return sum + 1 + sizeof(int64_t); // @todo
}
/// Compute object size.
int64_t ComputeObjectSize() const {
assert(type_ == OBJECT_TYPE);
int64_t object_size = 0;
for (Object::const_iterator it = object_.begin();
it != object_.end();
++it) {
const std::string& key = it->first;
int64_t key_len = key.length() + 1; // + '\0'
int64_t data_len = it->second.ComputeSize();
object_size += key_len + data_len + 1; // +1 = tag size.
}
return object_size;
}
/// Compute data size.
int64_t ComputeSize() const {
switch (type_) {
case INT64_TYPE:
return 8;
break;
case FLOAT64_TYPE:
return 8;
break;
case STRING_TYPE:
return string_.size() + sizeof(int64_t); // N + str data
break;
case BINARY_TYPE:
return size_ + sizeof(int64_t); // N + bin data
break;
case ARRAY_TYPE:
return ComputeArraySize() + sizeof(int64_t); // datalen + N
break;
case OBJECT_TYPE:
return ComputeObjectSize() + sizeof(int64_t); // datalen + N
break;
default:
assert(0);
break;
}
assert(0);
return -1; // Never come here.
}
int64_t Size() const {
if (!dirty_) {
return size_;
} else {
// Recompute data size.
size_ = ComputeSize();
dirty_ = false;
return size_;
}
}
const char Type() const {
return (const char)type_;
}
const bool IsBool() const {
return (type_ == BOOL_TYPE);
}
const bool IsInt64() const {
return (type_ == INT64_TYPE);
}
const bool IsFloat64() const {
return (type_ == FLOAT64_TYPE);
}
const bool IsString() const {
return (type_ == STRING_TYPE);
}
const bool IsBinary() const {
return (type_ == BINARY_TYPE);
}
const bool IsArray() const {
return (type_ == ARRAY_TYPE);
}
const bool IsObject() const {
return (type_ == OBJECT_TYPE);
}
// Accessor
template <typename T> const T& Get() const;
template <typename T> T& Get();
// Lookup value from an array
const Value& Get(int64_t idx) const {
static Value null_value;
assert(IsArray());
assert(idx >= 0);
return ((uint64_t)idx < array_.size()) ? array_[idx] : null_value;
}
// Lookup value from a key-value pair
const Value& Get(const std::string& key) const {
static Value null_value;
assert(IsObject());
Object::const_iterator it = object_.find(key);
return (it != object_.end()) ? it->second : null_value;
}
size_t ArrayLen() const {
if (!IsArray()) return 0;
return array_.size();
}
// Valid only for object type.
bool Has(const std::string& key) const {
if (!IsObject()) return false;
Object::const_iterator it = object_.find(key);
return (it != object_.end()) ? true : false;
}
// List keys
std::vector<std::string> Keys() const {
std::vector<std::string> keys;
if (!IsObject()) return keys; // empty
for (Object::const_iterator it = object_.begin();
it != object_.end();
++it) {
keys.push_back(it->first);
}
return keys;
}
// Serialize data to memory 'p'.
// Memory of 'p' must be allocated by app before calling this function.
// (size can be obtained by calling 'Size' function.
// Return next data location.
uint8_t* Serialize(uint8_t* p) const;
private:
};
// Alias
typedef Value::Array Array;
typedef Value::Object Object;
typedef Value::Binary Binary;
#define GET(ctype, var) \
template <> inline const ctype& Value::Get<ctype>() const { \
return var; \
} \
template <> inline ctype& Value::Get<ctype>() { \
return var; \
}
GET(bool, boolean_)
GET(double, float64_)
GET(int64_t, int64_)
GET(std::string, string_)
GET(Binary, binary_)
GET(Array, array_)
GET(Object, object_)
#undef GET
// Deserialize data from memory 'p'.
// Returns error string. Empty if success.
std::string Parse(Value& v, const uint8_t* p);
std::string Parse(Array& v, const uint8_t* p);
class ESON
{
public:
ESON();
~ESON();
/// Load data from a file.
bool Load(const char* filename);
/// Dump data to a file.
bool Dump(const char* filename);
private:
uint8_t* data_; /// Pointer to data
uint64_t size_; /// Total data size
bool valid_;
};
}
#endif // __ESON_H__