Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2012 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef SkDataSet_DEFINED
      9 #define SkDataSet_DEFINED
     10 
     11 #include "SkData.h"
     12 #include "SkFlattenable.h"
     13 
     14 class SkStream;
     15 class SkWStream;
     16 
     17 class SkDataSet : public SkFlattenable {
     18 public:
     19     /**
     20      *  Returns a new empty dataset. Note: since SkDataSet is immutable, this
     21      *  "new" set may be the same one that was returned before, but each
     22      *  returned object must have its reference-count balanced regardless.
     23      *
     24      *  SkDataSet* empty = SkDataSet::NewEmpty();
     25      *  ...
     26      *  empty->unref();
     27      */
     28     static SkDataSet* NewEmpty();
     29 
     30     struct Pair {
     31         const char* fKey;
     32         SkData*     fValue;
     33     };
     34 
     35     SkDataSet(const char key[], SkData* value);
     36     SkDataSet(const Pair[], int count);
     37     virtual ~SkDataSet();
     38 
     39     bool isEmpty() const { return 0 == fCount; }
     40     int count() const { return fCount; }
     41     SkData* find(const char name[]) const;
     42 
     43     class Iter {
     44     public:
     45         Iter(const SkDataSet& ds) {
     46             fPair = ds.fPairs;
     47             fStop = ds.fPairs + ds.fCount;
     48         }
     49 
     50         const char* key() const {
     51             SkASSERT(!this->done());
     52             return fPair->fKey;
     53         }
     54 
     55         SkData* value() const {
     56             SkASSERT(!this->done());
     57             return fPair->fValue;
     58         }
     59 
     60         bool done() const { return fPair >= fStop; }
     61         void next() {
     62             SkASSERT(!this->done());
     63             fPair += 1;
     64         }
     65 
     66     private:
     67         const SkDataSet::Pair* fPair;
     68         const SkDataSet::Pair* fStop;
     69     };
     70 
     71     explicit SkDataSet(SkStream*);
     72     void writeToStream(SkWStream*) const;
     73 
     74     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDataSet)
     75 
     76 protected:
     77     SkDataSet(SkFlattenableReadBuffer&);
     78     virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
     79 
     80 private:
     81     int32_t     fCount;
     82     uint32_t    fKeySize;
     83     Pair*       fPairs;
     84 
     85     typedef SkFlattenable INHERITED;
     86 };
     87 
     88 #endif
     89