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 "include/core/SkRefCnt.h" 12 #include "include/private/base/SkAPI.h" 13 #include "include/private/base/SkAssert.h" 14 15 #include <cstdint> 16 #include <cstring> 17 18 /** 19 * Like SkData, SkDataTable holds an immutable data buffer. The data buffer is 20 * organized into a table of entries, each with a length, so the entries are 21 * not required to all be the same size. 22 */ 23 class SK_API SkDataTable : public SkRefCnt { 24 public: 25 /** 26 * Returns true if the table is empty (i.e. has no entries). 27 */ isEmpty()28 bool isEmpty() const { return 0 == fCount; } 29 30 /** 31 * Return the number of entries in the table. 0 for an empty table 32 */ count()33 int count() const { return fCount; } 34 35 /** 36 * Return the size of the index'th entry in the table. The caller must 37 * ensure that index is valid for this table. 38 */ 39 size_t atSize(int index) const; 40 41 /** 42 * Return a pointer to the data of the index'th entry in the table. 43 * The caller must ensure that index is valid for this table. 44 * 45 * @param size If non-null, this returns the byte size of this entry. This 46 * will be the same value that atSize(index) would return. 47 */ 48 const void* at(int index, size_t* size = nullptr) const; 49 50 template <typename T> 51 const T* atT(int index, size_t* size = nullptr) const { 52 return reinterpret_cast<const T*>(this->at(index, size)); 53 } 54 55 /** 56 * Returns the index'th entry as a c-string, and assumes that the trailing 57 * null byte had been copied into the table as well. 58 */ atStr(int index)59 const char* atStr(int index) const { 60 size_t size; 61 const char* str = this->atT<const char>(index, &size); 62 SkASSERT(strlen(str) + 1 == size); 63 return str; 64 } 65 66 typedef void (*FreeProc)(void* context); 67 68 static sk_sp<SkDataTable> MakeEmpty(); 69 70 /** 71 * Return a new DataTable that contains a copy of the data stored in each 72 * "array". 73 * 74 * @param ptrs array of points to each element to be copied into the table. 75 * @param sizes array of byte-lengths for each entry in the corresponding 76 * ptrs[] array. 77 * @param count the number of array elements in ptrs[] and sizes[] to copy. 78 */ 79 static sk_sp<SkDataTable> MakeCopyArrays(const void * const * ptrs, 80 const size_t sizes[], int count); 81 82 /** 83 * Return a new table that contains a copy of the data in array. 84 * 85 * @param array contiguous array of data for all elements to be copied. 86 * @param elemSize byte-length for a given element. 87 * @param count the number of entries to be copied out of array. The number 88 * of bytes that will be copied is count * elemSize. 89 */ 90 static sk_sp<SkDataTable> MakeCopyArray(const void* array, size_t elemSize, int count); 91 92 static sk_sp<SkDataTable> MakeArrayProc(const void* array, size_t elemSize, int count, 93 FreeProc proc, void* context); 94 95 private: 96 struct Dir { 97 const void* fPtr; 98 uintptr_t fSize; 99 }; 100 101 int fCount; 102 size_t fElemSize; 103 union { 104 const Dir* fDir; 105 const char* fElems; 106 } fU; 107 108 FreeProc fFreeProc; 109 void* fFreeProcContext; 110 111 SkDataTable(); 112 SkDataTable(const void* array, size_t elemSize, int count, 113 FreeProc, void* context); 114 SkDataTable(const Dir*, int count, FreeProc, void* context); 115 ~SkDataTable() override; 116 117 friend class SkDataTableBuilder; // access to Dir 118 119 using INHERITED = SkRefCnt; 120 }; 121 122 #endif 123