gfx/skia/trunk/include/core/SkMetaData.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/include/core/SkMetaData.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,175 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2006 The Android Open Source Project
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +
    1.13 +#ifndef SkMetaData_DEFINED
    1.14 +#define SkMetaData_DEFINED
    1.15 +
    1.16 +#include "SkScalar.h"
    1.17 +
    1.18 +class SkRefCnt;
    1.19 +
    1.20 +class SK_API SkMetaData {
    1.21 +public:
    1.22 +    /**
    1.23 +     *  Used to manage the life-cycle of a ptr in the metadata. This is option
    1.24 +     *  in setPtr, and is only invoked when either copying one metadata to
    1.25 +     *  another, or when the metadata is destroyed.
    1.26 +     *
    1.27 +     *  setPtr(name, ptr, proc) {
    1.28 +     *      fPtr = proc(ptr, true);
    1.29 +     *  }
    1.30 +     *
    1.31 +     *  copy: A = B {
    1.32 +     *      A.fPtr = B.fProc(B.fPtr, true);
    1.33 +     *  }
    1.34 +     *
    1.35 +     *  ~SkMetaData {
    1.36 +     *      fProc(fPtr, false);
    1.37 +     *  }
    1.38 +     */
    1.39 +    typedef void* (*PtrProc)(void* ptr, bool doRef);
    1.40 +
    1.41 +    /**
    1.42 +     *  Implements PtrProc for SkRefCnt pointers
    1.43 +     */
    1.44 +    static void* RefCntProc(void* ptr, bool doRef);
    1.45 +
    1.46 +    SkMetaData();
    1.47 +    SkMetaData(const SkMetaData& src);
    1.48 +    ~SkMetaData();
    1.49 +
    1.50 +    SkMetaData& operator=(const SkMetaData& src);
    1.51 +
    1.52 +    void reset();
    1.53 +
    1.54 +    bool findS32(const char name[], int32_t* value = NULL) const;
    1.55 +    bool findScalar(const char name[], SkScalar* value = NULL) const;
    1.56 +    const SkScalar* findScalars(const char name[], int* count,
    1.57 +                                SkScalar values[] = NULL) const;
    1.58 +    const char* findString(const char name[]) const;
    1.59 +    bool findPtr(const char name[], void** value = NULL, PtrProc* = NULL) const;
    1.60 +    bool findBool(const char name[], bool* value = NULL) const;
    1.61 +    const void* findData(const char name[], size_t* byteCount = NULL) const;
    1.62 +
    1.63 +    bool hasS32(const char name[], int32_t value) const {
    1.64 +        int32_t v;
    1.65 +        return this->findS32(name, &v) && v == value;
    1.66 +    }
    1.67 +    bool hasScalar(const char name[], SkScalar value) const {
    1.68 +        SkScalar v;
    1.69 +        return this->findScalar(name, &v) && v == value;
    1.70 +    }
    1.71 +    bool hasString(const char name[], const char value[]) const {
    1.72 +        const char* v = this->findString(name);
    1.73 +        return  (v == NULL && value == NULL) ||
    1.74 +                (v != NULL && value != NULL && !strcmp(v, value));
    1.75 +    }
    1.76 +    bool hasPtr(const char name[], void* value) const {
    1.77 +        void* v;
    1.78 +        return this->findPtr(name, &v) && v == value;
    1.79 +    }
    1.80 +    bool hasBool(const char name[], bool value) const {
    1.81 +        bool    v;
    1.82 +        return this->findBool(name, &v) && v == value;
    1.83 +    }
    1.84 +    bool hasData(const char name[], const void* data, size_t byteCount) const {
    1.85 +        size_t len;
    1.86 +        const void* ptr = this->findData(name, &len);
    1.87 +        return NULL != ptr && len == byteCount && !memcmp(ptr, data, len);
    1.88 +    }
    1.89 +
    1.90 +    void setS32(const char name[], int32_t value);
    1.91 +    void setScalar(const char name[], SkScalar value);
    1.92 +    SkScalar* setScalars(const char name[], int count, const SkScalar values[] = NULL);
    1.93 +    void setString(const char name[], const char value[]);
    1.94 +    void setPtr(const char name[], void* value, PtrProc proc = NULL);
    1.95 +    void setBool(const char name[], bool value);
    1.96 +    // the data is copied from the input pointer.
    1.97 +    void setData(const char name[], const void* data, size_t byteCount);
    1.98 +
    1.99 +    bool removeS32(const char name[]);
   1.100 +    bool removeScalar(const char name[]);
   1.101 +    bool removeString(const char name[]);
   1.102 +    bool removePtr(const char name[]);
   1.103 +    bool removeBool(const char name[]);
   1.104 +    bool removeData(const char name[]);
   1.105 +
   1.106 +    // helpers for SkRefCnt
   1.107 +    bool findRefCnt(const char name[], SkRefCnt** ptr = NULL) {
   1.108 +        return this->findPtr(name, reinterpret_cast<void**>(ptr));
   1.109 +    }
   1.110 +    bool hasRefCnt(const char name[], SkRefCnt* ptr) {
   1.111 +        return this->hasPtr(name, ptr);
   1.112 +    }
   1.113 +    void setRefCnt(const char name[], SkRefCnt* ptr) {
   1.114 +        this->setPtr(name, ptr, RefCntProc);
   1.115 +    }
   1.116 +    bool removeRefCnt(const char name[]) {
   1.117 +        return this->removePtr(name);
   1.118 +    }
   1.119 +
   1.120 +    enum Type {
   1.121 +        kS32_Type,
   1.122 +        kScalar_Type,
   1.123 +        kString_Type,
   1.124 +        kPtr_Type,
   1.125 +        kBool_Type,
   1.126 +        kData_Type,
   1.127 +
   1.128 +        kTypeCount
   1.129 +    };
   1.130 +
   1.131 +    struct Rec;
   1.132 +    class Iter;
   1.133 +    friend class Iter;
   1.134 +
   1.135 +    class Iter {
   1.136 +    public:
   1.137 +        Iter() : fRec(NULL) {}
   1.138 +        Iter(const SkMetaData&);
   1.139 +
   1.140 +        /** Reset the iterator, so that calling next() will return the first
   1.141 +            data element. This is done implicitly in the constructor.
   1.142 +        */
   1.143 +        void reset(const SkMetaData&);
   1.144 +
   1.145 +        /** Each time next is called, it returns the name of the next data element,
   1.146 +            or null when there are no more elements. If non-null is returned, then the
   1.147 +            element's type is returned (if not null), and the number of data values
   1.148 +            is returned in count (if not null).
   1.149 +        */
   1.150 +        const char* next(Type*, int* count);
   1.151 +
   1.152 +    private:
   1.153 +        Rec* fRec;
   1.154 +    };
   1.155 +
   1.156 +public:
   1.157 +    struct Rec {
   1.158 +        Rec*        fNext;
   1.159 +        uint16_t    fDataCount; // number of elements
   1.160 +        uint8_t     fDataLen;   // sizeof a single element
   1.161 +        uint8_t     fType;
   1.162 +
   1.163 +        const void* data() const { return (this + 1); }
   1.164 +        void*       data() { return (this + 1); }
   1.165 +        const char* name() const { return (const char*)this->data() + fDataLen * fDataCount; }
   1.166 +        char*       name() { return (char*)this->data() + fDataLen * fDataCount; }
   1.167 +
   1.168 +        static Rec* Alloc(size_t);
   1.169 +        static void Free(Rec*);
   1.170 +    };
   1.171 +    Rec*    fRec;
   1.172 +
   1.173 +    const Rec* find(const char name[], Type) const;
   1.174 +    void* set(const char name[], const void* data, size_t len, Type, int count);
   1.175 +    bool remove(const char name[], Type);
   1.176 +};
   1.177 +
   1.178 +#endif

mercurial