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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright 2013 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #ifndef SkDataTable_DEFINED
michael@0 9 #define SkDataTable_DEFINED
michael@0 10
michael@0 11 #include "SkChunkAlloc.h"
michael@0 12 #include "SkData.h"
michael@0 13 #include "SkString.h"
michael@0 14 #include "SkTDArray.h"
michael@0 15
michael@0 16 /**
michael@0 17 * Like SkData, SkDataTable holds an immutable data buffer. The data buffer is
michael@0 18 * organized into a table of entries, each with a length, so the entries are
michael@0 19 * not required to all be the same size.
michael@0 20 */
michael@0 21 class SK_API SkDataTable : public SkRefCnt {
michael@0 22 public:
michael@0 23 SK_DECLARE_INST_COUNT(SkDataTable)
michael@0 24
michael@0 25 /**
michael@0 26 * Returns true if the table is empty (i.e. has no entries).
michael@0 27 */
michael@0 28 bool isEmpty() const { return 0 == fCount; }
michael@0 29
michael@0 30 /**
michael@0 31 * Return the number of entries in the table. 0 for an empty table
michael@0 32 */
michael@0 33 int count() const { return fCount; }
michael@0 34
michael@0 35 /**
michael@0 36 * Return the size of the index'th entry in the table. The caller must
michael@0 37 * ensure that index is valid for this table.
michael@0 38 */
michael@0 39 size_t atSize(int index) const;
michael@0 40
michael@0 41 /**
michael@0 42 * Return a pointer to the data of the index'th entry in the table.
michael@0 43 * The caller must ensure that index is valid for this table.
michael@0 44 *
michael@0 45 * @param size If non-null, this returns the byte size of this entry. This
michael@0 46 * will be the same value that atSize(index) would return.
michael@0 47 */
michael@0 48 const void* at(int index, size_t* size = NULL) const;
michael@0 49
michael@0 50 template <typename T>
michael@0 51 const T* atT(int index, size_t* size = NULL) const {
michael@0 52 return reinterpret_cast<const T*>(this->at(index, size));
michael@0 53 }
michael@0 54
michael@0 55 /**
michael@0 56 * Returns the index'th entry as a c-string, and assumes that the trailing
michael@0 57 * null byte had been copied into the table as well.
michael@0 58 */
michael@0 59 const char* atStr(int index) const {
michael@0 60 size_t size;
michael@0 61 const char* str = this->atT<const char>(index, &size);
michael@0 62 SkASSERT(strlen(str) + 1 == size);
michael@0 63 return str;
michael@0 64 }
michael@0 65
michael@0 66 typedef void (*FreeProc)(void* context);
michael@0 67
michael@0 68 static SkDataTable* NewEmpty();
michael@0 69
michael@0 70 /**
michael@0 71 * Return a new DataTable that contains a copy of the data stored in each
michael@0 72 * "array".
michael@0 73 *
michael@0 74 * @param ptrs array of points to each element to be copied into the table.
michael@0 75 * @param sizes array of byte-lengths for each entry in the corresponding
michael@0 76 * ptrs[] array.
michael@0 77 * @param count the number of array elements in ptrs[] and sizes[] to copy.
michael@0 78 */
michael@0 79 static SkDataTable* NewCopyArrays(const void * const * ptrs,
michael@0 80 const size_t sizes[], int count);
michael@0 81
michael@0 82 /**
michael@0 83 * Return a new table that contains a copy of the data in array.
michael@0 84 *
michael@0 85 * @param array contiguous array of data for all elements to be copied.
michael@0 86 * @param elemSize byte-length for a given element.
michael@0 87 * @param count the number of entries to be copied out of array. The number
michael@0 88 * of bytes that will be copied is count * elemSize.
michael@0 89 */
michael@0 90 static SkDataTable* NewCopyArray(const void* array, size_t elemSize,
michael@0 91 int count);
michael@0 92
michael@0 93 static SkDataTable* NewArrayProc(const void* array, size_t elemSize,
michael@0 94 int count, FreeProc proc, void* context);
michael@0 95
michael@0 96 private:
michael@0 97 struct Dir {
michael@0 98 const void* fPtr;
michael@0 99 uintptr_t fSize;
michael@0 100 };
michael@0 101
michael@0 102 int fCount;
michael@0 103 size_t fElemSize;
michael@0 104 union {
michael@0 105 const Dir* fDir;
michael@0 106 const char* fElems;
michael@0 107 } fU;
michael@0 108
michael@0 109 FreeProc fFreeProc;
michael@0 110 void* fFreeProcContext;
michael@0 111
michael@0 112 SkDataTable();
michael@0 113 SkDataTable(const void* array, size_t elemSize, int count,
michael@0 114 FreeProc, void* context);
michael@0 115 SkDataTable(const Dir*, int count, FreeProc, void* context);
michael@0 116 virtual ~SkDataTable();
michael@0 117
michael@0 118 friend class SkDataTableBuilder; // access to Dir
michael@0 119
michael@0 120 typedef SkRefCnt INHERITED;
michael@0 121 };
michael@0 122
michael@0 123 /**
michael@0 124 * Helper class that allows for incrementally building up the data needed to
michael@0 125 * create a SkDataTable.
michael@0 126 */
michael@0 127 class SK_API SkDataTableBuilder : SkNoncopyable {
michael@0 128 public:
michael@0 129 SkDataTableBuilder(size_t minChunkSize);
michael@0 130 ~SkDataTableBuilder();
michael@0 131
michael@0 132 int count() const { return fDir.count(); }
michael@0 133 size_t minChunkSize() const { return fMinChunkSize; }
michael@0 134
michael@0 135 /**
michael@0 136 * Forget any previously appended entries, setting count() back to 0.
michael@0 137 */
michael@0 138 void reset(size_t minChunkSize);
michael@0 139 void reset() {
michael@0 140 this->reset(fMinChunkSize);
michael@0 141 }
michael@0 142
michael@0 143 /**
michael@0 144 * Copy size-bytes from data, and append it to the growing SkDataTable.
michael@0 145 */
michael@0 146 void append(const void* data, size_t size);
michael@0 147
michael@0 148 /**
michael@0 149 * Helper version of append() passes strlen() + 1 for the size,
michael@0 150 * so the trailing-zero will be copied as well.
michael@0 151 */
michael@0 152 void appendStr(const char str[]) {
michael@0 153 this->append(str, strlen(str) + 1);
michael@0 154 }
michael@0 155
michael@0 156 /**
michael@0 157 * Helper version of append() passes string.size() + 1 for the size,
michael@0 158 * so the trailing-zero will be copied as well.
michael@0 159 */
michael@0 160 void appendString(const SkString& string) {
michael@0 161 this->append(string.c_str(), string.size() + 1);
michael@0 162 }
michael@0 163
michael@0 164 /**
michael@0 165 * Return an SkDataTable from the accumulated entries that were added by
michael@0 166 * calls to append(). This call also clears any accumluated entries from
michael@0 167 * this builder, so its count() will be 0 after this call.
michael@0 168 */
michael@0 169 SkDataTable* detachDataTable();
michael@0 170
michael@0 171 private:
michael@0 172 SkTDArray<SkDataTable::Dir> fDir;
michael@0 173 SkChunkAlloc* fHeap;
michael@0 174 size_t fMinChunkSize;
michael@0 175 };
michael@0 176
michael@0 177 #endif

mercurial