Skia
2D Graphics Library
SkDataTable.h
Go to the documentation of this file.
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/SkData.h"
12 #include "include/private/SkTDArray.h"
13 
19 class SK_API SkDataTable : public SkRefCnt {
20 public:
24  bool isEmpty() const { return 0 == fCount; }
25 
29  int count() const { return fCount; }
30 
35  size_t atSize(int index) const;
36 
44  const void* at(int index, size_t* size = nullptr) const;
45 
46  template <typename T>
47  const T* atT(int index, size_t* size = nullptr) const {
48  return reinterpret_cast<const T*>(this->at(index, size));
49  }
50 
55  const char* atStr(int index) const {
56  size_t size;
57  const char* str = this->atT<const char>(index, &size);
58  SkASSERT(strlen(str) + 1 == size);
59  return str;
60  }
61 
62  typedef void (*FreeProc)(void* context);
63 
65 
75  static sk_sp<SkDataTable> MakeCopyArrays(const void * const * ptrs,
76  const size_t sizes[], int count);
77 
86  static sk_sp<SkDataTable> MakeCopyArray(const void* array, size_t elemSize, int count);
87 
88  static sk_sp<SkDataTable> MakeArrayProc(const void* array, size_t elemSize, int count,
89  FreeProc proc, void* context);
90 
91 private:
92  struct Dir {
93  const void* fPtr;
94  uintptr_t fSize;
95  };
96 
97  int fCount;
98  size_t fElemSize;
99  union {
100  const Dir* fDir;
101  const char* fElems;
102  } fU;
103 
104  FreeProc fFreeProc;
105  void* fFreeProcContext;
106 
107  SkDataTable();
108  SkDataTable(const void* array, size_t elemSize, int count,
109  FreeProc, void* context);
110  SkDataTable(const Dir*, int count, FreeProc, void* context);
111  ~SkDataTable() override;
112 
113  friend class SkDataTableBuilder; // access to Dir
114 
115  using INHERITED = SkRefCnt;
116 };
117 
118 #endif
SkRefCnt
Definition: SkRefCnt.h:118
SkDataTable::MakeCopyArray
static sk_sp< SkDataTable > MakeCopyArray(const void *array, size_t elemSize, int count)
Return a new table that contains a copy of the data in array.
SkDataTable::MakeEmpty
static sk_sp< SkDataTable > MakeEmpty()
SkDataTable::atStr
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:55
sk_sp
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:215
SK_API
#define SK_API
Definition: SkTypes.h:181
SkASSERT
#define SkASSERT(cond)
Definition: SkTypes.h:460
SkDataTable::MakeArrayProc
static sk_sp< SkDataTable > MakeArrayProc(const void *array, size_t elemSize, int count, FreeProc proc, void *context)
SkData.h
SkDataTable::fElems
const char * fElems
Definition: SkDataTable.h:101
SkDataTable::at
const void * at(int index, size_t *size=nullptr) const
Return a pointer to the data of the index'th entry in the table.
SkDataTable
Like SkData, SkDataTable holds an immutable data buffer.
Definition: SkDataTable.h:19
SkDataTable::atT
const T * atT(int index, size_t *size=nullptr) const
Definition: SkDataTable.h:47
SkDataTable::atSize
size_t atSize(int index) const
Return the size of the index'th entry in the table.
SkDataTable::fDir
const Dir * fDir
Definition: SkDataTable.h:100
SkDataTable::isEmpty
bool isEmpty() const
Returns true if the table is empty (i.e.
Definition: SkDataTable.h:24
SkDataTable::MakeCopyArrays
static sk_sp< SkDataTable > MakeCopyArrays(const void *const *ptrs, const size_t sizes[], int count)
Return a new DataTable that contains a copy of the data stored in each "array".
SkDataTable::count
int count() const
Return the number of entries in the table.
Definition: SkDataTable.h:29