Skia
2DGraphicsLibrary
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkData.h
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 "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 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  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) { ::operator delete(p); }
173 
174  // Called the first time someone calls NewEmpty to initialize the singleton.
175  friend SkData* sk_new_empty_data();
176 
177  // shared internal factory
178  static sk_sp<SkData> PrivateNewWithCopy(const void* srcOrNull, size_t length);
179 
180  static void DummyReleaseProc(const void*, void*); // {}
181 
182  typedef SkRefCnt INHERITED;
183 };
184 
185 #endif
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
size_t size() const
Returns the number of bytes stored.
Definition: SkData.h:27
Definition: SkRefCnt.h:125
void * writable_data()
USE WITH CAUTION.
Definition: SkData.h:49
SkData holds an immutable data buffer.
Definition: SkData.h:22
SkStream – abstraction for a source of bytes.
Definition: SkStream.h:40
const void * data() const
Returns the ptr to the data.
Definition: SkData.h:34
Definition: SkRefCnt.h:209
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