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 "include/core/SkRefCnt.h"
12 #include "include/private/base/SkAPI.h"
13 #include "include/private/base/SkAssert.h"
14 
15 #include <cstdint>
16 #include <cstdio>
17 
18 class SkStream;
19 
25 class SK_API SkData final : public SkNVRefCnt<SkData> {
26 public:
30  size_t size() const { return fSize; }
31 
32  bool isEmpty() const { return 0 == fSize; }
33 
37  const void* data() const { return fPtr; }
38 
43  const uint8_t* bytes() const {
44  return reinterpret_cast<const uint8_t*>(fPtr);
45  }
46 
52  void* writable_data() {
53  if (fSize) {
54  // only assert we're unique if we're not empty
55  SkASSERT(this->unique());
56  }
57  return const_cast<void*>(fPtr);
58  }
59 
66  size_t copyRange(size_t offset, size_t length, void* buffer) const;
67 
72  bool equals(const SkData* other) const;
73 
78  typedef void (*ReleaseProc)(const void* ptr, void* context);
79 
83  static sk_sp<SkData> MakeWithCopy(const void* data, size_t length);
84 
85 
90  static sk_sp<SkData> MakeUninitialized(size_t length);
91 
96  static sk_sp<SkData> MakeZeroInitialized(size_t length);
97 
104  static sk_sp<SkData> MakeWithCString(const char cstr[]);
105 
110  static sk_sp<SkData> MakeWithProc(const void* ptr, size_t length, ReleaseProc proc, void* ctx);
111 
116  static sk_sp<SkData> MakeWithoutCopy(const void* data, size_t length) {
117  return MakeWithProc(data, length, NoopReleaseProc, nullptr);
118  }
119 
124  static sk_sp<SkData> MakeFromMalloc(const void* data, size_t length);
125 
130  static sk_sp<SkData> MakeFromFileName(const char path[]);
131 
139  static sk_sp<SkData> MakeFromFILE(FILE* f);
140 
148  static sk_sp<SkData> MakeFromFD(int fd);
149 
155  static sk_sp<SkData> MakeFromStream(SkStream*, size_t size);
156 
161  static sk_sp<SkData> MakeSubset(const SkData* src, size_t offset, size_t length);
162 
168 
169 private:
170  friend class SkNVRefCnt<SkData>;
171  ReleaseProc fReleaseProc;
172  void* fReleaseProcContext;
173  const void* fPtr;
174  size_t fSize;
175 
176  SkData(const void* ptr, size_t size, ReleaseProc, void* context);
177  explicit SkData(size_t size); // inplace new/delete
178  ~SkData();
179 
180  // Ensure the unsized delete is called.
181  void operator delete(void* p);
182 
183  // shared internal factory
184  static sk_sp<SkData> PrivateNewWithCopy(const void* srcOrNull, size_t length);
185 
186  static void NoopReleaseProc(const void*, void*); // {}
187 
188  using INHERITED = SkRefCnt;
189 };
190 
191 #endif
SkData holds an immutable data buffer.
Definition: SkData.h:25
static sk_sp< SkData > MakeZeroInitialized(size_t length)
Create a new data with zero-initialized contents.
static sk_sp< SkData > MakeFromStream(SkStream *, size_t size)
Attempt to read size bytes into a SkData.
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.
static sk_sp< SkData > MakeFromMalloc(const void *data, size_t length)
Create a new dataref from a pointer allocated by malloc.
bool equals(const SkData *other) const
Returns true if these two objects have the same length and contents, effectively returning 0 == memcm...
static sk_sp< SkData > MakeFromFILE(FILE *f)
Create a new dataref from a stdio FILE.
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.
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:116
static sk_sp< SkData > MakeUninitialized(size_t length)
Create a new data with uninitialized contents.
static sk_sp< SkData > MakeEmpty()
Returns a new empty dataref (or a reference to a shared empty dataref).
const void * data() const
Returns the ptr to the data.
Definition: SkData.h:37
static sk_sp< SkData > MakeWithCString(const char cstr[])
Create a new dataref by copying the specified c-string (a null-terminated array of bytes).
void * writable_data()
USE WITH CAUTION.
Definition: SkData.h:52
static sk_sp< SkData > MakeFromFileName(const char path[])
Create a new dataref the file with the specified path.
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:43
static sk_sp< SkData > MakeFromFD(int fd)
Create a new dataref from a file descriptor.
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.
size_t size() const
Returns the number of bytes stored.
Definition: SkData.h:30
bool isEmpty() const
Definition: SkData.h:32
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Create a new dataref by copying the specified data.
Definition: SkRefCnt.h:160
bool unique() const
Definition: SkRefCnt.h:175
Definition: SkRefCnt.h:119
SkStream – abstraction for a source of bytes.
Definition: SkStream.h:29