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
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkJSON_DEFINED
#define SkJSON_DEFINED
#include "SkTypes.h"
class SkStream;
class SkString;
class SkJSON {
public:
enum Type {
kObject,
kArray,
kString,
kInt,
kFloat,
kBool,
};
class Array;
class Object {
private:
struct Slot;
public:
Object();
Object(const Object&);
~Object();
/**
* Create a new slot with the specified name and value. The name
* parameter is copied, but ownership of the Object parameter is
* transferred. The Object parameter may be null, but the name must
* not be null.
*/
void addObject(const char name[], Object* value);
/**
* Create a new slot with the specified name and value. The name
* parameter is copied, but ownership of the Array parameter is
* transferred. The Array parameter may be null, but the name must
* not be null.
*/
void addArray(const char name[], Array* value);
/**
* Create a new slot with the specified name and value. Both parameters
* are copied. The value parameter may be null, but the name must
* not be null.
*/
void addString(const char name[], const char value[]);
/**
* Create a new slot with the specified name and value. The name
* parameter is copied, and must not be null.
*/
void addInt(const char name[], int32_t value);
/**
* Create a new slot with the specified name and value. The name
* parameter is copied, and must not be null.
*/
void addFloat(const char name[], float value);
/**
* Create a new slot with the specified name and value. The name
* parameter is copied, and must not be null.
*/
void addBool(const char name[], bool value);
/**
* Return the number of slots/fields in this object. These can be
* iterated using Iter.
*/
int count() const;
/**
* Returns true if a slot matching the name and Type is found.
*/
bool find(const char name[], Type) const;
bool findObject(const char name[], Object** = NULL) const;
bool findArray(const char name[], Array** = NULL) const;
bool findString(const char name[], SkString* = NULL) const;
bool findInt(const char name[], int32_t* = NULL) const;
bool findFloat(const char name[], float* = NULL) const;
bool findBool(const char name[], bool* = NULL) const;
/**
* Finds the first slot matching the name and Type and removes it.
* Returns true if found, false if not.
*/
bool remove(const char name[], Type);
void toDebugf() const;
/**
* Iterator class which returns all of the fields/slots in an Object,
* in the order that they were added.
*/
class Iter {
public:
Iter(const Object&);
/**
* Returns true when there are no more entries in the iterator.
* In this case, no other methods should be called.
*/
bool done() const;
/**
* Moves the iterator to the next element. Should only be called
* if done() returns false.
*/
void next();
/**
* Returns the type of the current element. Should only be called
* if done() returns false.
*/
Type type() const;
/**
* Returns the name of the current element. Should only be called
* if done() returns false.
*/
const char* name() const;
/**
* Returns the type of the current element. Should only be called
* if done() returns false and type() returns kObject.
*/
Object* objectValue() const;
/**
* Returns the type of the current element. Should only be called
* if done() returns false and type() returns kArray.
*/
Array* arrayValue() const;
/**
* Returns the type of the current element. Should only be called
* if done() returns false and type() returns kString.
*/
const char* stringValue() const;
/**
* Returns the type of the current element. Should only be called
* if done() returns false and type() returns kInt.
*/
int32_t intValue() const;
/**
* Returns the type of the current element. Should only be called
* if done() returns false and type() returns kFloat.
*/
float floatValue() const;
/**
* Returns the type of the current element. Should only be called
* if done() returns false and type() returns kBool.
*/
bool boolValue() const;
private:
Slot* fSlot;
};
private:
Slot* fHead;
Slot* fTail;
const Slot* findSlot(const char name[], Type) const;
Slot* addSlot(Slot*);
void dumpLevel(int level) const;
friend class Array;
};
class Array {
public:
/**
* Creates an array with the specified Type and element count. All
* entries are initialized to NULL/0/false.
*/
Array(Type, int count);
/**
* Creates an array of ints, initialized by copying the specified
* values.
*/
Array(const int32_t values[], int count);
/**
* Creates an array of floats, initialized by copying the specified
* values.
*/
Array(const float values[], int count);
/**
* Creates an array of bools, initialized by copying the specified
* values.
*/
Array(const bool values[], int count);
Array(const Array&);
~Array();
int count() const { return fCount; }
Type type() const { return fType; }
/**
* Replace the element at the specified index with the specified
* Object (which may be null). Ownership of the Object is transferred.
* Should only be called if the Array's type is kObject.
*/
void setObject(int index, Object*);
/**
* Replace the element at the specified index with the specified
* Array (which may be null). Ownership of the Array is transferred.
* Should only be called if the Array's type is kArray.
*/
void setArray(int index, Array*);
/**
* Replace the element at the specified index with a copy of the
* specified string (which may be null). Should only be called if the
* Array's type is kString.
*/
void setString(int index, const char str[]);
Object* const* objects() const {
SkASSERT(kObject == fType);
return fArray.fObjects;
}
Array* const* arrays() const {
SkASSERT(kObject == fType);
return fArray.fArrays;
}
const char* const* strings() const {
SkASSERT(kString == fType);
return fArray.fStrings;
}
int32_t* ints() const {
SkASSERT(kInt == fType);
return fArray.fInts;
}
float* floats() const {
SkASSERT(kFloat == fType);
return fArray.fFloats;
}
bool* bools() const {
SkASSERT(kBool == fType);
return fArray.fBools;
}
private:
int fCount;
Type fType;
union {
void* fVoids;
Object** fObjects;
Array** fArrays;
char** fStrings;
int32_t* fInts;
float* fFloats;
bool* fBools;
} fArray;
void init(Type, int count, const void* src);
void dumpLevel(int level) const;
friend class Object;
};
};
#endif
|