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/SkRefCnt.h"
12 #include "include/private/base/SkAPI.h"
13 #include "include/private/base/SkAssert.h"
14 
15 #include <cstdint>
16 #include <cstring>
17 
23 class SK_API SkDataTable : public SkRefCnt {
24 public:
28  bool isEmpty() const { return 0 == fCount; }
29 
33  int count() const { return fCount; }
34 
39  size_t atSize(int index) const;
40 
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 
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 
69 
79  static sk_sp<SkDataTable> MakeCopyArrays(const void * const * ptrs,
80  const size_t sizes[], int count);
81 
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
Like SkData, SkDataTable holds an immutable data buffer.
Definition: SkDataTable.h:23
static sk_sp< SkDataTable > MakeArrayProc(const void *array, size_t elemSize, int count, FreeProc proc, void *context)
int count() const
Return the number of entries in the table.
Definition: SkDataTable.h:33
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".
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.
const void * at(int index, size_t *size=nullptr) const
Return a pointer to the data of the index'th entry in the table.
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:59
const char * fElems
Definition: SkDataTable.h:105
bool isEmpty() const
Returns true if the table is empty (i.e.
Definition: SkDataTable.h:28
const T * atT(int index, size_t *size=nullptr) const
Definition: SkDataTable.h:51
static sk_sp< SkDataTable > MakeEmpty()
const Dir * fDir
Definition: SkDataTable.h:104
size_t atSize(int index) const
Return the size of the index'th entry in the table.
Definition: SkRefCnt.h:119
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:220