mike@reedtribe.org | 0f175a6 | 2012-01-02 00:34:50 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkJSON_DEFINED |
| 9 | #define SkJSON_DEFINED |
| 10 | |
| 11 | #include "SkTypes.h" |
| 12 | |
| 13 | class SkString; |
| 14 | |
| 15 | class SkJSON { |
| 16 | public: |
| 17 | enum Type { |
| 18 | kObject, |
| 19 | kArray, |
| 20 | kString, |
| 21 | kInt, |
| 22 | kFloat, |
| 23 | kBool, |
| 24 | }; |
| 25 | |
| 26 | class Array; |
| 27 | |
| 28 | class Object { |
| 29 | public: |
| 30 | Object() : fHead(NULL), fTail(NULL) {} |
| 31 | Object(const Object&); |
| 32 | ~Object(); |
| 33 | |
| 34 | void addObject(const char name[], Object* value); |
| 35 | void addArray(const char name[], Array* value); |
| 36 | void addString(const char name[], const char value[]); |
| 37 | void addInt(const char name[], int32_t value); |
| 38 | void addFloat(const char name[], float value); |
| 39 | void addBool(const char name[], bool value); |
| 40 | |
| 41 | bool findObject(const char name[], Object** = NULL) const; |
| 42 | bool findArray(const char name[], Array** = NULL) const; |
| 43 | bool findString(const char name[], SkString* = NULL) const; |
| 44 | bool findInt(const char name[], int32_t* = NULL) const; |
| 45 | bool findFloat(const char name[], float* = NULL) const; |
| 46 | bool findBool(const char name[], bool* = NULL) const; |
| 47 | |
| 48 | void dump() const; |
| 49 | |
| 50 | private: |
| 51 | struct Slot; |
| 52 | Slot* fHead; |
| 53 | Slot* fTail; |
| 54 | |
| 55 | const Slot* findSlot(const char name[]) const; |
| 56 | const Slot* findSlotAndType(const char name[], Type) const; |
| 57 | Slot* addSlot(Slot*); |
| 58 | void dumpLevel(int level) const; |
| 59 | |
| 60 | friend class Array; |
| 61 | }; |
| 62 | |
| 63 | class Array { |
| 64 | public: |
| 65 | // do I support strings, objects, arrays? |
| 66 | Array(Type, int count); |
| 67 | Array(const int32_t values[], int count); |
| 68 | Array(const float values[], int count); |
| 69 | Array(const bool values[], int count); |
| 70 | Array(const Array&); |
| 71 | ~Array(); |
| 72 | |
| 73 | int count() const { return fCount; } |
| 74 | Type type() const { return fType; } |
| 75 | |
| 76 | int32_t* ints() const { return fArray.fInts; } |
| 77 | float* floats() const { return fArray.fFloats; } |
| 78 | bool* bools() const { return fArray.fBools; } |
| 79 | |
| 80 | private: |
| 81 | int fCount; |
| 82 | Type fType; |
| 83 | union { |
| 84 | void* fVoids; |
| 85 | int32_t* fInts; |
| 86 | float* fFloats; |
| 87 | bool* fBools; |
| 88 | } fArray; |
| 89 | |
| 90 | void init(Type, int count, const void* src); |
| 91 | void dumpLevel(int level) const; |
| 92 | |
| 93 | friend class Object; |
| 94 | }; |
| 95 | }; |
| 96 | |
| 97 | #endif |