Home | History | Annotate | Download | only in core
      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 SkRefCnt;
     23 
     24 class SK_API SkMetaData {
     25 public:
     26     /**
     27      *  Used to manage the life-cycle of a ptr in the metadata. This is option
     28      *  in setPtr, and is only invoked when either copying one metadata to
     29      *  another, or when the metadata is destroyed.
     30      *
     31      *  setPtr(name, ptr, proc) {
     32      *      fPtr = proc(ptr, true);
     33      *  }
     34      *
     35      *  copy: A = B {
     36      *      A.fPtr = B.fProc(B.fPtr, true);
     37      *  }
     38      *
     39      *  ~SkMetaData {
     40      *      fProc(fPtr, false);
     41      *  }
     42      */
     43     typedef void* (*PtrProc)(void* ptr, bool doRef);
     44 
     45     /**
     46      *  Implements PtrProc for SkRefCnt pointers
     47      */
     48     static void* RefCntProc(void* ptr, bool doRef);
     49 
     50     SkMetaData();
     51     SkMetaData(const SkMetaData& src);
     52     ~SkMetaData();
     53 
     54     SkMetaData& operator=(const SkMetaData& src);
     55 
     56     void reset();
     57 
     58     bool findS32(const char name[], int32_t* value = NULL) const;
     59     bool findScalar(const char name[], SkScalar* value = NULL) const;
     60     const SkScalar* findScalars(const char name[], int* count,
     61                                 SkScalar values[] = NULL) const;
     62     const char* findString(const char name[]) const;
     63     bool findPtr(const char name[], void** value = NULL, PtrProc* = NULL) const;
     64     bool findBool(const char name[], bool* value = NULL) const;
     65     const void* findData(const char name[], size_t* byteCount = NULL) const;
     66 
     67     bool hasS32(const char name[], int32_t value) const {
     68         int32_t v;
     69         return this->findS32(name, &v) && v == value;
     70     }
     71     bool hasScalar(const char name[], SkScalar value) const {
     72         SkScalar v;
     73         return this->findScalar(name, &v) && v == value;
     74     }
     75     bool hasString(const char name[], const char value[]) const {
     76         const char* v = this->findString(name);
     77         return  (v == NULL && value == NULL) ||
     78                 (v != NULL && value != NULL && !strcmp(v, value));
     79     }
     80     bool hasPtr(const char name[], void* value) const {
     81         void* v;
     82         return this->findPtr(name, &v) && v == value;
     83     }
     84     bool hasBool(const char name[], bool value) const {
     85         bool    v;
     86         return this->findBool(name, &v) && v == value;
     87     }
     88     bool hasData(const char name[], const void* data, size_t byteCount) const {
     89         size_t len;
     90         const void* ptr = this->findData(name, &len);
     91         return NULL != ptr && len == byteCount && !memcmp(ptr, data, len);
     92     }
     93 
     94     void setS32(const char name[], int32_t value);
     95     void setScalar(const char name[], SkScalar value);
     96     SkScalar* setScalars(const char name[], int count, const SkScalar values[] = NULL);
     97     void setString(const char name[], const char value[]);
     98     void setPtr(const char name[], void* value, PtrProc proc = NULL);
     99     void setBool(const char name[], bool value);
    100     // the data is copied from the input pointer.
    101     void setData(const char name[], const void* data, size_t byteCount);
    102 
    103     bool removeS32(const char name[]);
    104     bool removeScalar(const char name[]);
    105     bool removeString(const char name[]);
    106     bool removePtr(const char name[]);
    107     bool removeBool(const char name[]);
    108     bool removeData(const char name[]);
    109 
    110     // helpers for SkRefCnt
    111     bool findRefCnt(const char name[], SkRefCnt** ptr = NULL) {
    112         return this->findPtr(name, reinterpret_cast<void**>(ptr));
    113     }
    114     bool hasRefCnt(const char name[], SkRefCnt* ptr) {
    115         return this->hasPtr(name, ptr);
    116     }
    117     void setRefCnt(const char name[], SkRefCnt* ptr) {
    118         this->setPtr(name, ptr, RefCntProc);
    119     }
    120     bool removeRefCnt(const char name[]) {
    121         return this->removePtr(name);
    122     }
    123 
    124     enum Type {
    125         kS32_Type,
    126         kScalar_Type,
    127         kString_Type,
    128         kPtr_Type,
    129         kBool_Type,
    130         kData_Type,
    131 
    132         kTypeCount
    133     };
    134 
    135     struct Rec;
    136     class Iter;
    137     friend class Iter;
    138 
    139     class Iter {
    140     public:
    141         Iter() : fRec(NULL) {}
    142         Iter(const SkMetaData&);
    143 
    144         /** Reset the iterator, so that calling next() will return the first
    145             data element. This is done implicitly in the constructor.
    146         */
    147         void reset(const SkMetaData&);
    148 
    149         /** Each time next is called, it returns the name of the next data element,
    150             or null when there are no more elements. If non-null is returned, then the
    151             element's type is returned (if not null), and the number of data values
    152             is returned in count (if not null).
    153         */
    154         const char* next(Type*, int* count);
    155 
    156     private:
    157         Rec* fRec;
    158     };
    159 
    160 public:
    161     struct Rec {
    162         Rec*        fNext;
    163         uint16_t    fDataCount; // number of elements
    164         uint8_t     fDataLen;   // sizeof a single element
    165         uint8_t     fType;
    166 
    167         const void* data() const { return (this + 1); }
    168         void*       data() { return (this + 1); }
    169         const char* name() const { return (const char*)this->data() + fDataLen * fDataCount; }
    170         char*       name() { return (char*)this->data() + fDataLen * fDataCount; }
    171 
    172         static Rec* Alloc(size_t);
    173         static void Free(Rec*);
    174     };
    175     Rec*    fRec;
    176 
    177     const Rec* find(const char name[], Type) const;
    178     void* set(const char name[], const void* data, size_t len, Type, int count);
    179     bool remove(const char name[], Type);
    180 };
    181 
    182 #endif
    183 
    184