SkMetaData.h revision 80bacfeb4bda06541e8695bd502229727bccfea
180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2006 The Android Open Source Project
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifndef SkMetaData_DEFINED
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkMetaData_DEFINED
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkScalar.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkRefCnt;
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SK_API SkMetaData {
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Used to manage the life-cycle of a ptr in the metadata. This is option
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  in setPtr, and is only invoked when either copying one metadata to
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  another, or when the metadata is destroyed.
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  setPtr(name, ptr, proc) {
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *      fPtr = proc(ptr, true);
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  }
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  copy: A = B {
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *      A.fPtr = B.fProc(B.fPtr, true);
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  }
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  ~SkMetaData {
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *      fProc(fPtr, false);
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  }
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    typedef void* (*PtrProc)(void* ptr, bool doRef);
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Implements PtrProc for SkRefCnt pointers
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static void* RefCntProc(void* ptr, bool doRef);
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkMetaData();
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkMetaData(const SkMetaData& src);
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    ~SkMetaData();
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkMetaData& operator=(const SkMetaData& src);
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void reset();
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool findS32(const char name[], int32_t* value = NULL) const;
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool findScalar(const char name[], SkScalar* value = NULL) const;
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const SkScalar* findScalars(const char name[], int* count,
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                SkScalar values[] = NULL) const;
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const char* findString(const char name[]) const;
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool findPtr(const char name[], void** value = NULL, PtrProc* = NULL) const;
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool findBool(const char name[], bool* value = NULL) const;
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const void* findData(const char name[], size_t* byteCount = NULL) const;
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool hasS32(const char name[], int32_t value) const {
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        int32_t v;
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return this->findS32(name, &v) && v == value;
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool hasScalar(const char name[], SkScalar value) const {
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkScalar v;
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return this->findScalar(name, &v) && v == value;
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool hasString(const char name[], const char value[]) const {
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        const char* v = this->findString(name);
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return  (v == NULL && value == NULL) ||
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                (v != NULL && value != NULL && !strcmp(v, value));
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool hasPtr(const char name[], void* value) const {
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        void* v;
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return this->findPtr(name, &v) && v == value;
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool hasBool(const char name[], bool value) const {
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        bool    v;
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return this->findBool(name, &v) && v == value;
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool hasData(const char name[], const void* data, size_t byteCount) const {
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        size_t len;
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        const void* ptr = this->findData(name, &len);
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return NULL != ptr && len == byteCount && !memcmp(ptr, data, len);
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setS32(const char name[], int32_t value);
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setScalar(const char name[], SkScalar value);
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkScalar* setScalars(const char name[], int count, const SkScalar values[] = NULL);
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setString(const char name[], const char value[]);
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setPtr(const char name[], void* value, PtrProc proc = NULL);
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setBool(const char name[], bool value);
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // the data is copied from the input pointer.
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setData(const char name[], const void* data, size_t byteCount);
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool removeS32(const char name[]);
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool removeScalar(const char name[]);
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool removeString(const char name[]);
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool removePtr(const char name[]);
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool removeBool(const char name[]);
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool removeData(const char name[]);
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // helpers for SkRefCnt
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool findRefCnt(const char name[], SkRefCnt** ptr = NULL) {
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return this->findPtr(name, reinterpret_cast<void**>(ptr));
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool hasRefCnt(const char name[], SkRefCnt* ptr) {
10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return this->hasPtr(name, ptr);
10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setRefCnt(const char name[], SkRefCnt* ptr) {
11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->setPtr(name, ptr, RefCntProc);
11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
11380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool removeRefCnt(const char name[]) {
11480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return this->removePtr(name);
11580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
11680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    enum Type {
11880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kS32_Type,
11980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kScalar_Type,
12080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kString_Type,
12180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kPtr_Type,
12280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kBool_Type,
12380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kData_Type,
12480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kTypeCount
12680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
12780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    struct Rec;
12980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    class Iter;
13080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    friend class Iter;
13180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    class Iter {
13380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    public:
13480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        Iter() : fRec(NULL) {}
13580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        Iter(const SkMetaData&);
13680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        /** Reset the iterator, so that calling next() will return the first
13880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            data element. This is done implicitly in the constructor.
13980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        */
14080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        void reset(const SkMetaData&);
14180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        /** Each time next is called, it returns the name of the next data element,
14380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            or null when there are no more elements. If non-null is returned, then the
14480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            element's type is returned (if not null), and the number of data values
14580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            is returned in count (if not null).
14680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        */
14780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        const char* next(Type*, int* count);
14880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    private:
15080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        Rec* fRec;
15180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
15280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
15380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
15480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    struct Rec {
15580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        Rec*        fNext;
15680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        uint16_t    fDataCount; // number of elements
15780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        uint8_t     fDataLen;   // sizeof a single element
15880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        uint8_t     fType;
15980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
16080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        const void* data() const { return (this + 1); }
16180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        void*       data() { return (this + 1); }
16280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        const char* name() const { return (const char*)this->data() + fDataLen * fDataCount; }
16380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        char*       name() { return (char*)this->data() + fDataLen * fDataCount; }
16480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
16580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        static Rec* Alloc(size_t);
16680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        static void Free(Rec*);
16780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
16880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Rec*    fRec;
16980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
17080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const Rec* find(const char name[], Type) const;
17180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void* set(const char name[], const void* data, size_t len, Type, int count);
17280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool remove(const char name[], Type);
17380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
17480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
17580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
17680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
177