SkDataTable.h 3.7 KB

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