reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SkMetaData_DEFINED |
| 18 | #define SkMetaData_DEFINED |
| 19 | |
| 20 | #include "SkScalar.h" |
| 21 | |
| 22 | class SkMetaData { |
| 23 | public: |
| 24 | SkMetaData(); |
| 25 | SkMetaData(const SkMetaData& src); |
| 26 | ~SkMetaData(); |
| 27 | |
| 28 | SkMetaData& operator=(const SkMetaData& src); |
| 29 | |
| 30 | void reset(); |
| 31 | |
| 32 | bool findS32(const char name[], int32_t* value = NULL) const; |
| 33 | bool findScalar(const char name[], SkScalar* value = NULL) const; |
| 34 | const SkScalar* findScalars(const char name[], int* count, SkScalar values[] = NULL) const; |
| 35 | const char* findString(const char name[]) const; |
| 36 | bool findPtr(const char name[], void** value = NULL) const; |
| 37 | bool findBool(const char name[], bool* value = NULL) const; |
| 38 | |
| 39 | bool hasS32(const char name[], int32_t value) const |
| 40 | { |
| 41 | int32_t v; |
| 42 | return this->findS32(name, &v) && v == value; |
| 43 | } |
| 44 | bool hasScalar(const char name[], SkScalar value) const |
| 45 | { |
| 46 | SkScalar v; |
| 47 | return this->findScalar(name, &v) && v == value; |
| 48 | } |
| 49 | bool hasString(const char name[], const char value[]) const |
| 50 | { |
| 51 | const char* v = this->findString(name); |
| 52 | return v == NULL && value == NULL || |
| 53 | v != NULL && value != NULL && !strcmp(v, value); |
| 54 | } |
| 55 | bool hasPtr(const char name[], void* value) const |
| 56 | { |
| 57 | void* v; |
| 58 | return this->findPtr(name, &v) && v == value; |
| 59 | } |
| 60 | bool hasBool(const char name[], bool value) const |
| 61 | { |
| 62 | bool v; |
| 63 | return this->findBool(name, &v) && v == value; |
| 64 | } |
| 65 | |
| 66 | void setS32(const char name[], int32_t value); |
| 67 | void setScalar(const char name[], SkScalar value); |
| 68 | SkScalar* setScalars(const char name[], int count, const SkScalar values[] = NULL); |
| 69 | void setString(const char name[], const char value[]); |
| 70 | void setPtr(const char name[], void* value); |
| 71 | void setBool(const char name[], bool value); |
| 72 | |
| 73 | bool removeS32(const char name[]); |
| 74 | bool removeScalar(const char name[]); |
| 75 | bool removeString(const char name[]); |
| 76 | bool removePtr(const char name[]); |
| 77 | bool removeBool(const char name[]); |
| 78 | |
| 79 | SkDEBUGCODE(static void UnitTest();) |
| 80 | |
| 81 | enum Type { |
| 82 | kS32_Type, |
| 83 | kScalar_Type, |
| 84 | kString_Type, |
| 85 | kPtr_Type, |
| 86 | kBool_Type, |
| 87 | |
| 88 | kTypeCount |
| 89 | }; |
| 90 | |
| 91 | struct Rec; |
| 92 | class Iter; |
| 93 | friend class Iter; |
| 94 | |
| 95 | class Iter { |
| 96 | public: |
| 97 | Iter() : fRec(NULL) {} |
| 98 | Iter(const SkMetaData&); |
| 99 | |
| 100 | /** Reset the iterator, so that calling next() will return the first |
| 101 | data element. This is done implicitly in the constructor. |
| 102 | */ |
| 103 | void reset(const SkMetaData&); |
| 104 | |
| 105 | /** Each time next is called, it returns the name of the next data element, |
| 106 | or null when there are no more elements. If non-null is returned, then the |
| 107 | element's type is returned (if not null), and the number of data values |
| 108 | is returned in count (if not null). |
| 109 | */ |
| 110 | const char* next(Type*, int* count); |
| 111 | |
| 112 | private: |
| 113 | Rec* fRec; |
| 114 | }; |
| 115 | |
| 116 | public: |
| 117 | struct Rec { |
| 118 | Rec* fNext; |
| 119 | uint16_t fDataCount; // number of elements |
| 120 | uint8_t fDataLen; // sizeof a single element |
| 121 | #ifdef SK_DEBUG |
| 122 | Type fType; |
| 123 | #else |
| 124 | uint8_t fType; |
| 125 | #endif |
| 126 | |
| 127 | #ifdef SK_DEBUG |
| 128 | const char* fName; |
| 129 | union { |
| 130 | int32_t fS32; |
| 131 | SkScalar fScalar; |
| 132 | const char* fString; |
| 133 | void* fPtr; |
| 134 | bool fBool; |
| 135 | } fData; |
| 136 | #endif |
| 137 | |
| 138 | const void* data() const { return (this + 1); } |
| 139 | void* data() { return (this + 1); } |
| 140 | const char* name() const { return (const char*)this->data() + fDataLen * fDataCount; } |
| 141 | char* name() { return (char*)this->data() + fDataLen * fDataCount; } |
| 142 | |
| 143 | static Rec* Alloc(size_t); |
| 144 | static void Free(Rec*); |
| 145 | }; |
| 146 | Rec* fRec; |
| 147 | |
| 148 | const Rec* find(const char name[], Type) const; |
| 149 | void* set(const char name[], const void* data, size_t len, Type, int count); |
| 150 | bool remove(const char name[], Type); |
| 151 | }; |
| 152 | |
| 153 | #endif |
| 154 | |