gfx/skia/trunk/include/utils/SkJSON.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/include/utils/SkJSON.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,285 @@
     1.4 +/*
     1.5 + * Copyright 2011 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#ifndef SkJSON_DEFINED
    1.12 +#define SkJSON_DEFINED
    1.13 +
    1.14 +#include "SkTypes.h"
    1.15 +
    1.16 +class SkStream;
    1.17 +class SkString;
    1.18 +
    1.19 +class SkJSON {
    1.20 +public:
    1.21 +    enum Type {
    1.22 +        kObject,
    1.23 +        kArray,
    1.24 +        kString,
    1.25 +        kInt,
    1.26 +        kFloat,
    1.27 +        kBool,
    1.28 +    };
    1.29 +
    1.30 +    class Array;
    1.31 +
    1.32 +    class Object {
    1.33 +    private:
    1.34 +        struct Slot;
    1.35 +
    1.36 +    public:
    1.37 +        Object();
    1.38 +        Object(const Object&);
    1.39 +        ~Object();
    1.40 +
    1.41 +        /**
    1.42 +         *  Create a new slot with the specified name and value. The name
    1.43 +         *  parameter is copied, but ownership of the Object parameter is
    1.44 +         *  transferred. The Object parameter may be null, but the name must
    1.45 +         *  not be null.
    1.46 +         */
    1.47 +        void addObject(const char name[], Object* value);
    1.48 +
    1.49 +        /**
    1.50 +         *  Create a new slot with the specified name and value. The name
    1.51 +         *  parameter is copied, but ownership of the Array parameter is
    1.52 +         *  transferred. The Array parameter may be null, but the name must
    1.53 +         *  not be null.
    1.54 +         */
    1.55 +        void addArray(const char name[], Array* value);
    1.56 +
    1.57 +        /**
    1.58 +         *  Create a new slot with the specified name and value. Both parameters
    1.59 +         *  are copied. The value parameter may be null, but the name must
    1.60 +         *  not be null.
    1.61 +         */
    1.62 +        void addString(const char name[], const char value[]);
    1.63 +
    1.64 +        /**
    1.65 +         *  Create a new slot with the specified name and value. The name
    1.66 +         *  parameter is copied, and must not be null.
    1.67 +         */
    1.68 +        void addInt(const char name[], int32_t value);
    1.69 +
    1.70 +        /**
    1.71 +         *  Create a new slot with the specified name and value. The name
    1.72 +         *  parameter is copied, and must not be null.
    1.73 +         */
    1.74 +        void addFloat(const char name[], float value);
    1.75 +
    1.76 +        /**
    1.77 +         *  Create a new slot with the specified name and value. The name
    1.78 +         *  parameter is copied, and must not be null.
    1.79 +         */
    1.80 +        void addBool(const char name[], bool value);
    1.81 +
    1.82 +        /**
    1.83 +         *  Return the number of slots/fields in this object. These can be
    1.84 +         *  iterated using Iter.
    1.85 +         */
    1.86 +        int count() const;
    1.87 +
    1.88 +        /**
    1.89 +         *  Returns true if a slot matching the name and Type is found.
    1.90 +         */
    1.91 +        bool find(const char name[], Type) const;
    1.92 +        bool findObject(const char name[], Object** = NULL) const;
    1.93 +        bool findArray(const char name[], Array** = NULL) const;
    1.94 +        bool findString(const char name[], SkString* = NULL) const;
    1.95 +        bool findInt(const char name[], int32_t* = NULL) const;
    1.96 +        bool findFloat(const char name[], float* = NULL) const;
    1.97 +        bool findBool(const char name[], bool* = NULL) const;
    1.98 +
    1.99 +        /**
   1.100 +         *  Finds the first slot matching the name and Type and removes it.
   1.101 +         *  Returns true if found, false if not.
   1.102 +         */
   1.103 +        bool remove(const char name[], Type);
   1.104 +
   1.105 +        void toDebugf() const;
   1.106 +
   1.107 +        /**
   1.108 +         *  Iterator class which returns all of the fields/slots in an Object,
   1.109 +         *  in the order that they were added.
   1.110 +         */
   1.111 +        class Iter {
   1.112 +        public:
   1.113 +            Iter(const Object&);
   1.114 +
   1.115 +            /**
   1.116 +             *  Returns true when there are no more entries in the iterator.
   1.117 +             *  In this case, no other methods should be called.
   1.118 +             */
   1.119 +            bool done() const;
   1.120 +
   1.121 +            /**
   1.122 +             *  Moves the iterator to the next element. Should only be called
   1.123 +             *  if done() returns false.
   1.124 +             */
   1.125 +            void next();
   1.126 +
   1.127 +            /**
   1.128 +             *  Returns the type of the current element. Should only be called
   1.129 +             *  if done() returns false.
   1.130 +             */
   1.131 +            Type type() const;
   1.132 +
   1.133 +            /**
   1.134 +             *  Returns the name of the current element. Should only be called
   1.135 +             *  if done() returns false.
   1.136 +             */
   1.137 +            const char* name() const;
   1.138 +
   1.139 +            /**
   1.140 +             *  Returns the type of the current element. Should only be called
   1.141 +             *  if done() returns false and type() returns kObject.
   1.142 +             */
   1.143 +            Object* objectValue() const;
   1.144 +
   1.145 +            /**
   1.146 +             *  Returns the type of the current element. Should only be called
   1.147 +             *  if done() returns false and type() returns kArray.
   1.148 +             */
   1.149 +            Array* arrayValue() const;
   1.150 +
   1.151 +            /**
   1.152 +             *  Returns the type of the current element. Should only be called
   1.153 +             *  if done() returns false and type() returns kString.
   1.154 +             */
   1.155 +            const char* stringValue() const;
   1.156 +
   1.157 +            /**
   1.158 +             *  Returns the type of the current element. Should only be called
   1.159 +             *  if done() returns false and type() returns kInt.
   1.160 +             */
   1.161 +            int32_t intValue() const;
   1.162 +
   1.163 +            /**
   1.164 +             *  Returns the type of the current element. Should only be called
   1.165 +             *  if done() returns false and type() returns kFloat.
   1.166 +             */
   1.167 +            float floatValue() const;
   1.168 +
   1.169 +            /**
   1.170 +             *  Returns the type of the current element. Should only be called
   1.171 +             *  if done() returns false and type() returns kBool.
   1.172 +             */
   1.173 +            bool boolValue() const;
   1.174 +
   1.175 +        private:
   1.176 +            Slot* fSlot;
   1.177 +        };
   1.178 +
   1.179 +    private:
   1.180 +        Slot* fHead;
   1.181 +        Slot* fTail;
   1.182 +
   1.183 +        const Slot* findSlot(const char name[], Type) const;
   1.184 +        Slot* addSlot(Slot*);
   1.185 +        void dumpLevel(int level) const;
   1.186 +
   1.187 +        friend class Array;
   1.188 +    };
   1.189 +
   1.190 +    class Array {
   1.191 +    public:
   1.192 +        /**
   1.193 +         *  Creates an array with the specified Type and element count. All
   1.194 +         *  entries are initialized to NULL/0/false.
   1.195 +         */
   1.196 +        Array(Type, int count);
   1.197 +
   1.198 +        /**
   1.199 +         *  Creates an array of ints, initialized by copying the specified
   1.200 +         *  values.
   1.201 +         */
   1.202 +        Array(const int32_t values[], int count);
   1.203 +
   1.204 +        /**
   1.205 +         *  Creates an array of floats, initialized by copying the specified
   1.206 +         *  values.
   1.207 +         */
   1.208 +        Array(const float values[], int count);
   1.209 +
   1.210 +        /**
   1.211 +         *  Creates an array of bools, initialized by copying the specified
   1.212 +         *  values.
   1.213 +         */
   1.214 +        Array(const bool values[], int count);
   1.215 +
   1.216 +        Array(const Array&);
   1.217 +        ~Array();
   1.218 +
   1.219 +        int count() const { return fCount; }
   1.220 +        Type type() const { return fType; }
   1.221 +
   1.222 +        /**
   1.223 +         *  Replace the element at the specified index with the specified
   1.224 +         *  Object (which may be null). Ownership of the Object is transferred.
   1.225 +         *  Should only be called if the Array's type is kObject.
   1.226 +         */
   1.227 +        void setObject(int index, Object*);
   1.228 +
   1.229 +        /**
   1.230 +         *  Replace the element at the specified index with the specified
   1.231 +         *  Array (which may be null). Ownership of the Array is transferred.
   1.232 +         *  Should only be called if the Array's type is kArray.
   1.233 +         */
   1.234 +        void setArray(int index, Array*);
   1.235 +
   1.236 +        /**
   1.237 +         *  Replace the element at the specified index with a copy of the
   1.238 +         *  specified string (which may be null). Should only be called if the
   1.239 +         *  Array's type is kString.
   1.240 +         */
   1.241 +        void setString(int index, const char str[]);
   1.242 +
   1.243 +        Object* const* objects() const {
   1.244 +            SkASSERT(kObject == fType);
   1.245 +            return fArray.fObjects;
   1.246 +        }
   1.247 +        Array* const* arrays() const {
   1.248 +            SkASSERT(kObject == fType);
   1.249 +            return fArray.fArrays;
   1.250 +        }
   1.251 +        const char* const* strings() const {
   1.252 +            SkASSERT(kString == fType);
   1.253 +            return fArray.fStrings;
   1.254 +        }
   1.255 +        int32_t* ints() const {
   1.256 +            SkASSERT(kInt == fType);
   1.257 +            return fArray.fInts;
   1.258 +        }
   1.259 +        float* floats() const {
   1.260 +            SkASSERT(kFloat == fType);
   1.261 +            return fArray.fFloats;
   1.262 +        }
   1.263 +        bool* bools() const {
   1.264 +            SkASSERT(kBool == fType);
   1.265 +            return fArray.fBools;
   1.266 +        }
   1.267 +
   1.268 +    private:
   1.269 +        int fCount;
   1.270 +        Type fType;
   1.271 +        union {
   1.272 +            void*    fVoids;
   1.273 +            Object** fObjects;
   1.274 +            Array**  fArrays;
   1.275 +            char**   fStrings;
   1.276 +            int32_t* fInts;
   1.277 +            float*   fFloats;
   1.278 +            bool*    fBools;
   1.279 +        } fArray;
   1.280 +
   1.281 +        void init(Type, int count, const void* src);
   1.282 +        void dumpLevel(int level) const;
   1.283 +
   1.284 +        friend class Object;
   1.285 +    };
   1.286 +};
   1.287 +
   1.288 +#endif

mercurial