Skia
2D Graphics Library
SkData.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 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 SkData_DEFINED
9 #define SkData_DEFINED
10 
11 #include <stdio.h>
12 
13 #include "include/core/SkRefCnt.h"
14 
15 class SkStream;
16 
22 class SK_API SkData final : public SkNVRefCnt<SkData> {
23 public:
27  size_t size() const { return fSize; }
28 
29  bool isEmpty() const { return 0 == fSize; }
30 
34  const void* data() const { return fPtr; }
35 
40  const uint8_t* bytes() const {
41  return reinterpret_cast<const uint8_t*>(fPtr);
42  }
43 
49  void* writable_data() {
50  if (fSize) {
51  // only assert we're unique if we're not empty
52  SkASSERT(this->unique());
53  }
54  return const_cast<void*>(fPtr);
55  }
56 
63  size_t copyRange(size_t offset, size_t length, void* buffer) const;
64 
69  bool equals(const SkData* other) const;
70 
75  typedef void (*ReleaseProc)(const void* ptr, void* context);
76 
80  static sk_sp<SkData> MakeWithCopy(const void* data, size_t length);
81 
82 
87  static sk_sp<SkData> MakeUninitialized(size_t length);
88 
95  static sk_sp<SkData> MakeWithCString(const char cstr[]);
96 
101  static sk_sp<SkData> MakeWithProc(const void* ptr, size_t length, ReleaseProc proc, void* ctx);
102 
107  static sk_sp<SkData> MakeWithoutCopy(const void* data, size_t length) {
108  return MakeWithProc(data, length, DummyReleaseProc, nullptr);
109  }
110 
115  static sk_sp<SkData> MakeFromMalloc(const void* data, size_t length);
116 
121  static sk_sp<SkData> MakeFromFileName(const char path[]);
122 
130  static sk_sp<SkData> MakeFromFILE(FILE* f);
131 
139  static sk_sp<SkData> MakeFromFD(int fd);
140 
146  static sk_sp<SkData> MakeFromStream(SkStream*, size_t size);
147 
152  static sk_sp<SkData> MakeSubset(const SkData* src, size_t offset, size_t length);
153 
158  static sk_sp<SkData> MakeEmpty();
159 
160 private:
161  friend class SkNVRefCnt<SkData>;
162  ReleaseProc fReleaseProc;
163  void* fReleaseProcContext;
164  const void* fPtr;
165  size_t fSize;
166 
167  SkData(const void* ptr, size_t size, ReleaseProc, void* context);
168  explicit SkData(size_t size); // inplace new/delete
169  ~SkData();
170 
171  // Ensure the unsized delete is called.
172  void operator delete(void* p);
173 
174  // shared internal factory
175  static sk_sp<SkData> PrivateNewWithCopy(const void* srcOrNull, size_t length);
176 
177  static void DummyReleaseProc(const void*, void*); // {}
178 
179  using INHERITED = SkRefCnt;
180 };
181 
182 #endif
SkData::MakeFromFileName
static sk_sp< SkData > MakeFromFileName(const char path[])
Create a new dataref the file with the specified path.
SkRefCnt
Definition: SkRefCnt.h:118
SkData::MakeWithCopy
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Create a new dataref by copying the specified data.
SkData::MakeFromFD
static sk_sp< SkData > MakeFromFD(int fd)
Create a new dataref from a file descriptor.
SkNVRefCnt
Definition: SkRefCnt.h:159
SkData::MakeFromMalloc
static sk_sp< SkData > MakeFromMalloc(const void *data, size_t length)
Create a new dataref from a pointer allocated by malloc.
SkNVRefCnt::unique
bool unique() const
Definition: SkRefCnt.h:174
SkData::bytes
const uint8_t * bytes() const
Like data(), returns a read-only ptr into the data, but in this case it is cast to uint8_t*,...
Definition: SkData.h:40
SkData::isEmpty
bool isEmpty() const
Definition: SkData.h:29
SkData::MakeSubset
static sk_sp< SkData > MakeSubset(const SkData *src, size_t offset, size_t length)
Create a new dataref using a subset of the data in the specified src dataref.
SkData
SkData holds an immutable data buffer.
Definition: SkData.h:22
SkStream
SkStream – abstraction for a source of bytes.
Definition: SkStream.h:41
SkRefCnt.h
sk_sp< SkData >
SkData::size
size_t size() const
Returns the number of bytes stored.
Definition: SkData.h:27
SK_API
#define SK_API
Definition: SkTypes.h:181
SkASSERT
#define SkASSERT(cond)
Definition: SkTypes.h:460
SkData::copyRange
size_t copyRange(size_t offset, size_t length, void *buffer) const
Helper to copy a range of the data into a caller-provided buffer.
SkData::MakeWithCString
static sk_sp< SkData > MakeWithCString(const char cstr[])
Create a new dataref by copying the specified c-string (a null-terminated array of bytes).
SkData::data
const void * data() const
Returns the ptr to the data.
Definition: SkData.h:34
SkData::MakeUninitialized
static sk_sp< SkData > MakeUninitialized(size_t length)
Create a new data with uninitialized contents.
SkData::MakeFromStream
static sk_sp< SkData > MakeFromStream(SkStream *, size_t size)
Attempt to read size bytes into a SkData.
f
float f
Definition: skcms.cc:52
SkData::MakeWithProc
static sk_sp< SkData > MakeWithProc(const void *ptr, size_t length, ReleaseProc proc, void *ctx)
Create a new dataref, taking the ptr as is, and using the releaseproc to free it.
SkData::MakeWithoutCopy
static sk_sp< SkData > MakeWithoutCopy(const void *data, size_t length)
Call this when the data parameter is already const and will outlive the lifetime of the SkData.
Definition: SkData.h:107
SkData::equals
bool equals(const SkData *other) const
Returns true if these two objects have the same length and contents, effectively returning 0 == memcm...
SkData::writable_data
void * writable_data()
USE WITH CAUTION.
Definition: SkData.h:49
SkData::MakeFromFILE
static sk_sp< SkData > MakeFromFILE(FILE *f)
Create a new dataref from a stdio FILE.