OLD | NEW |
(Empty) | |
| 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 "SkChunkAlloc.h" |
| 12 #include "SkData.h" |
| 13 #include "SkFlattenable.h" |
| 14 #include "SkString.h" |
| 15 #include "SkTDArray.h" |
| 16 |
| 17 /** |
| 18 * Like SkData, SkDataTable holds an immutable data buffer. The data buffer is |
| 19 * organized into a table of entries, each with a length, so the entries are |
| 20 * not required to all be the same size. |
| 21 */ |
| 22 class SK_API SkDataTable : public SkFlattenable { |
| 23 public: |
| 24 SK_DECLARE_INST_COUNT(SkDataTable) |
| 25 |
| 26 /** |
| 27 * Returns true if the table is empty (i.e. has no entries). |
| 28 */ |
| 29 bool isEmpty() const { return 0 == fCount; } |
| 30 |
| 31 /** |
| 32 * Return the number of entries in the table. 0 for an empty table |
| 33 */ |
| 34 int count() const { return fCount; } |
| 35 |
| 36 /** |
| 37 * Return the size of the index'th entry in the table. The caller must |
| 38 * ensure that index is valid for this table. |
| 39 */ |
| 40 size_t atSize(int index) const; |
| 41 |
| 42 /** |
| 43 * Return a pointer to the data of the index'th entry in the table. |
| 44 * The caller must ensure that index is valid for this table. |
| 45 * |
| 46 * @param size If non-null, this returns the byte size of this entry. This |
| 47 * will be the same value that atSize(index) would return. |
| 48 */ |
| 49 const void* atData(int index, size_t* size = NULL) const; |
| 50 |
| 51 template <typename T> |
| 52 const T* atDataT(int index, size_t* size = NULL) const { |
| 53 return reinterpret_cast<const T*>(this->atData(index, size)); |
| 54 } |
| 55 |
| 56 /** |
| 57 * Returns the index'th entry as a c-string, and assumes that the trailing |
| 58 * null byte had been copied into the table as well. |
| 59 */ |
| 60 const char* atStr(int index) const { |
| 61 size_t size; |
| 62 const char* str = this->atDataT<const char>(index, &size); |
| 63 SkASSERT(strlen(str) + 1 == size); |
| 64 return str; |
| 65 } |
| 66 |
| 67 /** |
| 68 * Return a new DataTable that contains a copy of the data stored in each |
| 69 * "array". |
| 70 * |
| 71 * @param ptrs array of points to each element to be copied into the table. |
| 72 * @param sizes array of byte-lengths for each entry in the corresponding |
| 73 * ptrs[] array. |
| 74 * @param count the number of array elements in ptrs[] and sizes[] to copy. |
| 75 */ |
| 76 static SkDataTable* NewCopyArrays(const void * const * ptrs, const size_t si
zes[], |
| 77 int count); |
| 78 |
| 79 /** |
| 80 * Return a new table that contains a copy of the data in array. |
| 81 * |
| 82 * @param array contiguous array of data for all elements to be copied. |
| 83 * @param elemSize byte-length for a given element. |
| 84 * @param count the number of entries to be copied out of array. The number |
| 85 * of bytes that will be copied is count * elemSize. |
| 86 */ |
| 87 static SkDataTable* NewCopyArray(const void* array, size_t elemSize, |
| 88 int count); |
| 89 |
| 90 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDataTable) |
| 91 |
| 92 protected: |
| 93 SkDataTable(SkFlattenableReadBuffer&); |
| 94 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; |
| 95 |
| 96 private: |
| 97 SkDataTable(int count, SkData* dataWeTakeOverOwnership); |
| 98 virtual ~SkDataTable(); |
| 99 |
| 100 int fCount; |
| 101 SkData* fData; |
| 102 |
| 103 typedef SkFlattenable INHERITED; |
| 104 }; |
| 105 |
| 106 /** |
| 107 * Helper class that allows for incrementally building up the data needed to |
| 108 * create a SkDataTable. |
| 109 */ |
| 110 class SK_API SkDataTableBuilder { |
| 111 public: |
| 112 SkDataTableBuilder(size_t minChunkSize); |
| 113 ~SkDataTableBuilder(); |
| 114 |
| 115 int count() const { return fSizes.count(); } |
| 116 |
| 117 /** |
| 118 * Forget any previously appended entries, setting count() back to 0. |
| 119 */ |
| 120 void reset(); |
| 121 |
| 122 /** |
| 123 * Copy size-bytes from data, and append it to the growing SkDataTable. |
| 124 */ |
| 125 void append(const void* data, size_t size); |
| 126 |
| 127 /** |
| 128 * Helper version of append() passes strlen() + 1 for the size, |
| 129 * so the trailing-zero will be copied as well. |
| 130 */ |
| 131 void appendStr(const char str[]) { |
| 132 this->append(str, strlen(str) + 1); |
| 133 } |
| 134 |
| 135 /** |
| 136 * Helper version of append() passes string.size() + 1 for the size, |
| 137 * so the trailing-zero will be copied as well. |
| 138 */ |
| 139 void appendString(const SkString& string) { |
| 140 this->append(string.c_str(), string.size() + 1); |
| 141 } |
| 142 |
| 143 /** |
| 144 * Return an SkDataTable from the accumulated entries that were added by |
| 145 * calls to append(). This data is logically distinct from the builder, and |
| 146 * will not be affected by any subsequent calls to the builder. |
| 147 */ |
| 148 SkDataTable* createDataTable(); |
| 149 |
| 150 private: |
| 151 SkTDArray<size_t> fSizes; |
| 152 SkTDArray<void*> fPtrs; |
| 153 SkChunkAlloc fHeap; |
| 154 }; |
| 155 |
| 156 #endif |
OLD | NEW |