Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkDataTable.h
1 /*
2  * Copyright 2013 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 SkDataTable_DEFINED
9 #define SkDataTable_DEFINED
10 
11 #include "../private/SkTDArray.h"
12 #include "SkChunkAlloc.h"
13 #include "SkData.h"
14 #include "SkString.h"
15 
21 class SK_API SkDataTable : public SkRefCnt {
22 public:
26  bool isEmpty() const { return 0 == fCount; }
27 
31  int count() const { return fCount; }
32 
37  size_t atSize(int index) const;
38 
46  const void* at(int index, size_t* size = NULL) const;
47 
48  template <typename T>
49  const T* atT(int index, size_t* size = NULL) const {
50  return reinterpret_cast<const T*>(this->at(index, size));
51  }
52 
57  const char* atStr(int index) const {
58  size_t size;
59  const char* str = this->atT<const char>(index, &size);
60  SkASSERT(strlen(str) + 1 == size);
61  return str;
62  }
63 
64  typedef void (*FreeProc)(void* context);
65 
66  static sk_sp<SkDataTable> MakeEmpty();
67 
77  static sk_sp<SkDataTable> MakeCopyArrays(const void * const * ptrs,
78  const size_t sizes[], int count);
79 
88  static sk_sp<SkDataTable> MakeCopyArray(const void* array, size_t elemSize, int count);
89 
90  static sk_sp<SkDataTable> MakeArrayProc(const void* array, size_t elemSize, int count,
91  FreeProc proc, void* context);
92 
93 private:
94  struct Dir {
95  const void* fPtr;
96  uintptr_t fSize;
97  };
98 
99  int fCount;
100  size_t fElemSize;
101  union {
102  const Dir* fDir;
103  const char* fElems;
104  } fU;
105 
106  FreeProc fFreeProc;
107  void* fFreeProcContext;
108 
109  SkDataTable();
110  SkDataTable(const void* array, size_t elemSize, int count,
111  FreeProc, void* context);
112  SkDataTable(const Dir*, int count, FreeProc, void* context);
113  virtual ~SkDataTable();
114 
115  friend class SkDataTableBuilder; // access to Dir
116 
117  typedef SkRefCnt INHERITED;
118 };
119 
124 class SK_API SkDataTableBuilder : SkNoncopyable {
125 public:
126  SkDataTableBuilder(size_t minChunkSize);
128 
129  int count() const { return fDir.count(); }
130  size_t minChunkSize() const { return fMinChunkSize; }
131 
135  void reset(size_t minChunkSize);
136  void reset() {
137  this->reset(fMinChunkSize);
138  }
139 
143  void append(const void* data, size_t size);
144 
149  void appendStr(const char str[]) {
150  this->append(str, strlen(str) + 1);
151  }
152 
157  void appendString(const SkString& string) {
158  this->append(string.c_str(), string.size() + 1);
159  }
160 
166  sk_sp<SkDataTable> detachDataTable();
167 
168 private:
169  SkTDArray<SkDataTable::Dir> fDir;
170  SkChunkAlloc* fHeap;
171  size_t fMinChunkSize;
172 };
173 
174 #endif
const char * atStr(int index) const
Returns the index'th entry as a c-string, and assumes that the trailing null byte had been copied int...
Definition: SkDataTable.h:57
Like SkData, SkDataTable holds an immutable data buffer.
Definition: SkDataTable.h:21
Helper class that allows for incrementally building up the data needed to create a SkDataTable...
Definition: SkDataTable.h:124
Definition: SkRefCnt.h:135
Definition: SkChunkAlloc.h:15
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:258
void appendStr(const char str[])
Helper version of append() passes strlen() + 1 for the size, so the trailing-zero will be copied as w...
Definition: SkDataTable.h:149
int count() const
Return the number of entries in the table.
Definition: SkDataTable.h:31
void appendString(const SkString &string)
Helper version of append() passes string.size() + 1 for the size, so the trailing-zero will be copied...
Definition: SkDataTable.h:157
bool isEmpty() const
Returns true if the table is empty (i.e.
Definition: SkDataTable.h:26
Definition: SkDataTable.h:94
Light weight class for managing strings.
Definition: SkString.h:121