Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2011 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef SkJSON_DEFINED |
michael@0 | 9 | #define SkJSON_DEFINED |
michael@0 | 10 | |
michael@0 | 11 | #include "SkTypes.h" |
michael@0 | 12 | |
michael@0 | 13 | class SkStream; |
michael@0 | 14 | class SkString; |
michael@0 | 15 | |
michael@0 | 16 | class SkJSON { |
michael@0 | 17 | public: |
michael@0 | 18 | enum Type { |
michael@0 | 19 | kObject, |
michael@0 | 20 | kArray, |
michael@0 | 21 | kString, |
michael@0 | 22 | kInt, |
michael@0 | 23 | kFloat, |
michael@0 | 24 | kBool, |
michael@0 | 25 | }; |
michael@0 | 26 | |
michael@0 | 27 | class Array; |
michael@0 | 28 | |
michael@0 | 29 | class Object { |
michael@0 | 30 | private: |
michael@0 | 31 | struct Slot; |
michael@0 | 32 | |
michael@0 | 33 | public: |
michael@0 | 34 | Object(); |
michael@0 | 35 | Object(const Object&); |
michael@0 | 36 | ~Object(); |
michael@0 | 37 | |
michael@0 | 38 | /** |
michael@0 | 39 | * Create a new slot with the specified name and value. The name |
michael@0 | 40 | * parameter is copied, but ownership of the Object parameter is |
michael@0 | 41 | * transferred. The Object parameter may be null, but the name must |
michael@0 | 42 | * not be null. |
michael@0 | 43 | */ |
michael@0 | 44 | void addObject(const char name[], Object* value); |
michael@0 | 45 | |
michael@0 | 46 | /** |
michael@0 | 47 | * Create a new slot with the specified name and value. The name |
michael@0 | 48 | * parameter is copied, but ownership of the Array parameter is |
michael@0 | 49 | * transferred. The Array parameter may be null, but the name must |
michael@0 | 50 | * not be null. |
michael@0 | 51 | */ |
michael@0 | 52 | void addArray(const char name[], Array* value); |
michael@0 | 53 | |
michael@0 | 54 | /** |
michael@0 | 55 | * Create a new slot with the specified name and value. Both parameters |
michael@0 | 56 | * are copied. The value parameter may be null, but the name must |
michael@0 | 57 | * not be null. |
michael@0 | 58 | */ |
michael@0 | 59 | void addString(const char name[], const char value[]); |
michael@0 | 60 | |
michael@0 | 61 | /** |
michael@0 | 62 | * Create a new slot with the specified name and value. The name |
michael@0 | 63 | * parameter is copied, and must not be null. |
michael@0 | 64 | */ |
michael@0 | 65 | void addInt(const char name[], int32_t value); |
michael@0 | 66 | |
michael@0 | 67 | /** |
michael@0 | 68 | * Create a new slot with the specified name and value. The name |
michael@0 | 69 | * parameter is copied, and must not be null. |
michael@0 | 70 | */ |
michael@0 | 71 | void addFloat(const char name[], float value); |
michael@0 | 72 | |
michael@0 | 73 | /** |
michael@0 | 74 | * Create a new slot with the specified name and value. The name |
michael@0 | 75 | * parameter is copied, and must not be null. |
michael@0 | 76 | */ |
michael@0 | 77 | void addBool(const char name[], bool value); |
michael@0 | 78 | |
michael@0 | 79 | /** |
michael@0 | 80 | * Return the number of slots/fields in this object. These can be |
michael@0 | 81 | * iterated using Iter. |
michael@0 | 82 | */ |
michael@0 | 83 | int count() const; |
michael@0 | 84 | |
michael@0 | 85 | /** |
michael@0 | 86 | * Returns true if a slot matching the name and Type is found. |
michael@0 | 87 | */ |
michael@0 | 88 | bool find(const char name[], Type) const; |
michael@0 | 89 | bool findObject(const char name[], Object** = NULL) const; |
michael@0 | 90 | bool findArray(const char name[], Array** = NULL) const; |
michael@0 | 91 | bool findString(const char name[], SkString* = NULL) const; |
michael@0 | 92 | bool findInt(const char name[], int32_t* = NULL) const; |
michael@0 | 93 | bool findFloat(const char name[], float* = NULL) const; |
michael@0 | 94 | bool findBool(const char name[], bool* = NULL) const; |
michael@0 | 95 | |
michael@0 | 96 | /** |
michael@0 | 97 | * Finds the first slot matching the name and Type and removes it. |
michael@0 | 98 | * Returns true if found, false if not. |
michael@0 | 99 | */ |
michael@0 | 100 | bool remove(const char name[], Type); |
michael@0 | 101 | |
michael@0 | 102 | void toDebugf() const; |
michael@0 | 103 | |
michael@0 | 104 | /** |
michael@0 | 105 | * Iterator class which returns all of the fields/slots in an Object, |
michael@0 | 106 | * in the order that they were added. |
michael@0 | 107 | */ |
michael@0 | 108 | class Iter { |
michael@0 | 109 | public: |
michael@0 | 110 | Iter(const Object&); |
michael@0 | 111 | |
michael@0 | 112 | /** |
michael@0 | 113 | * Returns true when there are no more entries in the iterator. |
michael@0 | 114 | * In this case, no other methods should be called. |
michael@0 | 115 | */ |
michael@0 | 116 | bool done() const; |
michael@0 | 117 | |
michael@0 | 118 | /** |
michael@0 | 119 | * Moves the iterator to the next element. Should only be called |
michael@0 | 120 | * if done() returns false. |
michael@0 | 121 | */ |
michael@0 | 122 | void next(); |
michael@0 | 123 | |
michael@0 | 124 | /** |
michael@0 | 125 | * Returns the type of the current element. Should only be called |
michael@0 | 126 | * if done() returns false. |
michael@0 | 127 | */ |
michael@0 | 128 | Type type() const; |
michael@0 | 129 | |
michael@0 | 130 | /** |
michael@0 | 131 | * Returns the name of the current element. Should only be called |
michael@0 | 132 | * if done() returns false. |
michael@0 | 133 | */ |
michael@0 | 134 | const char* name() const; |
michael@0 | 135 | |
michael@0 | 136 | /** |
michael@0 | 137 | * Returns the type of the current element. Should only be called |
michael@0 | 138 | * if done() returns false and type() returns kObject. |
michael@0 | 139 | */ |
michael@0 | 140 | Object* objectValue() const; |
michael@0 | 141 | |
michael@0 | 142 | /** |
michael@0 | 143 | * Returns the type of the current element. Should only be called |
michael@0 | 144 | * if done() returns false and type() returns kArray. |
michael@0 | 145 | */ |
michael@0 | 146 | Array* arrayValue() const; |
michael@0 | 147 | |
michael@0 | 148 | /** |
michael@0 | 149 | * Returns the type of the current element. Should only be called |
michael@0 | 150 | * if done() returns false and type() returns kString. |
michael@0 | 151 | */ |
michael@0 | 152 | const char* stringValue() const; |
michael@0 | 153 | |
michael@0 | 154 | /** |
michael@0 | 155 | * Returns the type of the current element. Should only be called |
michael@0 | 156 | * if done() returns false and type() returns kInt. |
michael@0 | 157 | */ |
michael@0 | 158 | int32_t intValue() const; |
michael@0 | 159 | |
michael@0 | 160 | /** |
michael@0 | 161 | * Returns the type of the current element. Should only be called |
michael@0 | 162 | * if done() returns false and type() returns kFloat. |
michael@0 | 163 | */ |
michael@0 | 164 | float floatValue() const; |
michael@0 | 165 | |
michael@0 | 166 | /** |
michael@0 | 167 | * Returns the type of the current element. Should only be called |
michael@0 | 168 | * if done() returns false and type() returns kBool. |
michael@0 | 169 | */ |
michael@0 | 170 | bool boolValue() const; |
michael@0 | 171 | |
michael@0 | 172 | private: |
michael@0 | 173 | Slot* fSlot; |
michael@0 | 174 | }; |
michael@0 | 175 | |
michael@0 | 176 | private: |
michael@0 | 177 | Slot* fHead; |
michael@0 | 178 | Slot* fTail; |
michael@0 | 179 | |
michael@0 | 180 | const Slot* findSlot(const char name[], Type) const; |
michael@0 | 181 | Slot* addSlot(Slot*); |
michael@0 | 182 | void dumpLevel(int level) const; |
michael@0 | 183 | |
michael@0 | 184 | friend class Array; |
michael@0 | 185 | }; |
michael@0 | 186 | |
michael@0 | 187 | class Array { |
michael@0 | 188 | public: |
michael@0 | 189 | /** |
michael@0 | 190 | * Creates an array with the specified Type and element count. All |
michael@0 | 191 | * entries are initialized to NULL/0/false. |
michael@0 | 192 | */ |
michael@0 | 193 | Array(Type, int count); |
michael@0 | 194 | |
michael@0 | 195 | /** |
michael@0 | 196 | * Creates an array of ints, initialized by copying the specified |
michael@0 | 197 | * values. |
michael@0 | 198 | */ |
michael@0 | 199 | Array(const int32_t values[], int count); |
michael@0 | 200 | |
michael@0 | 201 | /** |
michael@0 | 202 | * Creates an array of floats, initialized by copying the specified |
michael@0 | 203 | * values. |
michael@0 | 204 | */ |
michael@0 | 205 | Array(const float values[], int count); |
michael@0 | 206 | |
michael@0 | 207 | /** |
michael@0 | 208 | * Creates an array of bools, initialized by copying the specified |
michael@0 | 209 | * values. |
michael@0 | 210 | */ |
michael@0 | 211 | Array(const bool values[], int count); |
michael@0 | 212 | |
michael@0 | 213 | Array(const Array&); |
michael@0 | 214 | ~Array(); |
michael@0 | 215 | |
michael@0 | 216 | int count() const { return fCount; } |
michael@0 | 217 | Type type() const { return fType; } |
michael@0 | 218 | |
michael@0 | 219 | /** |
michael@0 | 220 | * Replace the element at the specified index with the specified |
michael@0 | 221 | * Object (which may be null). Ownership of the Object is transferred. |
michael@0 | 222 | * Should only be called if the Array's type is kObject. |
michael@0 | 223 | */ |
michael@0 | 224 | void setObject(int index, Object*); |
michael@0 | 225 | |
michael@0 | 226 | /** |
michael@0 | 227 | * Replace the element at the specified index with the specified |
michael@0 | 228 | * Array (which may be null). Ownership of the Array is transferred. |
michael@0 | 229 | * Should only be called if the Array's type is kArray. |
michael@0 | 230 | */ |
michael@0 | 231 | void setArray(int index, Array*); |
michael@0 | 232 | |
michael@0 | 233 | /** |
michael@0 | 234 | * Replace the element at the specified index with a copy of the |
michael@0 | 235 | * specified string (which may be null). Should only be called if the |
michael@0 | 236 | * Array's type is kString. |
michael@0 | 237 | */ |
michael@0 | 238 | void setString(int index, const char str[]); |
michael@0 | 239 | |
michael@0 | 240 | Object* const* objects() const { |
michael@0 | 241 | SkASSERT(kObject == fType); |
michael@0 | 242 | return fArray.fObjects; |
michael@0 | 243 | } |
michael@0 | 244 | Array* const* arrays() const { |
michael@0 | 245 | SkASSERT(kObject == fType); |
michael@0 | 246 | return fArray.fArrays; |
michael@0 | 247 | } |
michael@0 | 248 | const char* const* strings() const { |
michael@0 | 249 | SkASSERT(kString == fType); |
michael@0 | 250 | return fArray.fStrings; |
michael@0 | 251 | } |
michael@0 | 252 | int32_t* ints() const { |
michael@0 | 253 | SkASSERT(kInt == fType); |
michael@0 | 254 | return fArray.fInts; |
michael@0 | 255 | } |
michael@0 | 256 | float* floats() const { |
michael@0 | 257 | SkASSERT(kFloat == fType); |
michael@0 | 258 | return fArray.fFloats; |
michael@0 | 259 | } |
michael@0 | 260 | bool* bools() const { |
michael@0 | 261 | SkASSERT(kBool == fType); |
michael@0 | 262 | return fArray.fBools; |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | private: |
michael@0 | 266 | int fCount; |
michael@0 | 267 | Type fType; |
michael@0 | 268 | union { |
michael@0 | 269 | void* fVoids; |
michael@0 | 270 | Object** fObjects; |
michael@0 | 271 | Array** fArrays; |
michael@0 | 272 | char** fStrings; |
michael@0 | 273 | int32_t* fInts; |
michael@0 | 274 | float* fFloats; |
michael@0 | 275 | bool* fBools; |
michael@0 | 276 | } fArray; |
michael@0 | 277 | |
michael@0 | 278 | void init(Type, int count, const void* src); |
michael@0 | 279 | void dumpLevel(int level) const; |
michael@0 | 280 | |
michael@0 | 281 | friend class Object; |
michael@0 | 282 | }; |
michael@0 | 283 | }; |
michael@0 | 284 | |
michael@0 | 285 | #endif |